{ config, lib, ... }:

# File-based override system for Nomarchy
#
# Users can place config files in ~/.config/nomarchy/overrides/ to completely
# replace upstream defaults. Override priority (highest to lowest):
# 1. User Nix options
# 2. User file overrides (~/.config/nomarchy/overrides/)
# 3. Upstream defaults
#
# Supported override paths:
# - hypr/             - Hyprland configs (bindings.conf, input.conf, etc.)
# - waybar/           - Waybar config and style
# - alacritty/        - Alacritty terminal config
# - walker/           - Walker launcher config
# - kitty/            - Kitty terminal config
# - btop/             - Btop resource monitor config
# - apps/             - Other application configs

let
  overridesDir = "${config.home.homeDirectory}/.config/nomarchy/overrides";

  # Check if a specific override exists
  hasOverride = path: builtins.pathExists "${overridesDir}/${path}";

  # Get override source if it exists, otherwise use default
  getOverrideOrDefault = { path, default }:
    if hasOverride path
    then "${overridesDir}/${path}"
    else default;

in
{
  options.nomarchy.overrides = {
    enable = lib.mkOption {
      type = lib.types.bool;
      default = true;
      description = "Whether to enable file-based override loading from ~/.config/nomarchy/overrides/";
    };

    paths = lib.mkOption {
      type = lib.types.attrsOf lib.types.path;
      default = {};
      description = "Override paths discovered at build time. Populated by the override system.";
    };
  };

  config = lib.mkIf config.nomarchy.overrides.enable {
    # Create the overrides directory structure if it doesn't exist
    home.activation.createOverridesDir = lib.hm.dag.entryAfter [ "writeBoundary" ] ''
      mkdir -p "${overridesDir}"
      mkdir -p "${overridesDir}/hypr"
      mkdir -p "${overridesDir}/waybar"
      mkdir -p "${overridesDir}/apps"
    '';

    # Document the override system
    xdg.configFile."nomarchy/overrides/README.md".text = lib.mkDefault ''
      # Nomarchy Configuration Overrides

      Place files in this directory to override upstream Nomarchy defaults.

      ## Directory Structure

      ```
      overrides/
      ├── hypr/
      │   ├── bindings.conf    # Keybindings
      │   ├── input.conf       # Input settings
      │   ├── monitors.conf    # Monitor layout
      │   ├── rules.conf       # Window rules
      │   └── autostart.conf   # Startup apps
      ├── waybar/
      │   ├── config.jsonc     # Waybar layout
      │   └── style.css        # Waybar styling
      ├── apps/
      │   ├── alacritty.toml   # Terminal behavior
      │   └── ...
      └── README.md            # This file
      ```

      ## Override Priority

      1. **Nix Options** (highest) - Set in your flake/config
      2. **File Overrides** - Files in this directory
      3. **Upstream Defaults** (lowest) - Nomarchy defaults

      ## Usage

      1. Copy the file you want to customize from the upstream config
      2. Place it in the appropriate directory here
      3. Edit to your preferences
      4. Run `nixos-rebuild switch` or `home-manager switch`

      ## Tips

      - For keybindings, copy `~/.config/hypr/bindings.conf` to `overrides/hypr/`
      - For Waybar styling, copy `~/.config/waybar/style.css` to `overrides/waybar/`
      - Changes here persist across Nomarchy updates
    '';
  };
}
