from __future__ import annotations

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


class TldPricingAnalyzer:
    key = "tld_pricing"
    ttl_seconds = 60 * 60 * 6  # 6h (DB updates periodically)

    async def analyze(self, ctx: AnalyzeContext) -> list[AnalyzerContribution]:
        tld = ctx.domain.split(".")[-1].lower()
        snap = await get_tld_price_snapshot(ctx.db, tld)

        market_items = [
            AnalyzeItem(
                key="tld_cheapest_register_usd",
                label="Cheapest registration (USD)",
                value=snap.min_register_usd,
                status="info" if snap.min_register_usd is not None else "na",
                source="db",
                details={
                    "registrar": snap.min_register_registrar,
                    "latest_recorded_at": snap.latest_recorded_at.isoformat() if snap.latest_recorded_at else None,
                    "reason": None if snap.min_register_usd is not None else "No TLD price data collected yet.",
                },
            ),
            AnalyzeItem(
                key="tld_cheapest_renew_usd",
                label="Cheapest renewal (USD)",
                value=snap.min_renew_usd,
                status="info" if snap.min_renew_usd is not None else "na",
                source="db",
                details={
                    "registrar": snap.min_renew_registrar,
                    "latest_recorded_at": snap.latest_recorded_at.isoformat() if snap.latest_recorded_at else None,
                    "reason": None if snap.min_renew_usd is not None else "No TLD price data collected yet.",
                },
            ),
            AnalyzeItem(
                key="tld_cheapest_transfer_usd",
                label="Cheapest transfer (USD)",
                value=snap.min_transfer_usd,
                status="info" if snap.min_transfer_usd is not None else "na",
                source="db",
                details={
                    "registrar": snap.min_transfer_registrar,
                    "latest_recorded_at": snap.latest_recorded_at.isoformat() if snap.latest_recorded_at else None,
                    "reason": None if snap.min_transfer_usd is not None else "No TLD price data collected yet.",
                },
            ),
        ]

        value_items = [
            AnalyzeItem(
                key="renewal_burn_usd_per_year",
                label="Renewal burn (USD/year)",
                value=snap.min_renew_usd,
                status="info" if snap.min_renew_usd is not None else "na",
                source="db",
                details={
                    "assumption": "Cheapest renewal among tracked registrars (your DB).",
                    "registrar": snap.min_renew_registrar,
                },
            )
        ]

        return [
            AnalyzerContribution(quadrant="market", items=market_items),
            AnalyzerContribution(quadrant="value", items=value_items),
        ]

