{ config, lib, ... }:
let
  cfg = config.khscodes.khs.shell.oh-my-posh;
  unicodeChar = code: builtins.fromJSON ''"\u${code}"'';
  powerline_symbol = unicodeChar "e0b0";
  rpowerline_symbol = unicodeChar "e0b2";
  colors = config.lib.stylix.colors.withHashtag;
  bright-yellow = config.lib.stylix.colors.yellow or config.lib.stylix.colors.base0a;
  bright-yellow-hashtag = "#${bright-yellow}";
  segment_style = {
    style = "diamond";
    leading_diamond = "";
    trailing_diamond = powerline_symbol;
  };
  rsegment_style = {
    style = "diamond";
    leading_diamond = rpowerline_symbol;
    trailing_diamond = "";
  };
in
{
  options.khscodes.khs.shell.oh-my-posh = {
    enable = lib.mkEnableOption "Enables oh my posh khs setup";
  };
  config = lib.mkIf cfg.enable {
    programs.oh-my-posh = {
      enable = true;
      enableBashIntegration = true;
      enableZshIntegration = true;
      enableFishIntegration = true;
      enableNushellIntegration = true;
      useTheme = null;
      settings = {
        "$schema" = "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json";
        "console_title_template" = "{{ .PWD }} @ {{ .HostName }}";
        "blocks" = [
          {
            "alignment" = "left";
            "type" = "prompt";
            "segments" = [
              {
                type = "shell";
                style = "diamond";
                leading_diamond = "";
                trailing_diamond = powerline_symbol;
                "template" = "{{ .Name }}";
                "background" = colors.base00;
                "foreground" = colors.blue;
                "background_templates" = [ "{{ if eq .Name \"🐠\" }}${colors.base00}{{ end }}" ];
                properties = {
                  mapped_shell_names = {
                    "nushell" = "nu";
                    "fish" = "🐟";
                    "bash" = "$_";
                    "zsh" = "%_";
                  };
                };
              }
              (
                {
                  "type" = "session";
                  "background" = colors.red;
                  "foreground" = colors.base07;
                  "template" = "{{ if .SSHSession }} {{ .HostName }} {{ end }}";
                }
                // segment_style
              )
              (
                {
                  "type" = "path";
                  "background" = colors.blue;
                  "foreground" = colors.base00;
                  "template" = " {{ .Path }} ";
                  "properties" = {
                    "style" = "full";
                  };
                }
                // segment_style
              )
              (
                {
                  "type" = "git";
                  "background" = colors.green;
                  "foreground" = colors.base00;
                  "background_templates" = [
                    "{{ if or (.Working.Changed) (.Staging.Changed) }}${colors.yellow}{{ end }}"
                  ];
                  "template" =
                    "{{ if .Detached }}➦ {{ trunc 7 .Commit.Sha }}{{ else }}{{ .UpstreamIcon }} {{ .Ref }}{{ end }}{{ if .Merge }}|merge{{ end }}{{ if .Rebase }}|rebase{{ end }}{{ if .CherryPick }}|cherrypick{{ end }}{{ if .Ahead}}↑{{ .Ahead }}{{ end }}{{ if .Behind }}↓{{ .Behind }}{{ end }}{{ if .Working.Changed}}●{{ end }}{{ if .Staging.Changed }}✚{{ end }}{{ if .StashCount }} 󰺿{{ end }} ";
                  "properties" = {
                    "fetch_status" = true;
                    "untracked_modes" = {
                      "/Users/user/Projects/oh-my-posh/" = "no";
                    };
                    fetch_upstream_icon = true;
                    upstream = {
                      git_icon = "";
                    };
                    "source" = "cli";
                  };
                }
                // segment_style
              )
            ];
          }
          {
            alignment = "right";
            type = "rprompt";
            "segments" = [
              (
                {
                  type = "status";
                  background = colors.base01;
                  background_templates = [ "{{ if .Error }}${colors.red}{{ end }}" ];
                  foreground = colors.green;
                  foreground_templates = [ "{{ if .Error}}${bright-yellow-hashtag}{{ end }}" ];
                  template = " {{ if .Error }}✘ {{ .Code }}{{ else }}✔{{ end }} ";
                  properties = {
                    always_enabled = true;
                  };
                }
                // rsegment_style
              )
              (
                {
                  type = "executiontime";
                  foreground = colors.base00;
                  background = colors.yellow;
                  template = " ⌚{{ .FormattedMs }} ";
                  properties = {
                    always_enabled = false;
                    threshold = 3000;
                    style = "round";
                  };
                }
                // rsegment_style
              )
              # Rendering this screws up spacing of the beginning of the prompt
              (
                {
                  type = "nix-shell";
                  background = colors.blue;
                  foreground = colors.base00;
                  template = " {{ .Type }} ";
                }
                // rsegment_style
              )
            ];
          }
        ];
        "terminal_background" = colors.base00;
        "disable_notice" = true;
        "final_space" = true;
        "version" = 2;
      };
    };
  };
}
