{ lib, namespace, ... }:
with lib;
let
  inherit (types)
    str
    int
    bool
    listOf
    attrs
    path
    nullOr
    submodule
    ;
in
{
  options.${namespace}.programs.waybar = {
    enable = mkEnableOption "Waybar status bar";

    # Legacy/compat options (kept for backwards compatibility)
    layer = mkOption {
      type = str;
      default = "top";
      description = "Waybar layer (compat). Prefer layout + feature flags.";
    };

    extraModules = mkOption {
      type = attrs;
      default = { };
      description = "Extra settings bars at top-level (compat with older module).";
    };

    extraModulesStyle = mkOption {
      type = str;
      default = "";
      description = "Extra CSS appended (compat). Prefer extra.style.";
    };

    windowOffset = mkOption {
      type = int;
      default = 4;
      description = "Right margin offset for the hyprland/window module (in rem).";
    };

    # Layout
    layout = mkOption {
      type = submodule {
        options = {
          left = mkOption {
            type = listOf str;
            default = [ "hyprland/workspaces" ];
            description = "Modules shown on the left.";
          };
          center = mkOption {
            type = listOf str;
            default = [ "hyprland/window" ];
            description = "Modules shown in the center.";
          };
          right = mkOption {
            type = listOf str;
            default = [
              "tray"
              "custom/left-end"
              "temperature"
              "temperature#gpu"
              "keyboard-state#capslock"
              "keyboard-state#numlock"
              "wireplumber#sink"
              "bluetooth"
              "network"
              "idle_inhibitor"
              "custom/right-end"
              "custom/left-end"
              "clock"
              "battery"
              "custom/notifications"
              "custom/weather"
              "custom/power"
              "custom/right-end"
            ];
            description = "Modules shown on the right.";
          };
        };
      };
      default = { };
      description = "Waybar module layout.";
    };

    # Network
    network = mkOption {
      type = submodule {
        options = {
          interface = mkOption {
            type = str;
            default = "wlan0";
            description = "Primary network interface name.";
          };
        };
      };
      default = { };
      description = "Network configuration.";
    };

    # Temperatures
    temperature = mkOption {
      type = submodule {
        options = {
          cpu = mkOption {
            type = submodule {
              options = {
                enable = mkOption {
                  type = bool;
                  default = true;
                  description = "Enable CPU temperature module.";
                };
                hwmonPath = mkOption {
                  type = str;
                  default = "/sys/devices/pci0000:00/0000:00:18.3/hwmon";
                  description = "CPU temperature hwmon path.";
                };
                hwmonFile = mkOption {
                  type = str;
                  default = "temp1_input";
                  description = "CPU temperature hwmon file.";
                };
              };
            };
            default = { };
          };

          gpu = mkOption {
            type = submodule {
              options = {
                enable = mkOption {
                  type = bool;
                  default = true;
                  description = "Enable GPU temperature module.";
                };
                hwmonPath = mkOption {
                  type = str;
                  default = "/sys/devices/pci0000:00/0000:00:01.1/0000:01:00.0/0000:02:00.0/0000:03:00.0/hwmon";
                  description = "GPU temperature hwmon path.";
                };
                hwmonFile = mkOption {
                  type = str;
                  default = "temp1_input";
                  description = "GPU temperature hwmon file.";
                };
              };
            };
            default = { };
          };
        };
      };
      default = { };
      description = "Temperature module configuration.";
    };

    # Features
    features = mkOption {
      type = submodule {
        options = {
          tray = mkOption {
            type = submodule {
              options = {
                enable = mkOption {
                  type = bool;
                  default = true;
                };
              };
            };
            default = { };
          };

          bluetooth = mkOption {
            type = submodule {
              options = {
                enable = mkOption {
                  type = bool;
                  default = true;
                };
              };
            };
            default = { };
          };

          idleInhibitor = mkOption {
            type = submodule {
              options = {
                enable = mkOption {
                  type = bool;
                  default = true;
                };
              };
            };
            default = { };
          };

          keyboardIndicators = mkOption {
            type = submodule {
              options = {
                enable = mkOption {
                  type = bool;
                  default = true;
                };
              };
            };
            default = { };
          };

          audio = mkOption {
            type = submodule {
              options = {
                sink = mkOption {
                  type = submodule {
                    options = {
                      enable = mkOption {
                        type = bool;
                        default = true;
                      };
                    };
                  };
                  default = { };
                };
                source = mkOption {
                  type = submodule {
                    options = {
                      enable = mkOption {
                        type = bool;
                        default = false;
                      };
                    };
                  };
                  default = { };
                };
              };
            };
            default = { };
          };

          weather = mkOption {
            type = submodule {
              options = {
                enable = mkOption {
                  type = bool;
                  default = true;
                };
              };
            };
            default = { };
          };

          hass = mkOption {
            type = submodule {
              options = {
                enable = mkOption {
                  type = bool;
                  default = false;
                };
              };
            };
            default = { };
          };

          clock = mkOption {
            type = submodule {
              options = {
                enable = mkOption {
                  type = bool;
                  default = true;
                };
              };
            };
            default = { };
          };

          battery = mkOption {
            type = submodule {
              options = {
                enable = mkOption {
                  type = bool;
                  default = true;
                };
              };
            };
            default = { };
          };
        };
      };
      default = { };
      description = "Toggle optional Waybar features.";
    };

    # Styling
    style = mkOption {
      type = submodule {
        options = {
          file = mkOption {
            type = nullOr path;
            default = null;
            description = "Optional external CSS file to use instead of the inline style.";
          };
          fragmentsDir = mkOption {
            type = nullOr path;
            default = null;
            description = "Optional directory of CSS fragments to append.";
          };
        };
      };
      default = { };
      description = "Styling configuration.";
    };

    # Extra overrides
    extra = mkOption {
      type = submodule {
        options = {
          settings = mkOption {
            type = attrs;
            default = { };
            description = "Extra settings merged into settings.mainBar.";
          };
          style = mkOption {
            type = str;
            default = "";
            description = "Extra CSS appended to the computed style.";
          };
        };
      };
      default = { };
      description = "Extra settings/style hooks.";
    };
  };
}
