# Git configuration with signing, aliases, and global ignore.
# Identity and host-specific SSH keys are set per-host in home/m3tam3re/.
{
  lib,
  pkgs,
  ...
}:
with lib; {
  programs.git = {
    enable = true;
    signing.format = null;
    settings = {
      user = {
        name = lib.mkDefault "m3tam3re";
        email = lib.mkDefault "p@m3ta.dev";
      };
      core.excludesfile = "~/.gitignore_global";
      init.defaultBranch = "master";

      # --- Diff-Config ---
      # Use difftastic as default external diff tool
      diff.external = "difft";
      diff.colorMoved = "default";

      # --- Merge-Config ---
      merge.conflictStyle = "zdiff3";

      # --- Pull-Config ---
      # Use merge instead of rebase for clearer history
      pull.rebase = false;

      # --- Rerere ---
      # Reuse recorded resolutions for recurring conflicts
      rerere.enabled = true;

      # --- Push-Config ---
      # Automatically set upstream when pushing to new branch
      push.autoSetupRemote = true;

      # --- Pager ---
      pager.diff = true;
      pager.log = false;

      # --- Aliases ---
      alias = {
        # Basic shortcuts
        st = "status";
        co = "checkout";
        br = "branch";

        # Enhanced log with graph
        lg = "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit";

        # Difftastic aliases for one-off usage
        # Use with: git dft, git ds, git dl
        dft = "-c diff.external=difft diff";
        ds = "-c diff.external=difft show --ext-diff";
        dl = "-c diff.external=difft log -p --ext-diff";
      };
    };
  };

  # Difftastic configuration
  programs.difftastic = {
    enable = true;
    options = {
      # Anzeige: Passt zu den meisten Terminal-Themes
      color = "dark";

      # Word-level diffs inline statt side-by-side
      side-by-side = false;

      # Standard-Tab-Breite
      tab-width = 8;

      # Trailing Commas anzeigen
      trailing-commas = true;

      # Konsistente Diffs durch alphabetische Pfad-Sortierung
      sort-paths = true;

      # Nix-spezifisch
      language = [
        "Nix"
        "Nanorc"
      ];
    };
  };

  # Jujutsu (jj) configuration
  programs.jujutsu = {
    enable = true;
    settings = {
      user = {
        email = "m@m3tam3re.com";
        name = "Sascha Koenig";
      };

      # --- UI ---
      ui = {
        # Immer Farben (auch in Pipes)
        color = "always";

        # Log als Default-Command
        default-command = "log";

        # Schönerer Graph
        graph-style = "curved";

        # Pager-Config
        pager = "less -FRX";
        paginate = "auto";

        # --- Diff ---
        # Difftastic als Default-Diff-Formatter
        diff-formatter = ["difft" "--color=always" "$left" "$right"];
      };

      # --- Revsets ---
      # Zeige @, Parent-Änderungen und immutable Heads
      revsets.log = "present(@) | @-:: | immutable_heads()..";
    };
  };

  home.packages = with pkgs; [
    lazygit
  ];
}
