"""Celery Tasks for background processing"""
import asyncio
from celery import shared_task

from app.workers.celery_app import celery_app
from app.services import (
    image_generator,
    image_upscaler,
    background_remover,
    video_generator,
    video_upscaler,
    subtitle_processor,
    voice_to_text,
    text_to_speech,
    alt_text_generator
)


def run_async(coro):
    """Helper to run async functions in sync context"""
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    try:
        return loop.run_until_complete(coro)
    finally:
        loop.close()


@celery_app.task(bind=True, name="process_image_generation")
def process_image_generation(self, job_id: str):
    """Process image generation job"""
    try:
        run_async(image_generator.generate(job_id))
    except Exception as e:
        self.retry(exc=e, countdown=60, max_retries=2)


@celery_app.task(bind=True, name="process_image_upscaling")
def process_image_upscaling(self, job_id: str):
    """Process image upscaling job"""
    try:
        run_async(image_upscaler.upscale(job_id))
    except Exception as e:
        self.retry(exc=e, countdown=60, max_retries=2)


@celery_app.task(bind=True, name="process_background_removal")
def process_background_removal(self, job_id: str):
    """Process background removal job"""
    try:
        run_async(background_remover.remove_background(job_id))
    except Exception as e:
        self.retry(exc=e, countdown=60, max_retries=2)


@celery_app.task(bind=True, name="process_video_generation")
def process_video_generation(self, job_id: str):
    """Process video generation job"""
    try:
        run_async(video_generator.generate(job_id))
    except Exception as e:
        self.retry(exc=e, countdown=120, max_retries=2)


@celery_app.task(bind=True, name="process_video_upscaling")
def process_video_upscaling(self, job_id: str):
    """Process video upscaling job"""
    try:
        run_async(video_upscaler.upscale(job_id))
    except Exception as e:
        self.retry(exc=e, countdown=120, max_retries=2)


@celery_app.task(bind=True, name="process_subtitles")
def process_subtitles(self, job_id: str):
    """Process subtitle generation job"""
    try:
        run_async(subtitle_processor.process(job_id))
    except Exception as e:
        self.retry(exc=e, countdown=60, max_retries=2)


@celery_app.task(bind=True, name="process_voice_to_text")
def process_voice_to_text(self, job_id: str):
    """Process voice to text transcription job"""
    try:
        run_async(voice_to_text.transcribe(job_id))
    except Exception as e:
        self.retry(exc=e, countdown=60, max_retries=2)


@celery_app.task(bind=True, name="process_text_to_speech")
def process_text_to_speech(self, job_id: str):
    """Process text to speech synthesis job"""
    try:
        run_async(text_to_speech.synthesize(job_id))
    except Exception as e:
        self.retry(exc=e, countdown=60, max_retries=2)


@celery_app.task(bind=True, name="process_speech_to_speech")
def process_speech_to_speech(self, job_id: str):
    """Process speech to speech conversion job"""
    try:
        run_async(text_to_speech.speech_to_speech(job_id))
    except Exception as e:
        self.retry(exc=e, countdown=60, max_retries=2)


@celery_app.task(bind=True, name="process_alt_text")
def process_alt_text(self, job_id: str):
    """Process alt text generation job"""
    try:
        run_async(alt_text_generator.generate(job_id))
    except Exception as e:
        self.retry(exc=e, countdown=60, max_retries=2)
