{
  inputs,
  config,
  lib,
  ...
}:
let
  cfg = config.sharedPrograms;
  unstable = import inputs.nixpkgs-unstable {
    system = "x86_64-linux";
    config.allowUnfree = true;
  };
in
{
  imports = [
    ../apps/firefox/firefox.nix
    ../apps/ghostty.nix
    ../apps/helix.nix
    ./json2nix.nix
    ./style-preview.nix
  ];

  options.sharedPrograms = {
    enable = lib.mkEnableOption "enable all shared programs with Stylix auto-theming";

    # CLI tools
    bat.enable = lib.mkEnableOption "enable bat (cat replacement)";
    fzf.enable = lib.mkEnableOption "enable fzf (fuzzy finder)";
    btop.enable = lib.mkEnableOption "enable btop (system monitor)";
    mpv.enable = lib.mkEnableOption "enable mpv (media player)";
    wofi.enable = lib.mkEnableOption "enable wofi (application launcher)";
    keepassxc.enable = lib.mkEnableOption "enable keepassxc (password manager)";
    opencode.enable = lib.mkEnableOption "enable opencode (AI coding assistant)";

    # Apps (pass-through to underlying modules)
    firefox.enable = lib.mkEnableOption "enable firefox (web browser)";

    ghostty = {
      enable = lib.mkEnableOption "enable ghostty (terminal emulator)";
      fontFamily = lib.mkOption {
        type = lib.types.str;
        default = "Maple Mono NF";
        description = "Font family for Ghostty";
      };
      enableLigatures = lib.mkOption {
        type = lib.types.bool;
        default = true;
        description = "Enable font ligatures in Ghostty";
      };
    };

    helix = {
      enable = lib.mkEnableOption "enable helix (text editor)";
      themeOverrides = lib.mkOption {
        type = lib.types.attrsOf lib.types.anything;
        default = { };
        description = "Override minimal theme scopes for helix";
      };
    };

    stylePreview.enable = lib.mkEnableOption "enable style preview (Stylix color viewer)";
    json2nix.enable = lib.mkEnableOption "enable json2nix (JSON to Nix converter)";
  };

  config = lib.mkMerge [
    # When sharedPrograms.enable is true, enable all programs by default
    (lib.mkIf cfg.enable {
      sharedPrograms = {
        bat.enable = lib.mkDefault true;
        fzf.enable = lib.mkDefault true;
        btop.enable = lib.mkDefault true;
        mpv.enable = lib.mkDefault true;
        wofi.enable = lib.mkDefault true;
        keepassxc.enable = lib.mkDefault true;
        opencode.enable = lib.mkDefault true;
        firefox.enable = lib.mkDefault true;
        ghostty.enable = lib.mkDefault true;
        helix.enable = lib.mkDefault true;
        stylePreview.enable = lib.mkDefault true;
        json2nix.enable = lib.mkDefault true;
      };
    })

    # CLI tools
    (lib.mkIf cfg.bat.enable {
      programs.bat.enable = true;
    })

    (lib.mkIf cfg.fzf.enable {
      programs.fzf = {
        enable = true;
        enableZshIntegration = true;
      };
    })

    (lib.mkIf cfg.btop.enable {
      programs.btop.enable = true;
    })

    (lib.mkIf cfg.mpv.enable {
      programs.mpv.enable = true;
    })

    (lib.mkIf cfg.wofi.enable {
      programs.wofi.enable = true;
    })

    (lib.mkIf cfg.opencode.enable {
      programs.opencode = {
        enable = true;
        package = unstable.opencode;
      };
    })

    (lib.mkIf cfg.stylePreview.enable {
      stylePreview.enable = true;
    })

    (lib.mkIf cfg.json2nix.enable {
      json2nix.enable = true;
    })

    (lib.mkIf cfg.helix.enable {
      helixApp = {
        enable = true;
        themeOverrides = cfg.helix.themeOverrides;
      };
    })

    
    # Apps — wire to underlying module options
    (lib.mkIf cfg.firefox.enable {
      firefoxApp.enable = true;
    })

    (lib.mkIf cfg.keepassxc.enable {
      programs.keepassxc = {
        enable = true;
        autostart = false;
        settings = {
          Browser = {
            Enabled = true;
            UpdateBinaryPath = false;
          };

          PasswordGenerator = {
            Length = 25;
            SpecialChars = false;
          };

          GUI = {
            AdvancedSettings = true;
            ApplicationTheme = "dark";
            CompactMode = true;
            HidePasswords = true;
          };

          Security = {
            ClearClipboardTimeout = 20;
            LockDatabaseIdleSeconds = 21600;
          };
        };
      };
    })

    (lib.mkIf cfg.ghostty.enable {
      ghosttyApp = {
        enable = true;
        fontFamily = cfg.ghostty.fontFamily;
        enableLigatures = cfg.ghostty.enableLigatures;
      };
    })

  ];
}
