"""
Tareas Celery para importación CSV de Agentes Aduanales.
Flujo: scan_file (validación) → insert_valid_rows (commit).
Usa layouts_csv.common (storage, normalize, meta, responses, csv_reader).
"""
import json
import logging
import os
from typing import Dict, Any, Optional, List, Set

from core.celery_app import celery_app
from core.database import CoreSessionLocal

from ..common import storage as common_storage
from ..common import normalize as common_normalize
from ..common import meta as common_meta
from ..common import responses as common_responses
from ..common import csv_reader as common_csv_reader
from .template_config import (
    row_from_template,
    CUSTOMS_BROKERS_FIELDNAMES_ORDER,
    CUSTOMS_BROKERS_HEADERLESS_FIRST_CELL,
)
from .validators import validate_row_customs_broker
from .common.mappers import row_to_customs_broker_data
from .common.fk_loader import load_customs_brokers_fk_sets

logger = logging.getLogger(__name__)

JOB_TYPE = "cb"

# Para routes.py
CB_IMPORT_FILE_PREFIX = "cb_import_file:"
CB_IMPORT_META_PREFIX = "cb_import_meta:"
CB_IMPORT_ERROR_LINES_PREFIX = "cb_import_error_lines:"
CB_IMPORT_REDIS_TTL = common_storage.IMPORT_REDIS_TTL


def _do_scan(job_id: str, progress_callback: Optional[Any] = None) -> Dict[str, Any]:
    file_path = common_storage.ensure_file_from_redis(JOB_TYPE, job_id, "CB import")
    if not file_path:
        return {"status": "failed", "error": "Archivo no encontrado (expirado o no subido). Sube de nuevo."}
    common_storage.ensure_meta_from_redis(JOB_TYPE, job_id, file_path, "CB import")

    error_path = common_storage.error_path_for_job(JOB_TYPE, job_id)

    try:
        total_rows = common_csv_reader.count_csv_rows(file_path)
    except Exception as e:
        return {"status": "failed", "error": str(e)}

    try:
        tenant_id, company_id = common_meta.require_tenant_context(file_path)
    except ValueError as e:
        return {"status": "failed", "error": str(e)}

    meta = common_meta.load_meta(file_path) or {}
    actualizar = meta.get("actualizar", False)
    existing_broker_keys: Set[str] = set()
    if actualizar:
        try:
            from api.v1.modules.a76.customs_brokers.models import CustomsBroker
            with CoreSessionLocal() as session:
                for b in (
                    session.query(CustomsBroker)
                    .filter(
                        CustomsBroker.tenant_id == tenant_id,
                        CustomsBroker.company_id == company_id,
                    )
                    .all()
                ):
                    if (b.broker_key or "").strip():
                        existing_broker_keys.add((b.broker_key or "").strip())
        except Exception as e:
            logger.warning("CB import: could not load existing broker_keys for ACT: %s", e)

    valid_country_m3 = load_customs_brokers_fk_sets()

    error_count = 0
    processed_rows = 0
    errors_detail: List[Dict[str, Any]] = []
    error_lines_list: List[int] = []

    try:
        with open(error_path, "w", encoding="utf-8") as f_err:
            for i, row in common_csv_reader.iter_csv_rows(
                file_path,
                fieldnames=CUSTOMS_BROKERS_FIELDNAMES_ORDER,
                headerless_first_cell_values=CUSTOMS_BROKERS_HEADERLESS_FIRST_CELL,
            ):
                if progress_callback:
                    progress_callback(i, total_rows, error_count)

                row_norm = row_from_template(row, common_normalize.normalize_header)
                row_errors = validate_row_customs_broker(
                    row_norm,
                    i,
                    actualizar=actualizar,
                    existing_broker_keys=existing_broker_keys,
                    valid_country_m3=valid_country_m3,
                )
                if row_errors:
                    has_blocking_error = any(not e.get("warning", False) for e in row_errors)
                    if has_blocking_error:
                        error_count += 1
                        # Guardar una sola vez por renglón (aunque haya múltiples errores en la misma línea).
                        error_lines_list.append(i)
                        for e in row_errors:
                            if not e.get("warning", False):
                                f_err.write(json.dumps(e) + "\n")
                    if len(errors_detail) < 500:
                        for e in row_errors:
                            if len(errors_detail) >= 500:
                                break
                            errors_detail.append({
                                "line": e["line"],
                                "col": e.get("col", ""),
                                "msg": e.get("msg", ""),
                                "solution": e.get("solution", ""),
                                "warning": bool(e.get("warning", False)),
                            })
                processed_rows += 1

        if error_lines_list:
            common_storage.store_error_lines(JOB_TYPE, job_id, error_lines_list)
    except Exception as e:
        logger.error("CB import scan failed: %s", e)
        return {"status": "failed", "error": str(e)}

    return common_responses.scan_result(job_id, processed_rows, error_count, errors_detail)


@celery_app.task(bind=True)
def scan_file(self, job_id: str, config: str = None):
    logger.info("CB import: starting scan for job %s", job_id)

    def on_progress(current: int, total: int, errors: int) -> None:
        self.update_state(state="PROGRESS", meta={"current": current, "total": total, "errors": errors})

    return _do_scan(job_id, progress_callback=on_progress)


def _do_commit(job_id: str) -> Dict[str, Any]:
    file_path = common_storage.ensure_file_from_redis(JOB_TYPE, job_id, "CB import")
    if not file_path:
        alt_path = common_storage.file_path_for_job(JOB_TYPE, job_id)
        if not os.path.exists(alt_path):
            return {"status": "failed", "error": "Archivo no encontrado (expirado). Sube y confirma de nuevo."}
        file_path = alt_path
    else:
        common_storage.ensure_meta_from_redis(JOB_TYPE, job_id, file_path, "CB import")

    error_path = common_storage.error_path_for_job(JOB_TYPE, job_id)
    error_lines = common_storage.get_error_lines(JOB_TYPE, job_id, error_path)

    try:
        tenant_id, company_id = common_meta.require_tenant_context(file_path)
    except ValueError as e:
        return {"status": "failed", "error": str(e)}

    meta = common_meta.load_meta(file_path) or {}
    actualizar = meta.get("actualizar", False)
    valid_country_m3 = load_customs_brokers_fk_sets()

    from api.v1.modules.a76.customs_brokers.models import CustomsBroker

    inserted_count = 0
    skipped_invalid = 0
    skipped_details: List[Dict[str, Any]] = []
    meta_path = common_meta.get_meta_path(file_path)

    try:
        with CoreSessionLocal() as session:
            existing_by_key: Dict[str, CustomsBroker] = {}
            for b in (
                session.query(CustomsBroker)
                .filter(
                    CustomsBroker.tenant_id == tenant_id,
                    CustomsBroker.company_id == company_id,
                )
                .all()
            ):
                existing_by_key[b.broker_key] = b

            existing_broker_keys = set(existing_by_key.keys())

            for i, row in common_csv_reader.iter_csv_rows(
                file_path,
                fieldnames=CUSTOMS_BROKERS_FIELDNAMES_ORDER,
                headerless_first_cell_values=CUSTOMS_BROKERS_HEADERLESS_FIRST_CELL,
            ):
                if i in error_lines:
                    continue

                row_norm = row_from_template(row, common_normalize.normalize_header)
                row_errors = validate_row_customs_broker(
                    row_norm,
                    i,
                    actualizar=actualizar,
                    existing_broker_keys=existing_broker_keys,
                    valid_country_m3=valid_country_m3,
                )
                if row_errors and any(not e.get("warning", False) for e in row_errors):
                    skipped_invalid += 1
                    blocking_errors = [e for e in row_errors if not e.get("warning", False)]
                    for blocking in blocking_errors:
                        skipped_details.append({
                            "line": i,
                            "reason": f"{blocking.get('col', '')}: {blocking.get('msg', '')}",
                            "solution": blocking.get("solution", ""),
                        })
                    continue

                data = row_to_customs_broker_data(row_norm, tenant_id, company_id)
                if not data or not data.get("broker_key"):
                    skipped_invalid += 1
                    continue

                clave = data["broker_key"]
                existing = existing_by_key.get(clave)
                if existing:
                    for k, v in data.items():
                        if k not in ("tenant_id", "company_id", "broker_key"):
                            setattr(existing, k, v)
                    session.add(existing)
                else:
                    new_broker = CustomsBroker(**data)
                    session.add(new_broker)
                    existing_by_key[clave] = new_broker
                inserted_count += 1

            try:
                session.commit()
            except Exception as db_err:
                session.rollback()
                logger.error("CB import DB error: %s", db_err)
                return {"status": "failed", "error": str(db_err)}

    except Exception as e:
        logger.exception("CB import task failed")
        return {"status": "failed", "error": str(e)}

    common_storage.cleanup_import_job(
        JOB_TYPE, job_id,
        file_path=file_path,
        error_path=error_path,
        meta_path=meta_path,
    )

    if inserted_count == 0 and skipped_invalid > 0:
        return {
            "status": "warning",
            "inserted": 0,
            "updated": 0,
            "skipped_invalid": skipped_invalid,
            "skipped_duplicate": 0,
            "skipped_missing_fk": 0,
            "skipped_details": skipped_details,
            "message": f"No se insertaron registros. {skipped_invalid} rechazados.",
        }
    if inserted_count == 0:
        return {
            "status": "failed",
            "error": "No hay registros válidos en el archivo CSV",
            "inserted": 0,
            "updated": 0,
            "skipped_invalid": skipped_invalid,
            "skipped_duplicate": 0,
            "skipped_missing_fk": 0,
            "skipped_details": skipped_details,
        }
    return {
        "status": "finished",
        "inserted": inserted_count,
        "updated": 0,
        "skipped_invalid": skipped_invalid,
        "skipped_duplicate": 0,
        "skipped_missing_fk": 0,
        "skipped_details": skipped_details,
    }


@celery_app.task(bind=True)
def insert_valid_rows(self, job_id: str):
    logger.info("CB import: starting commit for job %s", job_id)
    return _do_commit(job_id)
