from __future__ import annotations

"""Read and write GitMo's user settings.

The config file lives outside the project so installed copies and source-tree
copies share the same saved token, GitMo folder, and selected repos.
"""

import json
from dataclasses import asdict, dataclass, field
from pathlib import Path


APP_DIR = Path.home() / ".config" / "gitmo"
CONFIG_PATH = APP_DIR / "config.json"
LOG_PATH = APP_DIR / "gitmo.log"
LEGACY_CONFIG_PATH = Path.home() / ".config" / "gitlo" / "config.json"


@dataclass
class RepoConfig:
    name: str
    local_path: str = ""
    sync_mode: str = "two-way"
    enabled: bool = True


@dataclass
class AppConfig:
    github_token: str = ""
    github_login: str = ""
    github_email: str = ""
    gitmo_path: str = ""
    font_size_delta: int = 0
    repos: dict[str, RepoConfig] = field(default_factory=dict)

    @classmethod
    def from_dict(cls, data: dict) -> "AppConfig":
        repos = {
            name: RepoConfig(**repo_data)
            for name, repo_data in data.get("repos", {}).items()
        }
        return cls(
            github_token=data.get("github_token", ""),
            github_login=data.get("github_login", ""),
            github_email=data.get("github_email", ""),
            gitmo_path=data.get("gitmo_path", ""),
            font_size_delta=int(data.get("font_size_delta", 0)),
            repos=repos,
        )

    def to_dict(self) -> dict:
        data = asdict(self)
        data["repos"] = {
            name: asdict(repo_config)
            for name, repo_config in self.repos.items()
        }
        return data


def load_config() -> AppConfig:
    config_path = CONFIG_PATH if CONFIG_PATH.exists() else LEGACY_CONFIG_PATH
    if not config_path.exists():
        return AppConfig()
    data = json.loads(config_path.read_text(encoding="utf-8"))
    return AppConfig.from_dict(data)


def save_config(config: AppConfig) -> None:
    APP_DIR.mkdir(parents=True, exist_ok=True)
    CONFIG_PATH.write_text(
        json.dumps(config.to_dict(), indent=2, sort_keys=True),
        encoding="utf-8",
    )
