from fastapi import HTTPException
import base64
import logging
from core.celery_app import celery_app
from core.database import CoreSessionLocal 

from .service import AvisoConsolidadoExportacionService

logger = logging.getLogger(__name__)

@celery_app.task(name="generar_pdf_aviso_consolidado_exp_async", bind=True)
def generar_pdf_aviso_consolidado_exp_async(self, invoice_id: int, company_id: int):
    # 1. Abrimos conexión a la DB
    db = CoreSessionLocal()
    try:
        logger.info(f"Worker procesando Aviso Consolidado Exp {invoice_id}...")
        
        # 2. Instanciamos el servicio
        service = AvisoConsolidadoExportacionService()

        # Update state to PROCESSING
        self.update_state(state='PROCESSING', meta={'current': 5, 'total': 100, 'status': 'Iniciando generación...'})
        
        def progress_callback(progress: int, status: str):
            self.update_state(state='PROCESSING', meta={'current': progress, 'total': 100, 'status': status})

        # 3. Generamos los bytes del PDF
        pdf_bytes, nombre, media_type = service.generar_pdf(
            db=db, 
            invoice_id=invoice_id, 
            company_id=company_id,
            progress_callback=progress_callback
        )
        
        # 4. Codificamos a base64
        pdf_base64 = base64.b64encode(pdf_bytes).decode('utf-8')
        
        return {
            "status": "success",
            "file_name": nombre,
            "content": pdf_base64,
            "media_type": media_type
        }

    except Exception as e:
        logger.error(f"Error en Celery Worker Aviso Consolidado Exp: {str(e)}")
        return {"status": "error", "message": str(e)}
    
    finally:
        db.close()