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

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

  config = mkIf cfg.enable {

    home.packages = with pkgs; [
      zsh
      zsh-powerlevel10k
      (nerdfonts.override { fonts = [ "Iosevka" ]; })
    ];

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

      enableAutosuggestions = true;
      enableCompletion = true;
      enableSyntaxHighlighting = 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";
        }
      ];
    };

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

  };
}
