{ pkgs, lib, config, ... }:
let cfg = config.cos.mealie; in
{
  options.cos.mealie = {
    enable = lib.mkEnableOption "enable Mealie";
    hostname = lib.mkOption {
      type = lib.types.str;
    };
    port = lib.mkOption {
      type = lib.types.port;
    };
  };

  config = lib.mkIf cfg.enable {
    services.mealie = {
      enable = true;
      port = cfg.port;
      listenAddress = "127.0.0.1";
    };

    services.nginx = {
      enable = true;
      virtualHosts.${cfg.hostname} = {
        enableACME = true;
        forceSSL = true;
        locations."/" = {
          proxyPass = "http://127.0.0.1:${builtins.toString cfg.port}";
        };
      };
    };
  };
}
