{ pkgs, config, lib, ... }:
let
  p = config.colorScheme.palette;

  hexDigits = {
    "0"=0; "1"=1; "2"=2; "3"=3; "4"=4; "5"=5; "6"=6; "7"=7;
    "8"=8; "9"=9; "a"=10; "b"=11; "c"=12; "d"=13; "e"=14; "f"=15;
  };
  hexByte = h:
    hexDigits.${lib.toLower (builtins.substring 0 1 h)} * 16 +
    hexDigits.${lib.toLower (builtins.substring 1 1 h)};
  rgb = hex:
    let r = toString (hexByte (builtins.substring 0 2 hex));
        g = toString (hexByte (builtins.substring 2 2 hex));
        b = toString (hexByte (builtins.substring 4 2 hex));
    in "${r};${g};${b}";

  #  in a JSON string → ESC; JSON custom module format uses this directly
  blk  = c: "\\u001b[38;2;${rgb c}m\\ue0b7●\\ue0b5 \\u001b[0m";
  row  = cs: " " + builtins.concatStringsSep "" (map blk cs);

  base = row [ p.base00 p.base01 p.base02 p.base03 p.base04 p.base05 p.base06 p.base07 ];
  alt  = row [ p.alt00  p.alt01  p.alt02  p.alt03  p.alt04  p.alt05  p.alt06  p.alt07  ];
  high = row [ p.high08 p.high09 p.high0A p.high0B p.high0C p.high0D p.high0E p.high0F ];
  low  = row [ p.low08  p.low09  p.low0A  p.low0B  p.low0C  p.low0D  p.low0E  p.low0F  ];

  # ── logo colors ── change $1 / $2 to any palette entry ───────────────────
  logoColor1 = p.base07;
  logoColor2 = p.base07;
  # ─────────────────────────────────────────────────────────────────────────
in {
  home.packages = with pkgs; [ fastfetch ];
  xdg.configFile."neofetch/config.conf".source = ./config.conf;
  xdg.configFile."fastfetch/logo.txt".source = ./logo.txt;
  xdg.configFile."fastfetch/config.jsonc".text = ''
    {
        "$schema": "https://github.com/fastfetch-cli/fastfetch/raw/dev/doc/json_schema.json",
        "logo": {
            "type": "file",
            "source": "~/.config/fastfetch/logo.txt",
            "color": {
                "1": "38;2;${rgb logoColor1}",
                "2": "38;2;${rgb logoColor2}"
            },
            "padding": {
                "top": 1,
                "right": 4
            }
        },
        "display": {
            "separator": "  ",
            "key": { "width": 16 },
            "color": {
                "separator": "90"
            }
        },
        "modules": [
            {
                "type": "title",
                "colorUser": "blue",
                "colorAt": "90",
                "colorHost": "cyan"
            },
            "separator",
            {
                "type": "os",
                "key": "󱄅  os",
                "keyColor": "blue",
                "format": "{2} {8}"
            },
            {
                "type": "kernel",
                "key": "󰌽  kernel",
                "keyColor": "blue",
                "format": "{2}"
            },
            {
                "type": "packages",
                "key": "󰏗  pkgs",
                "keyColor": "blue"
            },
            {
                "type": "uptime",
                "key": "󰅐  uptime",
                "keyColor": "blue"
            },
            "break",
            {
                "type": "cpu",
                "key": "󱛠  cpu",
                "keyColor": "green",
                "format": "{1} ({4}t @ {6})"
            },
            {
                "type": "gpu",
                "key": "󰍹  gpu",
                "keyColor": "green",
                "format": "{2} [{6}]"
            },
            {
                "type": "memory",
                "key": "󰑭  ram",
                "keyColor": "green"
            },
            {
                "type": "disk",
                "key": "󰋊  disk",
                "keyColor": "green",
                "folders": "/"
            },
            "break",
            {
                "type": "wm",
                "key": "󱂬  wm",
                "keyColor": "yellow",
                "format": "{2}"
            },
            {
                "type": "terminal",
                "key": "󰆍  terminal",
                "keyColor": "yellow"
            },
            {
                "type": "shell",
                "key": "󱆃  shell",
                "keyColor": "yellow"
            },
            "break",
            {
                "type": "command",
                "key": "󰃑  installed",
                "keyColor": "magenta",
                "text": "birth=$(stat -c %W /); echo \"$(( ($(date +%s) - birth) / 86400 )) days\""
            },
            {
                "type": "command",
                "key": "󰜅  generation",
                "keyColor": "magenta",
                "text": "nixos-rebuild list-generations | awk '$NF == \"True\" {print $2, $3}' | xargs -I {} date -d \"{}\" +\"%s\" | awk '{diff=systime()-$1; if(diff<3600) printf \"%d mins\\n\", diff/60; else if(diff<86400) printf \"%.0f hours\\n\", diff/3600; else printf \"%.0f days\\n\", diff/86400}'"
            },
            "break",
            { "type": "custom", "format": "${base}" },
            { "type": "custom", "format": "${alt}"  },
            { "type": "custom", "format": "${high}" },
            { "type": "custom", "format": "${low}"  }
        ]
    }
  '';
}
