# echo 81 | sudo tee /sys/class/power_supply/macsmc-battery/charge_control_end_threshold
{
  config,
  pkgs,
  lib,
  namespace,
  ...
}:
with lib;
let
  inherit (lib.${namespace}) mkOpt;
  cfg = config.${namespace}.hardware.battery;
in
{
  options.${namespace}.hardware.battery = {
    enable = mkEnableOption "battery charge limit service";

    chargeLimit = mkOpt types.int 81 "Charge limit for the battery, ex: 81 = 80% charge limit";

    battery = mkOpt types.str "" "Path to the battery charge limit file";
  };

  config = mkIf cfg.enable {
    systemd = {
      services = {
        set-charge-limit = {
          enable = true;
          wantedBy = [ "multi-user.target" ];
          path = [
            pkgs.uutils-coreutils-noprefix
          ];
          script = ''
            ${pkgs.uutils-coreutils-noprefix}/bin/echo ${toString cfg.chargeLimit} | tee ${cfg.battery}
          '';
          serviceConfig = {
            Type = "oneshot";
            TimeoutSec = "5s";
          };
        };
      };
    };
  };
}
