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

let
  cfg = config.stylix;

  # all valid options for `cfg.scheme`
  validSchemes = [
    "ayu"
    "dracula"
    "gruvbox"
    "moonfly"
    "nord"
    "oxocarbon"
    "generate-from-image"
  ];
  # schemes names in `pkgs.base16-schemes` that need a suffix
  needsSuffix = [
    "ayu"
    "gruvbox"
  ];
  # schemes in ./schemes
  customSchemes = [
    "moonfly"
    "oxocarbon"
  ];
  schemeName =
    if builtins.elem cfg.scheme needsSuffix then "${cfg.scheme}-${cfg.polarity}" else cfg.scheme;

  inherit (lib)
    mkDefault
    mkIf
    types
    ;
in
{
  imports = [
    inputs.stylix.homeModules.stylix

    ./targets
  ];

  options.stylix = {
    scheme = lib.mkOption {
      type = types.str;
      default = "dracula";
      description = ''
        Base16 color scheme name. Available options are:
        ${toString validSchemes}

        "generate-from-image" generates a color scheme from `stylix.image`
      '';
    };
  };

  config = mkIf cfg.enable {
    assertions = [
      {
        assertion = builtins.elem cfg.scheme validSchemes;
        message = "Stylix: Invalid colorscheme '${cfg.scheme}'. Available options: ${toString validSchemes}";
      }
    ];

    stylix = {
      autoEnable = mkDefault true;
      base16Scheme = mkIf (cfg.scheme != "generate-from-image") (
        if builtins.elem cfg.scheme customSchemes then
          ./schemes/${schemeName}.yaml
        else
          "${pkgs.base16-schemes}/share/themes/${schemeName}.yaml"
      );
      fonts = {
        monospace = mkDefault {
          package = pkgs.hack-font;
          name = "Hack";
        };
      };
      polarity = mkDefault "dark";

      targets = {
        librewolf.profileNames = [ "default" ];
      };
    };

    home.packages = [
      (pkgs.callPackage ./print-colors { })
    ];
  };
}
