"""ARQ worker task: parse a master deck PPTX."""
import traceback
import uuid
from datetime import datetime

from models.sql.job import JobModel
from services.database import async_session_maker
from services.master_deck_parser_service import parse_master_deck
from services.redis_service import publish_job_progress


async def parse_master_deck_task(ctx: dict, job_id: str, deck_id: str = "") -> None:
    """ARQ task: parse a master deck via the existing parser service."""
    job_uuid = uuid.UUID(job_id)

    async with async_session_maker() as session:
        job = await session.get(JobModel, job_uuid)
        if not job:
            return

        try:
            job.status = "processing"
            job.started_at = datetime.utcnow()
            job.progress = 10
            job.progress_message = "Parsing master deck"
            await session.commit()

            try:
                await publish_job_progress(job_uuid, 10, "Parsing master deck")
            except Exception:
                pass

            # Parse the master deck by its own ID
            await parse_master_deck(uuid.UUID(deck_id))

            job.status = "completed"
            job.progress = 100
            job.progress_message = "Parsing complete"
            job.completed_at = datetime.utcnow()
            await session.commit()

            try:
                await publish_job_progress(job_uuid, 100, "Parsing complete", "completed")
            except Exception:
                pass

        except Exception as e:
            traceback.print_exc()
            job.status = "failed"
            job.error_message = str(e)[:500]
            job.progress_message = "Parsing failed"
            job.completed_at = datetime.utcnow()
            await session.commit()

            try:
                await publish_job_progress(job_uuid, job.progress, "Parsing failed", "failed")
            except Exception:
                pass
