from __future__ import annotations

import json
from pathlib import Path
from typing import Any

from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict


AGENT4_ROOT = Path(__file__).resolve().parents[2]


class Settings(BaseSettings):
    app_name: str = "Agent4 SW/ANSYS Learning Platform"
    host: str = "127.0.0.1"
    port: int = 7200

    knowledge_framework_path: Path = AGENT4_ROOT / "data" / "knowledge" / "reducer_three_layer_framework.json"
    legacy_knowledge_framework_path: Path | None = None
    standard_skillflow_dirs: list[Path] = Field(
        default_factory=lambda: [
            AGENT4_ROOT / "data" / "standards",
        ]
    )

    part_feature_audit_project: Path = AGENT4_ROOT / "tools" / "modeling-process" / "PartFeatureAudit" / "PartFeatureAudit.csproj"
    assembly_knowledge_audit_project: Path = AGENT4_ROOT / "tools" / "modeling-process" / "AssemblyKnowledgeAudit" / "AssemblyKnowledgeAudit.csproj"
    runtime_dir: Path = AGENT4_ROOT / "runtime"

    project_paths_file: Path = AGENT4_ROOT / "project_paths.json"

    model_config = SettingsConfigDict(
        env_file=str(AGENT4_ROOT / ".env"),
        env_prefix="AGENT4_",
        extra="ignore",
    )

    def software_paths(self) -> dict[str, Any]:
        if not self.project_paths_file.exists():
            return {}
        try:
            return json.loads(self.project_paths_file.read_text(encoding="utf-8-sig"))
        except Exception:
            return {}


settings = Settings()
