# utils/media.py

import subprocess

async def overlay_username_with_ffmpeg(base_video_path, output_path, username):
    font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
    text = f"@{username}"

    # Define your six positions (x, y)
    positions = {
        1: ("250", "370"),
        2: ("330", "130"),
        3: ("300", "90"),
        4: ("380", "180"),
        5: ("365", "215"),
        6: ("55", "365"),
        7: ("290", "130"),
        8: ("320", "210"),
        9: ("310", "240"),
        10: ("400", "240")
    }

    # Each entry: (start_time, end_time, position_index)
    text_entries = [
        (4.767, 5.367, 1, "username"),
        (5.4, 5.967, 2, "username"),
        (6.233, 6.833, 3, "username"),
        (6.967, 7.6, 4, "username"),
        (7.733, 8.367, 5, "username"),
        (8.667, 9.133, 6, "username"),
        (9.733, 10.667, 7, "username"),
        (11.6, 12.033, 8, "@everyone"),
        (12.067, 13.0, 9, "@everyone"),
        (13.033, 14.135, 10, "@everyone"),
    ]

    # Build drawtext filters
    drawtext_filters = []
    for start, end, pos_id, text_type in text_entries:
        x_coord, y_coord = positions[pos_id]

        # Determine actual text content
        text_content = f"@{username}" if text_type == "username" else text_type

        x = f"{x_coord} - text_w/2"
        y = f"{y_coord} - text_h/2"

        filter_str = (
            f"drawtext=text='{text_content}':"
            f"fontfile='{font_path}':"
            f"fontcolor=black:fontsize=30:x={x}:y={y}:"
            f"enable='between(t,{start},{end})'"
        )
        drawtext_filters.append(filter_str)

    vf_string = ",".join(drawtext_filters)

    ffmpeg_command = [
        "ffmpeg",
        "-i", base_video_path,
        "-vf", vf_string,
        "-codec:a", "copy",
        output_path
    ]

    try:
        subprocess.run(ffmpeg_command, check=True)
        print("✅ Video processed successfully with username overlays.")
    except subprocess.CalledProcessError as e:
        print(f"⚠️ FFmpeg error: {e}")
