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

# Theme Loader Module
#
# This module handles loading and deploying theme-specific application configs.
# It reads the active theme from state and deploys configs from the theme's apps/
# subdirectory to appropriate locations.
#
# Theme structure expected:
#   assets/themes/<theme-name>/
#   ├── colors.toml              # Color palette (required)
#   ├── light.mode               # Marker for light themes (optional)
#   ├── icons.theme              # Icon theme name
#   ├── backgrounds/             # Wallpapers
#   ├── apps/                    # App-specific themed configs
#   │   ├── alacritty.toml
#   │   ├── kitty.conf
#   │   ├── waybar.css
#   │   ├── hyprland-colors.conf
#   │   ├── mako.conf
#   │   ├── swayosd.css
#   │   ├── btop.theme
#   │   ├── vscode.json
#   │   └── neovim.lua
#   └── preview.png

let
  nomarchyLib = import ../../lib { inherit lib; };
  assetsPath = ../palettes;
  activeTheme = config.nomarchy.theme;
  themePath = assetsPath + "/${activeTheme}";
  themeAppsPath = themePath + "/apps";

  # Check if a theme has an apps directory
  themeHasApps = builtins.pathExists themeAppsPath;

  # Get the color palette for template processing
  palette = nomarchyLib.getPalette activeTheme;

  # Helper to check if a file exists in theme
  hasThemeFile = name: builtins.pathExists (themePath + "/${name}");
  hasThemeAppFile = name: themeHasApps && builtins.pathExists (themeAppsPath + "/${name}");

  # All app configs are now in apps/ subdirectory
  # Legacy root-level files are no longer supported

in
{
  options.nomarchy.themeLoader = {
    enable = lib.mkOption {
      type = lib.types.bool;
      default = true;
      description = "Whether to enable automatic theme app config loading.";
    };

    apps = {
      btop = lib.mkOption {
        type = lib.types.bool;
        default = true;
        description = "Whether to load btop theme from active theme.";
      };
      waybar = lib.mkOption {
        type = lib.types.bool;
        default = true;
        description = "Whether to load waybar CSS from active theme.";
      };
      mako = lib.mkOption {
        type = lib.types.bool;
        default = true;
        description = "Whether to load mako config from active theme.";
      };
      kitty = lib.mkOption {
        type = lib.types.bool;
        default = true;
        description = "Whether to load kitty config from active theme.";
      };
      alacritty = lib.mkOption {
        type = lib.types.bool;
        default = true;
        description = "Whether to load alacritty config from active theme.";
      };
    };
  };

  config = lib.mkIf config.nomarchy.themeLoader.enable {
    # Deploy btop theme if available in apps/
    xdg.configFile."btop/themes/nomarchy.theme" = lib.mkIf (config.nomarchy.themeLoader.apps.btop && hasThemeAppFile "btop.theme") {
      source = lib.mkDefault (themeAppsPath + "/btop.theme");
    };

    # Note: Waybar CSS is generated inline in waybar.nix using colorScheme
    # This loader would be used if themes provide complete waybar.css overrides
    # For now, theme waybar.css files are only used for themes that need
    # significantly different styling (like retro-82)

    # Create theme info file for scripts
    xdg.configFile."nomarchy/theme-loader/current".text = ''
      THEME_NAME="${activeTheme}"
      THEME_PATH="${toString themePath}"
      THEME_HAS_APPS="${if themeHasApps then "true" else "false"}"
      IS_LIGHT_MODE="${if config.nomarchy.isLightMode then "true" else "false"}"
      ICONS_THEME="${config.nomarchy.iconsTheme}"
    '';

    # Expose palette as shell-sourceable file for scripts
    xdg.configFile."nomarchy/theme-loader/palette.sh".text = ''
      # Auto-generated palette for ${activeTheme}
      # Source this file in scripts to get theme colors

      BASE00="${palette.base00}"
      BASE01="${palette.base01}"
      BASE02="${palette.base02}"
      BASE03="${palette.base03}"
      BASE04="${palette.base04}"
      BASE05="${palette.base05}"
      BASE06="${palette.base06}"
      BASE07="${palette.base07}"
      BASE08="${palette.base08}"
      BASE09="${palette.base09}"
      BASE0A="${palette.base0A}"
      BASE0B="${palette.base0B}"
      BASE0C="${palette.base0C}"
      BASE0D="${palette.base0D}"
      BASE0E="${palette.base0E}"
      BASE0F="${palette.base0F}"
    '';
  };
}
