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

let
  cfg = config.services.owOPointTracker;
  format = pkgs.formats.yaml {};
  configFile = format.generate "settings.yml" cfg.config;
  databaseOptionType = types.submodule {
	  options = {
			database = mkOption {
				default = "_env:YESOD_SQLITE_DATABASE:OwOpointTracker.sqlite3";
				type = types.str;
			};
			poolsize = mkOption {
				default = "_env:YESOD_SQLITE_POOLSIZE:10";
				type = types.str;
			};
	  };
  };
  stationType = types.submodule {
	  options = {
		  station-id = mkOption {
			  default = "Station";
			  type = types.str;
		  };
		  station-name = mkOption {
			  types = types.str;
		  };
	  };
  };
{
	imports = [];
	options.services.owOPointTracker = {
		enable = mkEnableOption "OwOPointTracker";
		config = {
			static-dir = mkOption {
				type = types.path;
			};
			host = mkOption {
				type = types.str;
				default = "_env:YESOD_HOST:*4";
			ad-files = mkOption {
				type = types.path;
			};
			port = mkOption {
				default = "_env:YESOD_PORT:3000";
				type = types.str;
			};
			ip-from-header = mkOption {
				default = "_env:YESOD_IP_FROM_HEADER:false";
				type = types.str;
			};
			database = {
				type = databaseOptionType;
			};
			copyright = mkOption {
				default = "copyright for this mess?";
				type = types.str;
			};
			station = mkOption {
				type = types.listOf stationType;
			};
			
		};
		
	};
	config = mkIf cfg.enable {
		users.users.owopoint = {
			isSystemUser = true;
			group = "owopoint";
			createHome = true;
			home = "/var/owopoint";
		};
		users.groups = {
			owopoint = {};
		};
		systemd.services.owOPointTracker = {
			wantedBy = ["multi-user.target"];
			after = ["network.target"];
			serviceConfig = {
				ExecStart = "${self.packages.${pkgs.system}.default} ${configFile}";
				Restart = always;
				User = "owopoint";
				Group = "owopoint";
			};
		};
			
	};
}
