from __future__ import annotations

from app.schemas.analyze import AnalyzeItem
from app.services.analyze.base import AnalyzerContribution, AnalyzeContext


class DomainFactsAnalyzer:
    key = "domain_facts"
    ttl_seconds = 60 * 60 * 12  # 12h (RDAP/WHOIS changes slowly)

    async def analyze(self, ctx: AnalyzeContext) -> list[AnalyzerContribution]:
        check = ctx.check
        items = [
            AnalyzeItem(
                key="availability",
                label="Availability",
                value="available" if check.is_available else "taken",
                status="pass" if check.is_available else "warn",
                source=str(check.check_method or "internal"),
                details={"status": str(check.status.value)},
            ),
            AnalyzeItem(
                key="created_at",
                label="Creation Date",
                value=check.creation_date.isoformat() if check.creation_date else None,
                status="pass" if check.creation_date else "na",
                source=str(check.check_method or "internal"),
                details={"reason": None if check.creation_date else "Not provided by RDAP/WHOIS for this TLD."},
            ),
            AnalyzeItem(
                key="expires_at",
                label="Expiry Date",
                value=check.expiration_date.isoformat() if check.expiration_date else None,
                status="pass" if check.expiration_date else "na",
                source=str(check.check_method or "internal"),
                details={"reason": None if check.expiration_date else "Not provided by RDAP/WHOIS for this TLD."},
            ),
            AnalyzeItem(
                key="registrar",
                label="Registrar",
                value=check.registrar,
                status="info" if check.registrar else "na",
                source=str(check.check_method or "internal"),
                details={},
            ),
            AnalyzeItem(
                key="nameservers",
                label="Nameservers",
                value=check.name_servers or [],
                status="info" if check.name_servers else "na",
                source="dns",
                details={},
            ),
        ]
        return [AnalyzerContribution(quadrant="authority", items=items)]

