{ config, lib, ... }:

with lib;

let
  cfg = config.settings.containers.stalwart;
in {
  options = {
    settings.containers.stalwart.enable = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = ''
        Enable stalwart mailserver container
      '';
    };
  };

  config = mkIf cfg.enable {
    services.podman.containers.stalwart = {
      image = "stalwartlabs/mail-server:latest";
      network = "proxy";
      ports = [
        "1025:25"
        "1587:587"
        "1465:465"
        "1143:143"
        "1993:993"
        "14190:4190"
        "1110:110"
        "1995:995"
      ];
      volumes = [
        "%h/containers/stalwart/data:/opt/stalwart-mail" 
      ];
    };

    settings.containers.caddy.routes.tbmrs.routes.stalwart = {
      host = "mail";
      url = "stalwart:8080"; 
    };
  };
}
