# Ghostty — Nomarchy's default terminal, themed from theme-state.json.
# Colors, fonts and the full 16-color ANSI palette are baked from the
# JSON at eval time.
{ config, lib, ... }:

let
  t = config.nomarchy.theme;
  c = t.colors;
in
{
  # Ghostty is ALWAYS installed — it's Nomarchy's default terminal and is
  # load-bearing (SUPER+E file manager, the calendar launcher's classed
  # window, etc.), so the distro enforces it. `nomarchy.ghostty.enable` now
  # gates only whether Nomarchy's theming/config is applied — a user can
  # keep ghostty but drop our config, they just can't remove ghostty itself.
  programs.ghostty = {
    enable = true;
    enableBashIntegration = true;

    settings = lib.mkIf config.nomarchy.ghostty.enable {
      # ── Typography (from theme-state.json) ────────────────────────
      font-family = t.fonts.mono;
      font-size = t.fonts.size;

      # ── Colors (from theme-state.json) ────────────────────────────
      background = c.base;
      foreground = c.text;
      cursor-color = c.accent;
      selection-background = c.overlay;
      selection-foreground = c.text;
      split-divider-color = c.surface;

      # "N=#rrggbb" entries; Ghostty accepts repeated `palette` keys,
      # which the HM module renders from this list.
      palette = lib.imap0 (i: color: "${toString i}=${color}") t.ansi;

      # ── Chrome ────────────────────────────────────────────────────
      # background-opacity is theme-driven (normal priority — use the
      # CLI); the rest are mkDefault so a plain home.nix value wins.
      background-opacity = t.ui.terminalOpacity;
      window-padding-x = lib.mkDefault 12;
      window-padding-y = lib.mkDefault 12;
      window-decoration = lib.mkDefault false;
      gtk-single-instance = lib.mkDefault true;
      confirm-close-surface = lib.mkDefault false;
    };
  };
}
