from fastapi import FastAPI, HTTPException, Request
from fastapi.staticfiles import StaticFiles
from concurrent.futures import ThreadPoolExecutor
import asyncio
import aiohttp
import tempfile
import uuid
import shutil
import os
import random
import traceback
import string

app = FastAPI()

def generate_hash(length=12):
    # Characters that can appear in the hash
    characters = string.ascii_lowercase + string.digits
    # Generate a random string of the specified length
    hash_string = ''.join(random.choice(characters) for _ in range(length))
    return hash_string

@app.get("/")
async def read_root():
    return {"message": "Saqib's API"}

# Create a directory to store MP3 files if it doesn't exist
AUDIO_DIR = "audio_files"
os.makedirs(AUDIO_DIR, exist_ok=True)

# Create a directory for storing output files
OUTPUT_DIR = "output"
os.makedirs(OUTPUT_DIR, exist_ok=True)

# Mount the audio directory
app.mount("/audio", StaticFiles(directory=AUDIO_DIR), name="audio")

# Mount the output directory
app.mount("/output", StaticFiles(directory=OUTPUT_DIR), name="output")

thread_pool = ThreadPoolExecutor(max_workers=2)

async def run_ffmpeg_async(ffmpeg_command):
    loop = asyncio.get_running_loop()
    await loop.run_in_executor(thread_pool, ffmpeg_command)

async def download_file(url: str, suffix: str):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            if response.status != 200:
                raise HTTPException(status_code=400, detail=f"Failed to download file from {url}")
            with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
                temp_file.write(await response.read())
                return temp_file.name


@app.post("/add_audio_to_image")
async def add_audio_to_image(request: Request):
    try:
        # Generate a unique filename
        output_filename = f"{uuid.uuid4()}.mp4"
        output_path = os.path.join(OUTPUT_DIR, output_filename)

        # Call the modal API with the request data and download the output file
        data = await request.json()
        image_url = data.get("image_url")
        audio_url = data.get("audio_url")

        if not image_url or not audio_url:
            raise HTTPException(status_code=400, detail="Missing image_url or audio_url in request")

        image_file = await download_file(image_url, ".jpg")
        audio_file = await download_file(audio_url, ".mp3")

        # Run ffmpeg command
        ffmpeg_cmd = f"ffmpeg -loop 1 -i {image_file} -i {audio_file} -c:v libx264 -tune stillimage -c:a aac -b:a 192k -shortest -pix_fmt yuv420p {output_path}"
        process = await asyncio.create_subprocess_shell(
            ffmpeg_cmd,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE
        )
        stdout, stderr = await process.communicate()

        if process.returncode != 0:
            print(f"FFmpeg error: {stderr.decode()}")
            raise HTTPException(status_code=500, detail=f"FFmpeg failed: {stderr.decode()}")

        # Clean up temporary files
        os.remove(image_file)
        os.remove(audio_file)

        # Return the URL path to the output file
        return f"/output/{output_filename}"
    except Exception as e:
        print(f"An error occurred: {str(e)}")
        print(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")

@app.post("/add_audio_to_video")
async def add_audio_to_video(request: Request):
    try:
        # Generate a unique filename
        output_filename = f"{uuid.uuid4()}.mp4"
        output_path = os.path.join(OUTPUT_DIR, output_filename)

        # Call the modal API with the request data and download the output file
        data = await request.json()
        video_url = data.get("video_url")
        audio_url = data.get("audio_url")

        if not video_url or not audio_url:
            raise HTTPException(status_code=400, detail="Missing video_url or audio_url in request")

        video_file = await download_file(video_url, ".mp4")
        audio_file = await download_file(audio_url, ".mp3")

        # Run ffmpeg command
        ffmpeg_cmd = f"ffmpeg -i {video_file} -i {audio_file} -c:v copy -c:a aac -shortest {output_path}"
        process = await asyncio.create_subprocess_shell(
            ffmpeg_cmd,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE
        )
        stdout, stderr = await process.communicate()

        if process.returncode != 0:
            print(f"FFmpeg error: {stderr.decode()}")
            raise HTTPException(status_code=500, detail=f"FFmpeg failed: {stderr.decode()}")

        # Clean up temporary files
        os.remove(video_file)
        os.remove(audio_file)

        # Return the URL path to the output file
        return f"/output/{output_filename}"
    except Exception as e:
        print(f"An error occurred: {str(e)}")
        print(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")

@app.post("/concatenate_videos")
async def concatenate_videos(request: Request):
    try:
        # Generate a unique filename for the output
        output_filename = f"{uuid.uuid4()}.mp4"
        output_path = os.path.join(OUTPUT_DIR, output_filename)

        # Call the modal API with the request data and download the output file
        data = await request.json()
        video_urls = data.get("video_urls")

        if not video_urls or not isinstance(video_urls, list):
            raise HTTPException(status_code=400, detail="Invalid video_urls in request. Must be a list of URLs.")

        # Download the video files
        video_files = []
        for i, url in enumerate(video_urls):
            video_file = await download_file(url, f"_{i}.mp4")
            video_files.append(video_file)

        # Create a temporary file with the list of input files
        concat_list_path = os.path.join(OUTPUT_DIR, "concat_list.txt")
        with open(concat_list_path, "w") as f:
            for file in video_files:
                f.write(f"file '{file}'\n")

        # Run ffmpeg command
        ffmpeg_cmd = f"ffmpeg -f concat -safe 0 -i {concat_list_path} -c copy {output_path}"
        process = await asyncio.create_subprocess_shell(
            ffmpeg_cmd,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE
        )
        stdout, stderr = await process.communicate()

        if process.returncode != 0:
            print(f"FFmpeg error: {stderr.decode()}")
            raise HTTPException(status_code=500, detail=f"FFmpeg failed: {stderr.decode()}")

        # Clean up temporary files
        for file in video_files:
            os.remove(file)
        os.remove(concat_list_path)

        # Return the URL path to the output file
        return f"/output/{output_filename}"

    except Exception as e:
        print(f"An error occurred: {str(e)}")
        print(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
    
@app.post("/concatenate_audio")
async def concatenate_audio(request: Request):
    try:
        # Generate a unique filename for the output
        output_filename = f"{uuid.uuid4()}.mp3"
        output_path = os.path.join(AUDIO_DIR, output_filename)

        # Call the modal API with the request data and download the output file
        data = await request.json()
        audio_urls = data.get("audio_urls")

        if not audio_urls or not isinstance(audio_urls, list):
            raise HTTPException(status_code=400, detail="Invalid audio_urls in request. Must be a list of URLs.")

        # Download the audio files
        audio_files = []
        for i, url in enumerate(audio_urls):
            audio_file = await download_file(url, f"_{i}.mp3")
            audio_files.append(audio_file)

        # Create a temporary file with the list of input files
        concat_list_path = os.path.join(AUDIO_DIR, "concat_list.txt")
        with open(concat_list_path, "w") as f:
            for file in audio_files:
                f.write(f"file '{file}'\n")

        # Run ffmpeg command
        ffmpeg_cmd = f"ffmpeg -f concat -safe 0 -i {concat_list_path} -c copy {output_path}"
        process = await asyncio.create_subprocess_shell(
            ffmpeg_cmd,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE
        )
        stdout, stderr = await process.communicate()

        if process.returncode != 0:
            print(f"FFmpeg error: {stderr.decode()}")
            raise HTTPException(status_code=500, detail=f"FFmpeg failed: {stderr.decode()}")

        # Clean up temporary files
        for file in audio_files:
            os.remove(file)
        os.remove(concat_list_path)

        # Return the URL path to the output file
        return f"/audio/{output_filename}"

    except Exception as e:
        print(f"An error occurred: {str(e)}")
        print(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")

@app.post("/make_video")
async def make_video(request: Request):
    try:
        # Generate a unique filename for the output
        output_filename = f"{uuid.uuid4()}.mp4"
        output_path = os.path.join(OUTPUT_DIR, output_filename)
        temp_dir = os.path.join(tempfile.gettempdir(), generate_hash())
        os.makedirs(temp_dir, exist_ok=True)

        data = await request.json()
        assets = data.get("assets", {})
        clips = assets.get("clips", [])
        music_url = assets.get("music_url")
        volume_adjustment = data.get("volume_adjustment", 1.0)  # Default to normal volume if not specified

        if not clips or not isinstance(clips, list):
            raise HTTPException(status_code=400, detail="Invalid clips in request.")

        # Create a list to hold clip video files that we'll concatenate later
        clip_videos = []
        segment_list_path = os.path.join(temp_dir, "segment_list.txt")
        
        with open(segment_list_path, "w") as segment_file:
            # Process each clip: combine image with its audio
            for i, clip in enumerate(clips):
                image_url = clip.get("image_url")
                audio_url = clip.get("audio_url")
                
                if not image_url or not audio_url:
                    raise HTTPException(status_code=400, detail=f"Missing image_url or audio_url in clip {i}")
                
                # Download files
                image_file = await download_file(image_url, ".jpg")
                audio_file = await download_file(audio_url, ".mp3")
                
                # Create segment video
                segment_output = os.path.join(temp_dir, f"segment_{i}.mp4")
                
                # Run ffmpeg command to create video segment
                segment_cmd = f'ffmpeg -loop 1 -i {image_file} -i {audio_file} -c:v libx264 -tune stillimage -c:a aac -b:a 192k -shortest -pix_fmt yuv420p {segment_output}'
                proc = await asyncio.create_subprocess_shell(
                    segment_cmd,
                    stdout=asyncio.subprocess.PIPE,
                    stderr=asyncio.subprocess.PIPE
                )
                _, stderr = await proc.communicate()
                
                if proc.returncode != 0:
                    print(f"FFmpeg error for segment {i}: {stderr.decode()}")
                    raise HTTPException(status_code=500, detail=f"FFmpeg failed for segment {i}: {stderr.decode()}")
                
                # Add to list for concatenation
                clip_videos.append(segment_output)
                segment_file.write(f"file '{segment_output}'\n")
                
                # Clean up individual files
                os.remove(image_file)
                os.remove(audio_file)
        
        # Concatenate all segment videos
        concat_output = os.path.join(temp_dir, "concat_output.mp4")
        concat_cmd = f'ffmpeg -f concat -safe 0 -i {segment_list_path} -c copy {concat_output}'
        
        proc = await asyncio.create_subprocess_shell(
            concat_cmd,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE
        )
        _, stderr = await proc.communicate()
        
        if proc.returncode != 0:
            print(f"FFmpeg concat error: {stderr.decode()}")
            raise HTTPException(status_code=500, detail=f"FFmpeg concat failed: {stderr.decode()}")
        
        # If there's a music URL, download it and mix with the video
        if music_url:
            music_file = await download_file(music_url, ".wav")
            
            # Final command to mix music with video at specified volume
            final_cmd = (
                f'ffmpeg -i {concat_output} -i {music_file} -filter_complex '
                f'"[1:a]volume={volume_adjustment}[music];[0:a][music]amix=inputs=2:duration=longest" '
                f'-c:v copy {output_path}'
            )
            
            proc = await asyncio.create_subprocess_shell(
                final_cmd,
                stdout=asyncio.subprocess.PIPE,
                stderr=asyncio.subprocess.PIPE
            )
            _, stderr = await proc.communicate()
            
            if proc.returncode != 0:
                print(f"FFmpeg final mix error: {stderr.decode()}")
                raise HTTPException(status_code=500, detail=f"FFmpeg music mixing failed: {stderr.decode()}")
            
            os.remove(music_file)
        else:
            # If no music, just copy the concatenated output
            shutil.copy(concat_output, output_path)
        
        # Clean up temporary files and directory
        for video_file in clip_videos:
            os.remove(video_file)
        os.remove(segment_list_path)
        os.remove(concat_output)
        os.rmdir(temp_dir)

        # Return the URL path to the output file
        return f"/output/{output_filename}"

    except Exception as e:
        print(f"An error occurred: {str(e)}")
        print(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")

@app.post("/concatenate_videos_with_music")
async def concatenate_videos_with_music(request: Request):
    try:
        # Generate a unique filename for the output
        output_filename = f"{uuid.uuid4()}.mp4"
        output_path = os.path.join(OUTPUT_DIR, output_filename)
        temp_dir = os.path.join(tempfile.gettempdir(), generate_hash())
        os.makedirs(temp_dir, exist_ok=True)

        data = await request.json()
        video_urls = data.get("video_urls")
        music_url = data.get("music_url")
        volume_adjustment = data.get("volume_adjustment", 1.0)  # Default volume if not specified

        if not video_urls or not isinstance(video_urls, list):
            raise HTTPException(status_code=400, detail="Invalid video_urls in request. Must be a list of URLs.")

        if not music_url:
            raise HTTPException(status_code=400, detail="Missing music_url in request.")

        # Download the video files
        video_files = []
        for i, url in enumerate(video_urls):
            video_file = await download_file(url, f"_{i}.mp4")
            video_files.append(video_file)

        # Create a temporary file with the list of input files
        concat_list_path = os.path.join(temp_dir, "concat_list.txt")
        with open(concat_list_path, "w") as f:
            for file in video_files:
                f.write(f"file '{file}'\n")

        # First concatenate all videos
        concat_output = os.path.join(temp_dir, "concat_output.mp4")
        concat_cmd = f"ffmpeg -f concat -safe 0 -i {concat_list_path} -c:v copy -an {concat_output}"
        process = await asyncio.create_subprocess_shell(
            concat_cmd,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE
        )
        stdout, stderr = await process.communicate()

        if process.returncode != 0:
            print(f"FFmpeg concat error: {stderr.decode()}")
            raise HTTPException(status_code=500, detail=f"FFmpeg concat failed: {stderr.decode()}")

        # Download music file
        music_file = await download_file(music_url, ".mp3")

        # Add music to the concatenated video
        final_cmd = (
            f'ffmpeg -i {concat_output} -i {music_file} -map 0:v -map 1:a '
            f'-c:v copy -c:a aac -shortest -af "volume={volume_adjustment}" {output_path}'
        )
        process = await asyncio.create_subprocess_shell(
            final_cmd,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE
        )
        stdout, stderr = await process.communicate()

        if process.returncode != 0:
            print(f"FFmpeg music mix error: {stderr.decode()}")
            raise HTTPException(status_code=500, detail=f"FFmpeg music mixing failed: {stderr.decode()}")

        # Clean up temporary files
        for file in video_files:
            os.remove(file)
        os.remove(concat_list_path)
        os.remove(concat_output)
        os.remove(music_file)
        os.rmdir(temp_dir)

        # Return the URL path to the output file
        return f"/output/{output_filename}"

    except Exception as e:
        print(f"An error occurred: {str(e)}")
        print(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)