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

with lib;

let
  cfg = config.power_manager;
in
{
  options.power_manager = {
    enable = mkEnableOption "power management with auto-cpufreq";

    settings = mkOption {
      type = types.attrs;
      default = {
        battery = {
          governor = "powersave";
          turbo = "never";
        };
        charger = {
          governor = "performance";
          turbo = "auto";
        };
      };
      description = "Auto-cpufreq configuration settings";
    };

    enableThermald = mkOption {
      type = types.bool;
      default = true;
      description = "Enable thermald for Intel CPU thermal management";
    };

    enableUpower = mkOption {
      type = types.bool;
      default = true;
      description = "Enable UPower for battery monitoring";
    };
  };

  config = mkIf cfg.enable {
    services.auto-cpufreq = {
      enable = true;
      settings = cfg.settings;
    };

    # Supporting services
    services.upower.enable = mkIf cfg.enableUpower true;
    services.thermald.enable = mkIf cfg.enableThermald true;
    powerManagement.enable = mkIf cfg.enable true;

    # Disable conflicting power management services
    services.tlp.enable = mkForce false;
    services.power-profiles-daemon.enable = mkForce false;
    powerManagement.powertop.enable = mkForce false;
    
    # Don't set a static CPU governor (let auto-cpufreq handle it)
    powerManagement.cpuFreqGovernor = mkForce null;
  };

  # # Warning if flake input is missing when expected
  # warnings = optional (cfg.useFlake && !(inputs ? auto-cpufreq))
  #   "power_manager: useFlake is enabled but auto-cpufreq flake input not found. Using nixpkgs version instead.";
}
