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

let
  cfg = config.local.git;
in
{
  options.local.git = with lib; {
    userName = mkOption {
      type = types.str;
      description = "Set your global name";
    };

    userEmail = mkOption {
      type = types.str;
      description = "Set your global email";
    };

    gpgKey = mkOption {
      type = types.nullOr types.str;
      default = null;
      description = "The default GnuPG signing key fingerprint";
    };

    git-crypt = {
      enable = mkOption {
        type = types.bool;
        default = false;
        description = "Add git-crypt package";
      };
    };
  };

  config = {
    home.packages = lib.mkIf cfg.git-crypt.enable [ pkgs.git-crypt ];

    programs.git = {
      enable = true;
      inherit (cfg) userName userEmail;
      signing = lib.mkIf (cfg.gpgKey != null) {
        key = cfg.gpgKey;
        signByDefault = true;
      };
      extraConfig = {
        init.defaultBranch = "main";
        pull.rebase = true;
      };
      aliases = {
        co = "checkout";
        cob = "checkout -b";
        cobf = "checkout -B";
        st = "status -sb";
        d = "diff";
        dc = "diff --cached";
        aa = "add .";
        ai = "add -i";
        c = "commit";
        cm = "commit -m";
        ca = "commit --amend";
        cam = "commit --amend -m";
        can = "commit --amend --no-edit";
        p = "push";
        po = "push origin";
        pf = "push --force-with-lease";
        pfo = "push --force-with-lease origin";
        pl = "pull";
        plo = "pull origin";
        rb = "rebase";
        rbi = "rebase -i";
        lo = "log --pretty=oneline";
      };
    };
  };
}
