# My shell configuration
{
  pkgs,
  lib,
  config,
  ...
}: {
  home.packages = with pkgs; [
    bat
    ripgrep
  ];

  # Add go binaries to the PATH
  home.sessionPath = ["$HOME/go/bin"];

  home.sessionVariables = {
    COLORTERM = "truecolor";
  };

  programs.zsh = {
    enable = true;
    enableCompletion = true;
    autosuggestion.enable = true;
    syntaxHighlighting = {
      enable = true;
      highlighters = [
        "main"
        "brackets"
        "pattern"
        "regexp"
        "root"
        "line"
      ];
    };
    historySubstringSearch.enable = true;

    history = {
      ignoreDups = true;
      save = 10000;
      size = 10000;
    };

    profileExtra = lib.optionalString (config.home.sessionPath != []) ''
      export PATH="$PATH''${PATH:+:}${lib.concatStringsSep ":" config.home.sessionPath}"
    '';

    shellAliases = {
      # Change default
      vim = "nvim";
      vi = "nvim";
      cd = "z";
      ls = "eza --icons=always --no-quotes";
      tree = "eza --icons=always --tree --no-quotes";
      cat = "bat --theme=base16 --color=always --paging=never --tabs=2 --wrap=never --plain";
      mkdir = "mkdir -p";

      # Shortcuts
      spt = "spotatui";
      v = "nvim";
      c = "clear";
      e = "exit";
      open = "${pkgs.xdg-utils}/bin/xdg-open";

      notes = "nvim ~/notes/index.md --cmd 'cd ~/notes' -c ':lua Snacks.picker.smart()'";
      nix-shell = "nix-shell --command zsh";

      # git
      g = "lazygit";
      ga = "git add";
      gc = "git commit";
      gcu = "git add . && git commit -m 'Update'";
      gp = "git push";
      gpl = "git pull";
      gs = "git status";
      gd = "git diff";
      gco = "git checkout";
      gcb = "git checkout -b";
      gbr = "git branch";
      grs = "git reset HEAD~1";
      grh = "git reset --hard HEAD~1";
      gaa = "git add .";
      gcm = "git commit -m";

      # Typo
      clera = "clear";
      celar = "clear";
      claer = "clear";
      sl = "ls";
    };

    initContent =
      # bash
      ''
        bindkey -e

        # Open command in VIM
        autoload -Uz edit-command-line
        zle -N edit-command-line
        bindkey '^X' edit-command-line

        # Suffix Aliases
        alias -s {nix,md,txt,json,yml,yaml,go}=nvim
        alias -s {png,jpg,jpeg,gif,pdf}=xdg-open

        # 3. Global Aliases (Remplacés n'importe où dans la commande, pas seulement au début)
        alias -g G="| grep"
        alias -g L="| less"
        alias -g V="| nvim"
        alias -g H="| head"
        alias -g T="| tail"
        alias -g JQ="| jq"
        alias -g C="| wl-copy"
        alias -g NE="2>/dev/null"
        alias -g ND=">/dev/null"
        alias -g NUL=">/dev/null 2>1"

        # search history based on what's typed in the prompt
        autoload -U history-search-end
        zle -N history-beginning-search-backward-end history-search-end
        zle -N history-beginning-search-forward-end history-search-end
        bindkey "^[OA" history-beginning-search-backward-end
        bindkey "^[OB" history-beginning-search-forward-end

        # General completion behavior
        zstyle ':completion:*' completer _extensions _complete _approximate

        # Use cache
        zstyle ':completion:*' use-cache on
        zstyle ':completion:*' cache-path "$XDG_CACHE_HOME/zsh/.zcompcache"

        # Complete the alias
        zstyle ':completion:*' complete true

        # Autocomplete options
        zstyle ':completion:*' complete-options true

        # Completion matching control
        zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
        zstyle ':completion:*' keep-prefix true

        # Group matches and describe
        zstyle ':completion:*' menu select
        zstyle ':completion:*' list-grouped false
        zstyle ':completion:*' list-separator '''
        zstyle ':completion:*' group-name '''
        zstyle ':completion:*' verbose yes
        zstyle ':completion:*:matches' group 'yes'
        zstyle ':completion:*:warnings' format '%F{red}%B-- No match for: %d --%b%f'
        zstyle ':completion:*:messages' format '%d'
        zstyle ':completion:*:corrections' format '%B%d (errors: %e)%b'
        zstyle ':completion:*:descriptions' format '[%d]'

        # Colors
        zstyle ':completion:*' list-colors ''${(s.:.)LS_COLORS}

        # Case insensitive tab completion
        zstyle ':completion:*:*:cd:*' tag-order local-directories directory-stack path-directories
        zstyle ':completion:*:*:cd:*:directory-stack' menu yes select
        zstyle ':completion:*:-tilde-:*' group-order 'named-directories' 'path-directories' 'users' 'expand'
        zstyle ':completion:*:*:-command-:*:*' group-order aliases builtins functions commands
        zstyle ':completion:*' special-dirs true
        zstyle ':completion:*' squeeze-slashes true

        # Sort
        zstyle ':completion:*' sort false
        zstyle ":completion:*:git-checkout:*" sort false
        zstyle ':completion:*' file-sort modification
        zstyle ':completion:*:eza' sort false
        zstyle ':completion:complete:*:options' sort false
        zstyle ':completion:files' sort false

        autoload zmv

        hash -d dl=~/Downloads
        hash -d ni=~/.config/nixos
        hash -d de=~/dev
        hash -d cy=~/Cyber

        ${lib.optionalString config.services.gpg-agent.enable ''
          gnupg_path=$(ls $XDG_RUNTIME_DIR/gnupg)
          export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/gnupg/$gnupg_path/S.gpg-agent.ssh"
        ''}

        # Allow foot to pipe command output
        function precmd {
          if ! builtin zle; then
              print -n "\e]133;D\e\\"
          fi
        }
        function preexec {
          print -n "\e]133;C\e\\"
        }

      '';
  };
}
