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

let
  cfg = config.samfelag.modules.shell.zsh;
in
{
  options.samfelag.modules.shell.zsh = {
    enable = lib.mkEnableOption "zsh";
  };

  config = lib.mkIf cfg.enable {
    environment.systemPackages = with pkgs; [
      zsh
    ];

    programs.zsh.enable = true;
    hm.programs.zsh = {
      enable = true;
      dotDir = ".config/zsh";

      autosuggestion.enable = true;
      enableCompletion = true;
      syntaxHighlighting.enable = true;

      initContent = let
        zshConfig = lib.mkOrder 1000 ''
          # Source generic (bash) configuration
          if [ -d $HOME/.config/shell/sh ]; then
            for rc in $HOME/.config/shell/sh/*.sh; do
              emulate bash -c ". $rc"
            done
          fi

          # Source zsh specific configuration
          if [[ -d $HOME/.config/shell/zsh ]]; then
            for rc in $HOME/.config/shell/zsh/*.zsh; do
              source $rc
            done
          fi
        '';
      in
      lib.mkMerge [
        zshConfig
      ];

      oh-my-zsh = {
        enable = true;
        plugins = [
          "git"
          # "zsh-vi-mode"
        ];
      };

      plugins = [
        {
          name = "zsh-nix-shell";
          file = "nix-shell.plugin.zsh";
          src = pkgs.fetchFromGitHub {
            owner = "chisui";
            repo = "zsh-nix-shell";
            rev = "v0.5.0";
            sha256 = "0za4aiwwrlawnia4f29msk822rj9bgcygw6a8a6iikiwzjjz0g91";
          };
        }
        {
          name = "vi-mode";
          src = pkgs.zsh-vi-mode;
          file = "share/zsh-vi-mode/zsh-vi-mode.plugin.zsh";
        }
      ];
    };

    samfelag.modules.shell.starship.enable = true;
  };
}
