{ pkgs, lib, config, ... }:

let
  inherit (lib) concatStringsSep mkOption types;

  mkFzfOptionsString = lines: concatStringsSep " " lines;

  catppuccinFzfOptions = {
    mocha = mkFzfOptionsString [
      "--color=bg+:#313244,bg:#1e1e2e,spinner:#f5e0dc,hl:#f38ba8"
      "--color=fg:#cdd6f4,header:#f38ba8,info:#cba6f7,pointer:#f5e0dc"
      "--color=marker:#b4befe,fg+:#cdd6f4,prompt:#cba6f7,hl+:#f38ba8"
      "--color=selected-bg:#45475a"
      "--multi"
    ];
    latte = mkFzfOptionsString [
      "--color=bg+:#ccd0da,bg:#eff1f5,spinner:#dc8a78,hl:#d20f39"
      "--color=fg:#4c4f69,header:#d20f39,info:#1e66f5,pointer:#dc8a78"
      "--color=marker:#fe640b,fg+:#4c4f69,prompt:#1e66f5,hl+:#d20f39"
      "--color=selected-bg:#acb0be"
      "--multi"
    ];
  };
in
{
  options.fish = {
    theme = mkOption {
      type = types.enum (builtins.attrNames catppuccinFzfOptions);
      default = "mocha";
      description = "Catppuccin theme variant used for Fish integrations such as FZF.";
      example = "latte";
    };
  };

  config = {
    programs.fish = {
      enable = true;
      plugins = [
        {
          name = "z";
          src = pkgs.fetchFromGitHub {
            owner = "jethrokuan";
            repo = "z";
            rev = "e0e1b9dfdba362f8ab1ae8c1afc7ccf62b89f7eb";
            sha256 = "0dbnir6jbwjpjalz14snzd3cgdysgcs3raznsijd6savad3qhijc";
          };
        }
        {
          name = "autopair";
          src = pkgs.fishPlugins.autopair.src;
        }
        {
          name = "foreign-env";
          src = pkgs.fishPlugins.foreign-env.src;
        }
        {
          name = "fzf";
          src = pkgs.fishPlugins.fzf.src;
        }
        {
          name = "done";
          src = pkgs.fishPlugins.done.src;
        }
        {
          name = "grc";
          src = pkgs.fishPlugins.grc.src;
        }
      ];
      shellAbbrs = {
        ls = "eza -l";
        lg = "lazygit";
        ka = "kubectl apply -f";
        t = "timew";
        todo = "jira issue list -a 'Fredrik Wastring' -s ~Done -s ~Closed -s ~Released --plain";
        cam = "jira issue create && jira issue assign && jira issue move";
        e = "kubectx";
        s = {
          setCursor = "%";
          expansion = "cha https://search.wastring.com/search?q=%";
        };
        tw = {
          setCursor = "%";
          expansion = "typst watch % --open zathura /tmp/zathura.pdf";
        };
        c = {
          setCursor = "%";
          expansion = "ssh 'fw:%@gateway.internalifacts.se' -p 2222";
        };
        dl = {
          setCursor = "&";
          expansion = "yt-dlp -o \"~/videor/%(title)s - %(uploader)s\" \"&\"";
        };
      };
      interactiveShellInit =
        let
          fzfOpts = catppuccinFzfOptions.${config.fish.theme};
        in ''
          fish_vi_key_bindings
          set fish_greeting
          set FLAKE_DIR "/home/fw/nix"
          set JIRA_API_TOKEN ATATT3xFfGF0_fkpGB1ne-QOSJzFVG0yH31j2CRtdNqbePCyEm9enpnA2uA3go75_GQwZPFX_IO9tf10ALJWvDLjsuHl8MSOUkNd703Vqr4uuGLAbHY73Z_b9fDJVrfodTrGAN9sZ5Sp75opCVkXB7MVXSAIvlWimRdwe-tqDPH4vhwY9Hqcs6M=0510D6CD
          set -Ux FZF_CTRL_R_OPTS "--reverse"
          set -Ux FZF_TMUX_OPTS "-p"
          set -e GOROOT
          set -e GOPATH
          gh completion -s fish > ~/.config/fish/completions/gh.fish
          set -gx PATH $PATH $HOME/.krew/bin

          set -Ux FZF_DEFAULT_OPTS "${fzfOpts}"
        '';
    };
  };
}
