from __future__ import annotations

from dataclasses import dataclass, field
from typing import Iterable

from ..constants import CAPABILITY_LEVEL_READ_ONLY, SKILL_SCOPE_PUBLIC


@dataclass(frozen=True)
class SkillMatch:
    score: int = 0
    reason: str = ""

    @property
    def matched(self) -> bool:
        return self.score > 0


@dataclass(frozen=True)
class SkillContextResult:
    instruction: str = ""
    items: tuple[str, ...] = field(default_factory=tuple)
    metadata: dict = field(default_factory=dict)

    @classmethod
    def from_items(
        cls, items: Iterable[str], instruction: str = "", metadata: dict | None = None
    ) -> "SkillContextResult":
        return cls(
            instruction=instruction,
            items=tuple(item for item in items if item),
            metadata=metadata or {},
        )


class AiAssistantSkill:
    key = ""
    label = ""
    allowed_modes: tuple[str, ...] = ()
    public_safe = True
    skill_scope = SKILL_SCOPE_PUBLIC
    capability_level = CAPABILITY_LEVEL_READ_ONLY

    def is_enabled(self, site, settings_obj, mode: str) -> bool:
        return mode in self.allowed_modes

    def match(
        self, request_text: str, request, site, mode: str, settings_obj
    ) -> SkillMatch:
        return SkillMatch()

    def build_context(
        self, request_text: str, request, site, mode: str, settings_obj
    ) -> SkillContextResult:
        return SkillContextResult()
