from __future__ import annotations

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


class TldMatrixAnalyzer:
    key = "tld_matrix"
    ttl_seconds = 60 * 30  # 30m (availability can change)

    async def analyze(self, ctx: AnalyzeContext) -> list[AnalyzerContribution]:
        # If main domain check says it's taken, pass that info to TLD matrix
        # This ensures the original TLD shows correctly as "taken" even if
        # DNS-based checks fail (e.g., domain registered but no DNS records)
        original_is_taken = ctx.check and not ctx.check.is_available
        
        rows = await run_tld_matrix(ctx.domain, original_is_taken=original_is_taken)
        item = AnalyzeItem(
            key="tld_matrix",
            label="TLD Matrix",
            value=[row.__dict__ for row in rows],
            status="info",
            source="dns",
            details={"tlds": [r.tld for r in rows]},
        )
        return [AnalyzerContribution(quadrant="market", items=[item])]

