"""設定ファイル（TOML）の読み込みと検証.

データの場所（目録・ストア・取り込み元・計算サーバ）はコードに書かず，
研究室 PC のユーザー領域に置いた設定ファイルから受け取る（SPEC_01「設定ファイル」）．
"""

import os
import tomllib
from pathlib import Path

from pydantic import BaseModel, ConfigDict, ValidationInfo, field_validator, model_validator

CONFIG_ENV_VAR = "TIASDATA_CONFIG"

CATALOG_FILENAME = "catalog.sqlite"

# importer ごとの必須のキー・使わないキー（`path`・`importer` は常に必須）．
# 依存方向（importers → config）のため，importer の名前は文字列で持つ（SPEC_03「取り込み元」）
_REQUIRED_SOURCE_KEYS: dict[str, tuple[str, ...]] = {
    "deeptias_output_files": ("number_source",),
    "deeptias_new_captures": ("patient_map", "number_source"),
    "namiki_label_rounds": ("number_source",),
    "namiki_first_round_labels": ("number_source",),
}
_UNUSED_SOURCE_KEYS: dict[str, tuple[str, ...]] = {
    "deeptias_output_files": ("patient_map", "reference_table"),
    "deeptias_new_captures": ("reference_table",),
    "namiki_label_rounds": ("patient_map", "reference_table"),
    "namiki_first_round_labels": ("patient_map", "reference_table"),
}


def default_config_path() -> Path:
    """設定ファイルの場所を返す．

    環境変数 `TIASDATA_CONFIG` があればそのパス，無ければ `~/.config/tiasdata/config.toml`
    （Windows でも同じ相対位置．`%USERPROFILE%\\.config\\tiasdata\\config.toml`）．

    Returns:
        設定ファイルのパス（存在するかは確かめない）．
    """
    env = os.environ.get(CONFIG_ENV_VAR)
    if env:
        return Path(env).expanduser()
    return Path.home() / ".config" / "tiasdata" / "config.toml"


def _resolve_path(value: Path, info: ValidationInfo) -> Path:
    # 相対パスは設定ファイルのあるフォルダを基準にする（作業フォルダに依存させないため）
    path = value.expanduser()
    base_dir = info.context.get("base_dir") if info.context else None
    if base_dir is not None and not path.is_absolute():
        path = Path(base_dir) / path
    return path


class _StrictModel(BaseModel):
    # 未知のキーは拒否する（綴りの誤りで設定が黙って無視されるのを防ぐ）
    model_config = ConfigDict(extra="forbid", frozen=True)


class CatalogConfig(_StrictModel):
    """`[catalog]`: 目録・ストア・対応表の場所．

    Attributes:
        root: `catalog.sqlite`・`store/`・`bundles/`・`backups/` を置くフォルダ．
        identity: 対応表（`identity.sqlite`）のパス．`root` とは別のフォルダに置く．
    """

    root: Path
    identity: Path

    @field_validator("root", "identity")
    @classmethod
    def _resolve(cls, value: Path, info: ValidationInfo) -> Path:
        return _resolve_path(value, info)

    @model_validator(mode="after")
    def _identity_outside_root(self) -> "CatalogConfig":
        # 対応表は目録とは別のフォルダに置く（SPEC_01「構成要素」）．
        # 目録のバックアップ・受け渡しに対応表が紛れ込まないようにするため
        root = self.root.resolve()
        identity_dir = self.identity.resolve().parent
        if identity_dir == root or root in identity_dir.parents:
            raise ValueError("catalog.identity は catalog.root の外のフォルダに置く")
        return self

    @property
    def catalog_path(self) -> Path:
        """目録（`catalog.sqlite`）のパス．"""
        return self.root / CATALOG_FILENAME

    @property
    def store_dir(self) -> Path:
        """ストアのフォルダ．"""
        return self.root / "store"

    @property
    def bundles_dir(self) -> Path:
        """書き出したバンドルのフォルダ．"""
        return self.root / "bundles"

    @property
    def backups_dir(self) -> Path:
        """目録のバックアップのフォルダ．"""
        return self.root / "backups"


class SourceConfig(_StrictModel):
    """`[sources.<名前>]`: 取り込み元．

    Attributes:
        path: 取り込み元のフォルダ（読むだけ）．
        importer: 取り込み元の形（`importers/` のモジュール名．例: `tongue_color_folders`）．
        patient_map: 画像の番号 → 患者 ID の対応表のファイル（任意）．
        number_source: 番号 → 撮影を引くときに使う，登録済みの取り込み元の名前．1 つなら文字列，
            複数なら配列で書く（`deeptias_output_folders` 等で必須．`number_sources` で読む）．
        reference_table: 取り込む値と照合するだけの表（xlsx）のファイル（任意）．
    """

    path: Path
    importer: str
    patient_map: Path | None = None
    number_source: str | tuple[str, ...] | None = None
    reference_table: Path | None = None

    @field_validator("path", "patient_map", "reference_table")
    @classmethod
    def _resolve(cls, value: Path | None, info: ValidationInfo) -> Path | None:
        if value is None:
            return None
        return _resolve_path(value, info)

    @field_validator("number_source")
    @classmethod
    def _check_number_source(cls, value: str | tuple[str, ...] | None):
        if value is None:
            return None
        names = (value,) if isinstance(value, str) else value
        if not names or not all(names):
            raise ValueError("number_source は空でない名前を 1 つ以上書く")
        if len(set(names)) != len(names):
            raise ValueError("number_source に同じ名前が 2 回以上ある")
        return value

    @model_validator(mode="after")
    def _check_importer_keys(self) -> "SourceConfig":
        missing = [k for k in _REQUIRED_SOURCE_KEYS.get(self.importer, ()) if not self._has(k)]
        if missing:
            raise ValueError(f"importer '{self.importer}' には {', '.join(missing)} が要る")
        unused = [k for k in _UNUSED_SOURCE_KEYS.get(self.importer, ()) if self._has(k)]
        if unused:
            # 使われない設定を，効いていると誤解させたまま残さない
            raise ValueError(f"importer '{self.importer}' は {', '.join(unused)} を使わない")
        return self

    def _has(self, key: str) -> bool:
        return getattr(self, key) is not None

    @property
    def number_sources(self) -> tuple[str, ...]:
        """番号を引く取り込み元の名前（`number_source` の 1 つの形・複数の形をそろえたもの）．"""
        if self.number_source is None:
            return ()
        return (self.number_source,) if isinstance(self.number_source, str) else self.number_source


class ServerConfig(_StrictModel):
    """`[servers.<名前>]`: 計算サーバ（受け渡し先）．

    Attributes:
        host: SSH の接続先（`user@host`）．
        key: SSH の秘密鍵のパス（研究室 PC 上）．
        root: サーバ上のバンドルの置き場所．サーバ側で解釈するため `~` は展開しない．
    """

    host: str
    key: Path
    root: str

    @field_validator("key")
    @classmethod
    def _resolve(cls, value: Path, info: ValidationInfo) -> Path:
        return _resolve_path(value, info)


class Config(_StrictModel):
    """設定ファイル全体．

    Attributes:
        catalog: 目録・ストア・対応表の場所．
        sources: 取り込み元（名前 → 設定）．
        servers: 計算サーバ（名前 → 設定）．
    """

    catalog: CatalogConfig
    sources: dict[str, SourceConfig] = {}
    servers: dict[str, ServerConfig] = {}


def load_config(path: Path | None = None) -> Config:
    """設定ファイルを読み込んで検証する．

    Args:
        path: 設定ファイルのパス．省略時は `default_config_path()`．

    Returns:
        検証済みの設定．相対パスは設定ファイルのあるフォルダ基準の絶対パスに，`~` は展開済み．

    Raises:
        ValueError: 設定ファイルが無い，または TOML として読めない．
        pydantic.ValidationError: 未知のキー・必須キーの欠落・値の形の誤り．
    """
    config_path = path if path is not None else default_config_path()
    try:
        raw = config_path.read_bytes()
    except FileNotFoundError:
        raise ValueError(
            f"設定ファイルが見つからない: {config_path}"
            f"（環境変数 {CONFIG_ENV_VAR} で場所を指定できる）"
        ) from None
    try:
        data = tomllib.loads(raw.decode("utf-8"))
    except (UnicodeDecodeError, tomllib.TOMLDecodeError) as e:
        raise ValueError(f"設定ファイルを TOML として読めない: {e}") from e
    base_dir = config_path.expanduser().resolve().parent
    return Config.model_validate(data, context={"base_dir": base_dir})
