"""Celery task definitions."""

import logging

from celery_app import celery_app
from database import SessionLocal
from services.media_task_runner import run_parse_media_task
from services.propagation_task_runner import run_propagate_project_task

logger = logging.getLogger(__name__)


@celery_app.task(name="media.parse_project")
def parse_project_media(task_id: int) -> dict:
    """Run media parsing for one queued task."""
    db = SessionLocal()
    try:
        return run_parse_media_task(db, task_id)
    except Exception as exc:  # noqa: BLE001
        logger.exception("Parse media task failed: task_id=%s", task_id)
        raise exc
    finally:
        db.close()


@celery_app.task(name="ai.propagate_project")
def propagate_project_masks(task_id: int) -> dict:
    """Run SAM video propagation for one queued task."""
    db = SessionLocal()
    try:
        return run_propagate_project_task(db, task_id)
    except Exception as exc:  # noqa: BLE001
        logger.exception("Propagation task failed: task_id=%s", task_id)
        raise exc
    finally:
        db.close()
