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

  config = lib.mkIf cfg.enable {
    services.grocy = {
      enable = true;
      hostName = cfg.hostname;
      dataDir = cfg.dataDir;
    };

    services.nginx.virtualHosts.${cfg.hostname} = {
      listen = [{
        addr = "127.0.0.1";
        port = 8303;
      }];
      serverName = null;
      enableACME = lib.mkForce false;
      forceSSL = lib.mkForce false;
    };
    services.cloudflared.tunnels."4fc85af1-38f7-4c96-856c-7c797c5e3bf8".ingress.${cfg.hostname} = "http://127.0.0.1:8303";
  };
}
