"""Data models for drafts, ratings, and categories."""

from __future__ import annotations

from dataclasses import dataclass, field
from datetime import datetime

# Map old verbose category names to canonical short names
CATEGORY_NORMALIZE: dict[str, str] = {
    "Agent-to-agent communication protocols": "A2A protocols",
    "AI safety / guardrails / alignment": "AI safety/alignment",
    "ML-based traffic management / optimization": "ML traffic mgmt",
    "Autonomous network operations": "Autonomous netops",
    "Identity / authentication for AI agents": "Agent identity/auth",
    "Data formats / semantics for AI interop": "Data formats/interop",
    "Policy / governance / ethical frameworks": "Policy/governance",
    "AI model serving / inference protocols": "Model serving/inference",
    "Agent discovery / registration": "Agent discovery/reg",
}


def normalize_category(cat: str) -> str:
    """Normalize a category name to its canonical short form."""
    return CATEGORY_NORMALIZE.get(cat, cat)


@dataclass
class Author:
    person_id: int
    name: str
    ascii_name: str = ""
    affiliation: str = ""
    resource_uri: str = ""
    fetched_at: str | None = None


@dataclass
class Draft:
    name: str  # e.g. "draft-zheng-dispatch-agent-identity-management"
    rev: str  # e.g. "00"
    title: str
    abstract: str
    time: str  # ISO datetime from API
    dt_id: int | None = None  # Datatracker document ID
    pages: int | None = None
    words: int | None = None
    group: str | None = None  # Working group name (resolved)
    group_uri: str | None = None  # Raw API URI
    expires: str | None = None
    ad: str | None = None  # Area director URI
    shepherd: str | None = None
    states: list[str] = field(default_factory=list)
    full_text: str | None = None
    categories: list[str] = field(default_factory=list)
    tags: list[str] = field(default_factory=list)
    fetched_at: str | None = None
    source: str = "ietf"
    source_id: str = ""
    source_url: str = ""
    doc_status: str = ""

    @property
    def text_url(self) -> str:
        return f"https://www.ietf.org/archive/id/{self.name}-{self.rev}.txt"

    @property
    def datatracker_url(self) -> str:
        return f"https://datatracker.ietf.org/doc/{self.name}/"

    @property
    def date(self) -> str:
        """Return just the date portion of time."""
        if self.time:
            return self.time[:10]
        return ""


@dataclass
class Rating:
    draft_name: str
    novelty: int  # 1-5
    maturity: int  # 1-5
    overlap: int  # 1-5 (5 = highly overlapping with others)
    momentum: int  # 1-5
    relevance: int  # 1-5
    summary: str  # 2-4 sentence AI summary
    novelty_note: str = ""
    maturity_note: str = ""
    overlap_note: str = ""
    momentum_note: str = ""
    relevance_note: str = ""
    categories: list[str] = field(default_factory=list)
    rated_at: str | None = None

    @property
    def composite_score(self) -> float:
        """Weighted composite: novelty and relevance matter most."""
        return (
            self.novelty * 0.30
            + self.relevance * 0.25
            + self.maturity * 0.20
            + self.momentum * 0.15
            + (6 - self.overlap) * 0.10  # Invert: less overlap = better
        )
