"""Splitter service that calls the core audio_splitter logic."""

import os
import sys
import shutil
from pathlib import Path
from typing import List, Dict, Any

from backend.config import settings
from backend.services.task_manager import task_manager
from backend.services.file_manager import FileManager
from backend.models.request import TracklistEntry
from backend.models.response import TaskStatus


def run_split_task(task_id: str, tracklist: List[TracklistEntry], options: Dict[str, Any]) -> None:
    try:
        task_manager.update_task_with_progress(
            task_id, progress=5, message="Initializing..."
        )

        input_path = FileManager.get_input_path(task_id)
        if not input_path or not input_path.exists():
            raise RuntimeError(f"Input file not found for task {task_id}")

        output_dir = FileManager.ensure_output_dir(task_id)

        tracks = []
        for entry in tracklist:
            track_dict = {
                "ts": entry.ts,
                "tn": entry.tn or "",
                "an": entry.an or "",
                "al": entry.al or "",
                "date": entry.date or "",
                "ext": entry.ext or ""
            }
            tracks.append(track_dict)

        from types import SimpleNamespace

        args = SimpleNamespace(
            format=options.get("format", "mp3"),
            transcode_to=options.get("transcode_to", None),
            drop_video=options.get("drop_video", False),
            drop_subs=options.get("drop_subs", False),
            number_tracks=options.get("number_tracks", False),
            replace_bad_chars=options.get("replace_bad_chars", False),
            replacement_char=options.get("replacement_char", "_"),
            bad_chars=options.get("bad_chars", r'!@#№$;:%^&?*(){}[]\/<>+=~`\' '),
            skip_existing=options.get("skip_existing", False),
            output_template=options.get("output_template", "%an-%tn.%ext"),
            album=options.get("album", None),
            comment=options.get("comment", None),
            no_comment=options.get("no_comment", False),
            comment_stream=options.get("comment_stream", None),
            merge_comments=options.get("merge_comments", False),
            comment_separator=options.get("comment_separator", "; "),
            delete_original=False,
        )

        project_root = Path(__file__).parent.parent.parent.parent
        if str(project_root) not in sys.path:
            sys.path.insert(0, str(project_root))

        from audio_splitter.core import split_audio

        # We need to track progress from the core.
        # Since the core doesn't have a progress callback, we'll update progress
        # based on track list size (approximate).
        total_tracks = len(tracks)
        progress_base = 10  # Starting progress after init

        task_manager.update_task_with_progress(
            task_id, progress=progress_base, message="Starting split..."
        )

        # Run the split
        # The core prints progress to stdout, but we can't easily capture it.
        # We'll update progress based on track count.
        # For now, we'll report progress after the split completes.
        # A more advanced implementation would capture stdout or add a callback.

        split_audio(str(input_path), str(output_dir), tracks, args)

        # After split completes, get the output files
        output_files = FileManager.get_output_files(task_id)

        task_manager.update_task_with_progress(
            task_id,
            progress=100,
            message="Split complete",
            status=TaskStatus.DONE
        )

        # Add tracks to the task state
        task_manager.update_task(
            task_id,
            tracks=output_files
        )

    except Exception as e:
        task_manager.update_task_with_progress(
            task_id,
            progress=0,
            message="Split failed",
            status=TaskStatus.ERROR,
            error=str(e)
        )