"""Analyze API endpoints (Alpha Terminal - Diligence)."""

from __future__ import annotations

from fastapi import APIRouter, Query, Request
from slowapi import Limiter
from slowapi.util import get_remote_address

from app.api.deps import CurrentUser, Database
from app.schemas.analyze import AnalyzeResponse
from app.services.analyze.service import get_domain_analysis

router = APIRouter()
limiter = Limiter(key_func=get_remote_address)


@router.get("/{domain}", response_model=AnalyzeResponse)
@limiter.limit("60/minute")
async def analyze_domain(
    request: Request,
    domain: str,
    current_user: CurrentUser,
    db: Database,
    fast: bool = Query(False, description="Skip slower HTTP/SSL checks"),
    refresh: bool = Query(False, description="Bypass cache and recompute"),
):
    """
    Analyze a domain with open-data-first signals.

    Requires authentication (Terminal feature).
    """
    _ = current_user  # enforce auth
    res = await get_domain_analysis(db, domain, fast=fast, refresh=refresh)
    await db.commit()  # persist cache upsert
    return res

