import argparse
import json
from pathlib import Path
import os
import sys
from dotenv import load_dotenv

# Video pipeline imports

from celery import chain, group, chord
from celery_app import celery


load_dotenv()  # load env variables

@celery.task(name="video_main.merge")
def merge(results):
    """Merge results from previous tasks."""
    pdf_result, audio_result = results
    pdf_path = pdf_result.get("pdf_path")
    audio_mapping_file = audio_result.get("audio_mapping_file")
    session_id = pdf_result.get("id") or audio_result.get("id")
    if not session_id:
        raise ValueError("Both ppt_result and audio_result must include 'id' key for session identification")
    if not pdf_path or not audio_mapping_file:
        raise ValueError("ppt_result must include 'ppt_path' and audio_result must include 'audio_mapping_file'")
    
    print(f"📁 PPT Path: {pdf_path}")
    print(f"📁 Audio Mapping File: {audio_mapping_file}")
    return {"pdf_path": pdf_path, "audio_mapping_file": audio_mapping_file, "id": session_id}


# pipeline celery code

def run_full_pipeline(pdf_path, avatar_path, session_id, voice_id= None):
    """Run the full pipeline from PDF to final video, but route everything to the
    'video_serial' queue so jobs execute one-by-one when the worker is started
    with concurrency=1 and listening on that queue.
    """
    from Video_generation.Video_sync import pdf_audio_map_to_video  # final step
    from Video_generation.ppt_pro import create_ppt_from_data
    from Video_generation.audio_conversion import generate_audio_from_presentation
    from Video_generation.Content_creator import generate_presentation_json, load_text_from_pdf

    # prepare initial data (same as before)
    text = load_text_from_pdf(pdf_path)
    data = {
        "id": session_id,
        "text": text,
        "pdf_path": pdf_path,
    }

    # name of the dedicated serial queue
    SERIAL_QUEUE = "video_serial"

    # Create subtasks and pin them to the same queue
    gen_json_task = generate_presentation_json.s(data).set(queue=SERIAL_QUEUE)
    ppt_task = create_ppt_from_data.s().set(queue=SERIAL_QUEUE)
    audio_task = generate_audio_from_presentation.s(voice_id=voice_id).set(queue=SERIAL_QUEUE)
    merge_task = merge.s().set(queue=SERIAL_QUEUE)
    video_task = pdf_audio_map_to_video.s(avatar_path=avatar_path).set(queue=SERIAL_QUEUE)

    # Chain: generate_json -> (ppt + audio) as chord -> merge -> video
    work_chain = chain(
        gen_json_task,
        chord([ppt_task, audio_task], merge_task),
        video_task
    ).apply_async()  # dispatch to broker immediately

    # return AsyncResult to caller (still non-blocking)
    return work_chain
