"""
Helpers para construir respuestas de scan y commit (formato unificado).
"""
from typing import Dict, Any, List, Optional


ERRORS_PREVIEW_LIMIT = 100
SUMMARY_LIMIT = 10


def _summarize_reasons(items: List[Dict[str, Any]], key_name: str) -> List[Dict[str, Any]]:
    counts: Dict[str, int] = {}
    for item in items:
        reason = str(item.get(key_name, "")).strip()
        if not reason:
            continue
        counts[reason] = counts.get(reason, 0) + 1
    ordered = sorted(counts.items(), key=lambda kv: kv[1], reverse=True)
    return [{"reason": reason, "count": count} for reason, count in ordered[:SUMMARY_LIMIT]]


def scan_result(
    job_id: str,
    processed_rows: int,
    error_count: int,
    errors_detail: List[Dict[str, Any]],
    total_rows_in_file: Optional[int] = None,
    message: Optional[str] = None,
) -> Dict[str, Any]:
    """Respuesta de scan_file (waiting_confirmation).
    Si total_rows_in_file no se pasa, se usa processed_rows como total (comportamiento anterior).
    """
    total = total_rows_in_file if total_rows_in_file is not None else processed_rows
    out: Dict[str, Any] = {
        "status": "waiting_confirmation",
        "job_id": job_id,
        "total_rows": total,
        "error_count": error_count,
        "valid_rows": processed_rows - error_count,
        # Para no saturar el front: devolvemos solo un preview.
        # El detalle completo se descarga desde CSV usando el job_id.
        "errors": errors_detail[:ERRORS_PREVIEW_LIMIT],
        "error_summary": _summarize_reasons([e for e in errors_detail if not e.get("warning")], "msg"),
    }
    if message:
        out["message"] = message
    return out


def commit_result(
    status: str,
    inserted: int,
    skipped_invalid: int,
    skipped_missing_fk: int,
    skipped_duplicate: int,
    skipped_details: List[Dict[str, Any]],
    message: Optional[str] = None,
    error: Optional[str] = None,
) -> Dict[str, Any]:
    """Respuesta de insert_valid_rows (finished / warning / failed)."""
    out = {
        "status": status,
        "inserted": inserted,
        "skipped_invalid": skipped_invalid,
        "skipped_missing_fk": skipped_missing_fk,
        "skipped_duplicate": skipped_duplicate,
        "skipped_details": skipped_details,
        "skipped_summary": _summarize_reasons(skipped_details, "reason"),
    }
    if message:
        out["message"] = message
    if error:
        out["error"] = error
    return out
