import subprocess
from pathlib import Path
import math

MAX_SIZE = 49 * 1024 * 1024  # 49 MB safety margin


def get_duration_seconds(file_path: Path) -> float:
    cmd = [
        "ffprobe",
        "-v", "error",
        "-show_entries", "format=duration",
        "-of", "default=noprint_wrappers=1:nokey=1",
        str(file_path)
    ]
    return float(subprocess.check_output(cmd).decode().strip())


def split_audio(file_path: Path):
    size = file_path.stat().st_size

    if size <= MAX_SIZE:
        return [file_path]

    existing = sorted(file_path.parent.glob(f"{file_path.stem}_part*.mp3"))
    if existing:
        return existing

    duration = get_duration_seconds(file_path)

    parts_count = math.ceil(size / MAX_SIZE)
    part_duration = duration / parts_count

    output_files = []

    for i in range(parts_count):
        start = i * part_duration
        output = file_path.with_name(f"{file_path.stem}_part{i+1}.mp3")

        cmd = [
            "ffmpeg",
            "-y",
            "-i", str(file_path),
            "-ss", str(start),
            "-t", str(part_duration),
            "-acodec", "copy",
            str(output)
        ]

        subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        output_files.append(output)

    return output_files
