import sentry_sdk
from sentry_sdk.integrations.celery import CeleryIntegration

from app.core.config import settings
from app.core.logging import setup_logging, get_logger
from app.tasks import celery_app

# Set up logging first
setup_logging()
logger = get_logger(__name__)

# Initialize Sentry for worker
if settings.sentry_dsn and settings.sentry_dsn.startswith(('http', 'https')):
    sentry_sdk.init(
        dsn=settings.sentry_dsn,
        integrations=[CeleryIntegration(monitor_beat_tasks=True)],
        environment=settings.app_env,
        release="1.0.0",
        send_default_pii=False,
    )

logger.info("Starting Celery worker with structured logging")

# Import task modules to register them
from app.tasks import ingest_and_ai
from app.tasks import translate_and_synthesize

# Debug: Show registered tasks
logger.info(f"Celery app: {celery_app}")
logger.info(f"Registered tasks: {list(celery_app.tasks.keys())}")
logger.info(f"Task routes: {celery_app.conf.task_routes}")
logger.info(f"Worker listening to queues: default,ingest")

# Specifically check for our translation task
if 'app.tasks.translate_and_synthesize.translate_and_synthesize_task' in celery_app.tasks:
    logger.info("✅ translate_and_synthesize_task is registered")
else:
    logger.error("❌ translate_and_synthesize_task is NOT registered")
    logger.error(f"Available tasks: {[t for t in celery_app.tasks.keys() if not t.startswith('celery.')]}")

if __name__ == "__main__":
    celery_app.start()
