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

{
  services.postgresql = {
    enable = true;
    ensureDatabases = [ "photoprism" ];
    ensureUsers = [{
      name = "photoprism";
      ensureDBOwnership = true;
    }];
  };

  services.photoprism = {
    enable = true;
    package = pkgs.unstable.photoprism;
    port = 5152;
    originalsPath = "/var/lib/private/photoprism/originals";
    address = "0.0.0.0";
    settings = {
      PHOTOPRISM_ADMIN_USER = "admin";
      PHOTOPRISM_ADMIN_PASSWORD = "photoprism"; # initial password, do change it!
      PHOTOPRISM_DEFAULT_LOCALE = "en";
      PHOTOPRISM_DATABASE_DRIVER = "postgres";
      PHOTOPRISM_DATABASE_NAME = "photoprism";
      PHOTOPRISM_DATABASE_SERVER = "/run/postgresql/";
      PHOTOPRISM_DATABASE_USER = "photoprism";
      PHOTOPRISM_SITE_URL = "http://photos.mossnet.lan/";
      PHOTOPRISM_SITE_TITLE = "mossnet photos";
    };
  };

  services.nginx.clientMaxBodySize = "500m";
  services.nginx.recommendedProxySettings = true;
  services.nginx.virtualHosts."photos.mossnet.lan" = {
    enableACME = false;
    forceSSL = false;
    locations."/" = {
      extraConfig = ''
        proxy_pass http://localhost:5152/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_buffering off;
        proxy_http_version 1.1;
      '';
    };
  };
}
