# retoor <retoor@molodetz.nl>

from dataclasses import dataclass

from devplacepy.config import INTERNAL_MODEL
from devplacepy.database import get_setting, internal_gateway_key
from devplacepy.services.base import ConfigField

BASE_URL_DEFAULT = "https://retoor.molodetz.nl"
OWNER_DEFAULT = "retoor"
REPO_DEFAULT = "devplacepy"
HTTP_TIMEOUT_SECONDS = 20.0


@dataclass(frozen=True)
class GiteaConfig:
    base_url: str
    owner: str
    repo: str
    token: str
    ai_enhance: bool
    ai_model: str
    ai_key: str

    @property
    def api_base(self) -> str:
        return f"{self.base_url.rstrip('/')}/api/v1"

    @property
    def repo_base(self) -> str:
        return f"{self.api_base}/repos/{self.owner}/{self.repo}"

    @property
    def is_configured(self) -> bool:
        return bool(self.base_url and self.owner and self.repo and self.token)


CONFIG_FIELDS: list[ConfigField] = [
    ConfigField(
        "gitea_base_url",
        "Gitea base URL",
        type="url",
        default=BASE_URL_DEFAULT,
        help="Origin of the Gitea instance, without the /api path.",
        group="Gitea",
    ),
    ConfigField(
        "gitea_owner",
        "Repository owner",
        type="str",
        default=OWNER_DEFAULT,
        help="Owner (user or organisation) of the issue tracker repository.",
        group="Gitea",
    ),
    ConfigField(
        "gitea_repo",
        "Repository name",
        type="str",
        default=REPO_DEFAULT,
        help="Repository whose issues back the issue tracker.",
        group="Gitea",
    ),
    ConfigField(
        "gitea_token",
        "Gitea access token",
        type="password",
        default="",
        secret=True,
        help="Personal access token with repo issue scope. All actions use this single account.",
        group="Gitea",
    ),
    ConfigField(
        "issue_ai_enhance",
        "Improve tickets with AI",
        type="bool",
        default=True,
        help="Rewrite each submitted report into a consistent, high-quality ticket before posting.",
        group="AI",
    ),
    ConfigField(
        "issue_ai_model",
        "AI model",
        type="str",
        default=INTERNAL_MODEL,
        help="Model name sent to the internal AI gateway for ticket enhancement.",
        group="AI",
    ),
    ConfigField(
        "issue_ai_key",
        "AI API key",
        type="password",
        default="",
        secret=True,
        help="Defaults to the internal gateway key when left blank.",
        group="AI",
    ),
]


def gitea_config() -> GiteaConfig:
    ai_key = get_setting("issue_ai_key", "") or internal_gateway_key()
    return GiteaConfig(
        base_url=get_setting("gitea_base_url", BASE_URL_DEFAULT),
        owner=get_setting("gitea_owner", OWNER_DEFAULT),
        repo=get_setting("gitea_repo", REPO_DEFAULT),
        token=get_setting("gitea_token", ""),
        ai_enhance=get_setting("issue_ai_enhance", "1") == "1",
        ai_model=get_setting("issue_ai_model", INTERNAL_MODEL),
        ai_key=ai_key,
    )


def is_configured() -> bool:
    return gitea_config().is_configured
