# Tele-Québec extractor fixes until upstream yt-dlp carries them.
#
# Regenerate `modules/patches/yt-dlp-telequebec.patch` from yt-dlp checkout
# (branch with fixes vs master), redirecting into this flake's modules/patches/:
#
#   git diff master..ie/telequebec-update -- \
#     yt_dlp/extractor/telequebec.py yt_dlp/extractor/_extractors.py \
#     > path/to/chiasson-nix/modules/patches/yt-dlp-telequebec.patch
#
{ ... }: {
  flake.nixosModules.systemYtDlpTelequebecPatch =
    { config, lib, ... }:
    let
      cfg = config.chiasson.system.ytDlpTelequebecPatch;
      patchFile = ../patches/yt-dlp-telequebec.patch;
    in
    {
      options.chiasson.system.ytDlpTelequebecPatch = {
        enable = lib.mkEnableOption ''
          Patch yt-dlp with Tele-Québec extractor updates (telequebec.tv, Coucou, season playlists).
          Regenerate `modules/patches/yt-dlp-telequebec.patch` when nixpkgs bumps yt-dlp if the build fails.
        '';
      };

      config = lib.mkIf cfg.enable {
        nixpkgs.overlays = lib.mkOrder 1000 [
          (final: prev: {
            yt-dlp = prev.yt-dlp.overrideAttrs (old: {
              patches = (old.patches or [ ]) ++ [ patchFile ];
            });
          })
        ];
      };
    };
}
