"""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

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()
