{ pkgs, lib, config, ... }:
let
  palettes = {
    latte = {
      "os" = "#ACB0BE";
      "closer" = "p:os";
      "pink" = "#ea76cb";
      "lavender" = "#7287FD";
      "blue" = "#1e66f5";
      "peach" = "#fe640b";
      "red" = "#d20f39";
      "green" = "#40a02b";
      "mauve" = "#8839ef";
    };
    mocha = {
      "os" = "#ACB0BE";
      "closer" = "p:os";
      "pink" = "#F5BDE6";
      "lavender" = "#B7BDF8";
      "blue" = "#8aadf4";
      "peach" = "#f5a97f";
      "red" = "#ed8796";
      "green" = "#a6da95";
      "mauve" = "#c6a0f6";
    };
  };
in
with lib;
{
  options = {
    oh-my-posh = {
      enable = mkEnableOption "enable oh-my-posh";
      theme = mkOption {
        type = types.enum (builtins.attrNames palettes);
        default = "latte";
        description = "Catppuccin theme variant for oh-my-posh.";
      };
    };
  };

  config = mkIf config.oh-my-posh.enable {
    programs.oh-my-posh = {
      enable = true;
      settings = {
        "$schema" = "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json";
        "palette" = palettes.${config.oh-my-posh.theme};
        "blocks" = [
          {
            "alignment" = "left";
            "newline" = true;
            "type" = "prompt";
            "segments" = [
              {
                "background" = "transparent";
                "foreground" = "p:blue";
                "style" = "diamond";
                "template" = "{{.Icon}}  ";
                "type" = "os";
              }
              {
                "foreground" = "p:peach";
                "properties" = {
                  "home_icon" = "~";
                  "style" = "unique";
                };
                "style" = "plain";
                "template" = "{{ .Path }} ";
                "type" = "path";
              }
              {
                "foreground" = "p:green";
                "properties" = {
                  "fetch_status" = false;
                  "fetch_upstream_icon" = false;
                };
                "template" = "{{ .HEAD }} ";
                "style" = "plain";
                "type" = "git";
              }
              {
                "style" = "plain";
                "foreground" = "p:closer";
                "type" = "text";
              }
              {
                "foreground" = "p:mauve";
                "style" = "plain";
                "template" = "{{ .UserName }}@";
                "type" = "session";
              }
              {
                "foreground" = "p:peach";
                "style" = "plain";
                "template" = "{{ .HostName }}";
                "type" = "session";
              }
            ];
          }
          {
            "type" = "prompt";
            "alignment" = "left";
            "newline" = true;
            "segments" = [
              {
                "type" = "text";
                "style" = "plain";
                "background" = "transparent";
                "foreground_templates" = [
                  "{{if gt .Code 0}}p:red{{end}}"
                  "{{if eq .Code 0}}p:green{{end}}"
                ];
                "template" = ">";
              }
            ];
          }
        ];
        "final_space" = true;
        "version" = 2;
        "transient_prompt" = {
          "background" = "transparent";
          "foreground_templates" = [
            "{{if gt .Code 0}}p:red{{end}}"
            "{{if eq .Code 0}}p:green{{end}}"
          ];
          "template" = ">";
        };
        "secondary_prompt" = {
          "background" = "transparent";
          "foreground_templates" = [
            "{{if gt .Code 0}}p:red{{end}}"
            "{{if eq .Code 0}}p:green{{end}}"
          ];
          "template" = ">";
        };
      };
    };
  };
}
