{ config, lib, pkgs, ... }:

let
  cfg = config.stylePreview;

  styleScript = pkgs.writeShellScriptBin "style" ''
    # Display current Stylix base16 theme colors
    PALETTE="$HOME/.config/stylix/palette.json"

    if [ ! -f "$PALETTE" ]; then
      echo "Error: Stylix palette not found at $PALETTE"
      exit 1
    fi

    # Extract scheme name
    SCHEME=$(${pkgs.jq}/bin/jq -r '.scheme // "Unknown"' "$PALETTE")
    echo -e "\033[1m$SCHEME\033[0m"
    echo

    # Display each base color with name and swatch
    for i in 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F; do
      hex=$(${pkgs.jq}/bin/jq -r ".base$i" "$PALETTE")
      r=$((16#''${hex:0:2}))
      g=$((16#''${hex:2:2}))
      b=$((16#''${hex:4:2}))
      printf "base%s \033[48;2;%d;%d;%dm    \033[0m #%s\n" "$i" "$r" "$g" "$b" "$hex"
    done
  '';
in
{
  options.stylePreview = {
    enable = lib.mkEnableOption "style command for previewing Stylix theme colors";
  };

  config = lib.mkIf cfg.enable {
    home.packages = [ styleScript ];
  };
}
