"""Celery tasks for data collection"""

import asyncio
from datetime import datetime
from typing import Dict, Any

from app.db.session import async_session_factory
from app.services.collectors.registry import collector_registry


async def run_collector_task(collector_name: str) -> Dict[str, Any]:
    """Run a single collector task"""
    from sqlalchemy import select
    from app.models.datasource import DataSource

    collector = collector_registry.get(collector_name)
    if not collector:
        return {"status": "failed", "error": f"Collector {collector_name} not found"}

    if not collector_registry.is_active(collector_name):
        return {"status": "skipped", "reason": "Collector is disabled"}

    async with async_session_factory() as db:
        result = await db.execute(
            select(DataSource.id).where(DataSource.collector_class == collector_name)
        )
        datasource = result.scalar_one_or_none()

        if datasource:
            collector._datasource_id = datasource

        result = await collector.run(db)
        return result


def run_collector_sync(collector_name: str) -> Dict[str, Any]:
    """Synchronous wrapper for running collectors"""
    return asyncio.run(run_collector_task(collector_name))
