{ 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
      zsh-powerlevel10k
    ];

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

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

      initExtra = ''
        # 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
      '';

      initExtraBeforeCompInit = ''
        # p10k instant prompt
        P10K_INSTANT_PROMPT="$XDG_CACHE_HOME/p10k-instant-prompt-''${(%):-%n}.zsh"
        [[ ! -r "$P10K_INSTANT_PROMPT" ]] || source "$P10K_INSTANT_PROMPT"
      '';

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

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

    hm.xdg.configFile."zsh/.p10k.zsh".source = ../../config/zsh/.p10k.zsh;

  };
}
