"""
Rutas de importación CSV para Exportación (encabezado y partidas).
Flujo: upload → scan → status (polling) → commit. Sin validaciones ni inserción aún.
"""
import base64
import json
import logging
import os
from uuid import uuid4

from fastapi import APIRouter, File, HTTPException, UploadFile, Depends, Form, Query
from sqlalchemy.orm import Session
from typing import Literal, Optional, Dict, Any

from core.celery_app import celery_app
from core.database import get_core_db
from core.paths import layout_path
from core.security import get_current_user, validate_access_to_resource

from .schemas import ImportJobResponse, CommitRequest
from .tasks import scan_file, insert_valid_rows, JOB_TYPE, EXP_IMPORT_REDIS_TTL
from ..common import storage as common_storage

router = APIRouter()
logger = logging.getLogger(__name__)


def _get_redis():
    import redis
    url = os.getenv("VALKEY_URL", os.getenv("REDIS_URL", "redis://valkey:6379/0"))
    return redis.Redis.from_url(url, decode_responses=False)


@router.post("/upload/{model_target}", response_model=ImportJobResponse)
async def upload_import_file(
    model_target: Literal["invoice_header", "invoice_details", "invoice_series"],
    file: UploadFile = File(...),
    footer_config: Optional[str] = Form(None),
    template_id: Optional[str] = Form(None),
    company_id: int = Query(..., description="Company ID"),
    operation_type: Optional[str] = Query("exp"),
    db: Session = Depends(get_core_db),
    current_user: Dict[str, Any] = Depends(get_current_user),
):
    """Subir CSV, guardar en Redis, encolar scan. operation_type=exp para exportación."""
    try:
        tenant_id = validate_access_to_resource(db, company_id, current_user)
    except Exception as e:
        logger.error("Exportación import: access validation failed: %s", e)
        raise HTTPException(status_code=403, detail="Invalid company access")

    if not file.filename or not file.filename.lower().endswith(".csv"):
        raise HTTPException(status_code=400, detail="Solo se permiten archivos .csv")

    job_id = str(uuid4())
    contents = await file.read()

    file_key, meta_key, _ = common_storage.storage_keys(JOB_TYPE, job_id)
    default_template = (
        "exp_def_header" if model_target == "invoice_header"
        else "exp_def_series" if model_target == "invoice_series"
        else "exp_def_partidas"
    )
    meta_data = {
        "tenant_id": tenant_id,
        "company_id": company_id,
        "user_id": current_user.get("id"),
        "capture_user": (
            current_user.get("preferred_username")
            or current_user.get("email")
            or current_user.get("sub")
            or "CSV"
        ),
        "footer_config": footer_config,
        "operation_type": operation_type or "exp",
        "template_id": template_id or default_template,
    }

    try:
        r = _get_redis()
        r.set(file_key, base64.b64encode(contents), ex=EXP_IMPORT_REDIS_TTL)
        r.set(meta_key, json.dumps(meta_data).encode("utf-8"), ex=EXP_IMPORT_REDIS_TTL)
    except Exception as e:
        logger.error("Exportación import: Redis store error: %s", e)
        raise HTTPException(status_code=500, detail="No se pudo encolar el archivo.")

    try:
        upload_dir = layout_path("imports", "temp")
        os.makedirs(upload_dir, exist_ok=True)
        csv_path = common_storage.file_path_for_job(JOB_TYPE, job_id)
        with open(csv_path, "wb") as f:
            f.write(contents)
        meta_path = csv_path.replace(".csv", ".meta.json")
        with open(meta_path, "w") as f:
            json.dump(meta_data, f)
    except Exception as e:
        logger.warning("Exportación import: local file save failed: %s", e)

    scan_file.apply_async(args=[job_id, model_target, footer_config], task_id=job_id)

    return ImportJobResponse(
        job_id=job_id,
        status="queued",
        message="Archivo subido. Escaneo iniciado.",
    )


@router.get("/{job_id}/status")
async def get_import_status(job_id: str):
    """Polling: estado del escaneo o del commit."""
    task_result = celery_app.AsyncResult(job_id)

    if task_result.state == "PENDING":
        return {"status": "processing", "progress": 0}
    if task_result.state == "PROGRESS":
        info = task_result.info or {}
        return {
            "status": "processing",
            "progress": info.get("current", 0),
            "total": info.get("total", 0),
        }
    if task_result.state == "SUCCESS":
        result = task_result.result
        if isinstance(result, dict) and "status" in result:
            return result
        return {"status": "finished", "result": result}

    if isinstance(getattr(task_result, "result", None), dict) and task_result.result.get("status") in ("finished", "warning"):
        return task_result.result

    logger.warning("Exportación import task %s failed: state=%s", job_id, task_result.state)
    err_msg = None
    tb = getattr(task_result, "traceback", None)
    if tb and isinstance(tb, str):
        lines = [l.strip() for l in tb.strip().split("\n") if l.strip()]
        if lines:
            err_msg = lines[-1]
    if not err_msg:
        try:
            exc = task_result.get(propagate=False)
            if exc is not None:
                err_msg = str(exc)
        except Exception:
            pass
    if not err_msg:
        result = getattr(task_result, "result", None)
        if result is not None and not isinstance(result, dict):
            err_msg = str(result)
        elif isinstance(result, dict) and (result.get("error") or result.get("message")):
            err_msg = result.get("error") or result.get("message")
    return {"status": "failed", "error": err_msg or "Task failed"}


@router.post("/{job_id}/commit")
async def commit_import_job(job_id: str, body: CommitRequest):
    """Usuario confirma; se encola la tarea de commit (por ahora sin inserción real)."""
    task = insert_valid_rows.delay(job_id, body.model_target)
    return {
        "status": "committing",
        "message": "Proceso de commit iniciado.",
        "commit_job_id": task.id,
    }
