{lib, config, ...}:
let
  cfg = config.ghosttyApp;
in
{
  options.ghosttyApp = {
    enable = lib.mkEnableOption "enable ghostty terminal";

    fontFamily = lib.mkOption {
      type = lib.types.str;
      default = "Maple Mono NF";
      description = "Font family to use in ghostty";
    };

    enableLigatures = lib.mkOption {
      type = lib.types.bool;
      default = true;
      description = "Enable font ligatures in ghostty";
    };
  };

  config = lib.mkIf cfg.enable {
    programs.ghostty = {
      enable = true;
      settings = {
        font-family = cfg.fontFamily;

        # Maple Mono NF OpenType features
        # See: https://github.com/subframe7536/maple-font/blob/variable/source/features/README.md
        font-feature = lib.mkIf cfg.enableLigatures [
          # Ligatures (all Maple Mono ligatures are in calt)
          "calt"

          # Character variants
          "cv02"  # Alternative 'a' with top arm
          "cv05"  # Alternative 'g' double story style
          "cv06"  # Alternative 'i' without bottom bar
          "cv08"  # Alternative 'r' with bottom bar
          "cv09"  # Alternative '7' with middle bar
          "cv10"  # Alternative 'Z' and 'z' with middle bar
          "cv11"  # Alternative 'f' with bottom bar
          "cv61"  # Alternative ',' and ';' with straight tail
          "cv62"  # Alternative '?' with larger openings
          "cv65"  # Alternative '&' in handwriting style

          # Italic-only variants
          "cv31"  # Italic 'a' with top arm
          "cv32"  # Italic 'f' without bottom tail
          "cv38"  # Italic 'g' double story style
          "cv39"  # Italic 'i' without bottom bar
          "cv41"  # Italic 'r' with bottom bar
          "cv42"  # Italic '7' with middle bar
          "cv43"  # Italic 'Z' and 'z' with middle bar
          "cv44"  # Italic 'f' with bottom bar

          # CN variant
          "cv96"  # Full width quotes
        ];

        window-padding-x = 4;
        window-padding-y = 4;

        keybind = [
          "ctrl+shift+plus=increase_font_size:1"
          "shift+enter=text:\\n"
        ];
      };
    };
  };
}
