import logging
from pathlib import Path

import matchering as mg

from songmatch.config import SongMatchConfig

logger = logging.getLogger(__name__)

_BIT_DEPTH_RESULT = {
    16: mg.pcm16,
    24: mg.pcm24,
}


def run_matchering(
    target_audio: Path,
    reference_audio: Path,
    output_path: Path,
    config: SongMatchConfig,
) -> Path:
    """Run matchering on the target audio using the reference.

    Returns the output path.
    """
    result_fn = _BIT_DEPTH_RESULT.get(config.output_bits)
    if result_fn is None:
        raise ValueError(f"Unsupported output bit depth for matchering: {config.output_bits}")

    logger.info("Running matchering: target=%s, reference=%s", target_audio.name, reference_audio.name)

    mg.process(
        target=str(target_audio),
        reference=str(reference_audio),
        results=[result_fn(str(output_path))],
    )

    logger.info("Matchering output: %s", output_path.name)
    return output_path
