{ pkgs, ... }:

{
  packages = with pkgs; [
    ffmpeg-full
    git
    git-lfs
    jq
    imagemagick
    mkvtoolnix
    mediainfo
    tesseract
    (python312.withPackages (ps: with ps; [
      numpy
      opencv4
      scipy
      scikit-image
      pillow
      pytesseract
      tqdm
      pyyaml
      scenedetect
    ]))
  ];

  env = {
    PYTHONNOUSERSITE = "1";
    RESTORE_WORKDIR = ".";
  };

  enterShell = ''
    echo "Video restoration dev environment ready."
    echo "Run check-tools to verify toolchain and probe-video <input> for diagnostics."
  '';

  scripts = {
    "check-tools".exec = ''
      set -euo pipefail
      echo "ffmpeg: $(ffmpeg -version | head -n 1)"
      echo "python: $(python3 --version)"
      echo "tesseract: $(tesseract --version | head -n 1)"
      echo "scenedetect: $(python3 -m scenedetect version)"
    '';

    "probe-video".exec = ''
      set -euo pipefail
      if [ "$#" -ne 1 ]; then
        echo "Usage: probe-video <input-video>" >&2
        exit 2
      fi

      in="$1"
      ffprobe -v error \
        -show_entries format=filename,duration,size,bit_rate \
        -show_entries stream=index,codec_type,codec_name,width,height,pix_fmt,field_order,r_frame_rate,avg_frame_rate,bit_rate \
        -of default=noprint_wrappers=1 "$in"
      echo "--- idet summary (first 1200 frames) ---"
      ffmpeg -hide_banner -i "$in" -vf idet -frames:v 1200 -an -f null - 2>&1 | tail -n 20
    '';
  };
}
