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

let
  inherit (lib) mkOption mkIf mkMerge types;
  cfg = config.devtools;
in 
{
  options.devtools = {
    postgres.enable = mkOption {
      default = true;
      type = types.bool;
    };
    virtualisation.enable = mkOption {
      default = true;
      type = types.bool;
    };
    docker.enable = mkOption {
      default = true;
      type = types.bool;
    };
  };

  config = {
    services.postgresql = {
      enable = cfg.postgres.enable;
      package = pkgs.postgresql_17;
      ensureDatabases = [ "dpv" ];
      ensureUsers = [
        {
          name = "dpv";
          ensureDBOwnership = true;
        }
      ];
      authentication = lib.mkForce ''
        # Generated file; do not edit!
        # TYPE DATABASE USER ADDRESS     METHOD
        local  all      all              trust
        host   all      all 127.0.0.1/32 trust
        host   all      all ::1/128      trust
      '';
    };

    virtualisation = mkMerge [
      (mkIf cfg.virtualisation.enable {
        libvirtd = {
          enable = true;
          qemu = {
            swtpm.enable = true;
            ovmf.enable = true;
            ovmf.packages = [ pkgs.OVMFFull.fd ];
          };
        };
        spiceUSBRedirection.enable = true;
      })

      (mkIf cfg.docker.enable {
        docker = {
          enable = true;
          rootless = {
            enable = true;
            setSocketVariable = true;
          };
        };
      })
    ];

    programs.virt-manager.enable = cfg.virtualisation.enable;
  };
}
