"""Tests for config."""

from __future__ import annotations

import copy
import os
import tempfile
from pathlib import Path

import pytest

from file_organizer.config import (
    DEFAULT_CONFIG,
    Config,
    load_config,
    save_config,
)


def default_config_copy() -> dict:
    """Return a deep copy of DEFAULT_CONFIG to avoid test pollution."""
    return copy.deepcopy(DEFAULT_CONFIG)


class TestConfigDefaults:
    def test_default_config_structure(self) -> None:
        config = DEFAULT_CONFIG
        assert "organize" in config
        assert "ignore" in config
        assert "safety" in config
        assert "advanced" in config

    def test_default_organize_settings(self) -> None:
        org = DEFAULT_CONFIG["organize"]
        assert org["default_mode"] == "type"
        assert org["date_format"] == "%Y-%m"
        assert "size_thresholds" in org

    def test_default_ignore_patterns(self) -> None:
        patterns = DEFAULT_CONFIG["ignore"]["patterns"]
        assert "*.tmp" in patterns
        assert "*.crdownload" in patterns
        assert "Thumbs.db" in patterns


class TestConfigLoading:
    def test_load_nonexistent(self, monkeypatch: pytest.MonkeyPatch) -> None:
        with tempfile.TemporaryDirectory() as tmpdir:
            monkeypatch.setenv("APPDATA", tmpdir)
            monkeypatch.setenv("XDG_CONFIG_HOME", tmpdir)
            config = load_config()
            assert config == DEFAULT_CONFIG

    def test_load_existing(self, monkeypatch: pytest.MonkeyPatch) -> None:
        with tempfile.TemporaryDirectory() as tmpdir:
            config_dir = Path(tmpdir) / "file-organizer"
            config_dir.mkdir()
            config_file = config_dir / "config.yaml"
            config_file.write_text("organize:\n  default_mode: date\n")

            monkeypatch.setenv("APPDATA", tmpdir)
            monkeypatch.setenv("XDG_CONFIG_HOME", tmpdir)
            config = load_config()
            assert config["organize"]["default_mode"] == "date"
            # Other defaults preserved
            assert config["organize"]["date_format"] == "%Y-%m"


class TestConfigSaving:
    def test_save_and_load(self, monkeypatch: pytest.MonkeyPatch) -> None:
        with tempfile.TemporaryDirectory() as tmpdir:
            monkeypatch.setenv("APPDATA", tmpdir)
            monkeypatch.setenv("XDG_CONFIG_HOME", tmpdir)

            config = default_config_copy()
            config["organize"]["default_mode"] = "size"
            save_config(config)

            loaded = load_config()
            assert loaded["organize"]["default_mode"] == "size"


class TestConfigClass:
    def test_attribute_access(self) -> None:
        config = Config(default_config_copy())
        assert config.organize.default_mode == "type"
        assert config.ignore.patterns == DEFAULT_CONFIG["ignore"]["patterns"]
        assert config.safety.dry_run_by_default is True

    def test_get_set(self) -> None:
        with tempfile.TemporaryDirectory() as tmpdir:
            config_dir = Path(tmpdir) / "file-organizer"
            config_dir.mkdir()

            os.environ["APPDATA"] = tmpdir
            os.environ["XDG_CONFIG_HOME"] = tmpdir

            config = Config(default_config_copy())
            config.set("organize.default_mode", "date")
            assert config.get("organize.default_mode") == "date"

    def test_default_mode_property(self) -> None:
        config = Config(default_config_copy())
        from file_organizer.models import OrganizeMode

        assert config.default_mode == OrganizeMode.TYPE

        config._data["organize"]["default_mode"] = "date"
        assert config.default_mode == OrganizeMode.DATE
