-- Per-game shutdown controls and container mount overrides.
ALTER TABLE "games"
  ADD COLUMN IF NOT EXISTS "stop_timeout_seconds" integer NOT NULL DEFAULT 30;

ALTER TABLE "games"
  ADD COLUMN IF NOT EXISTS "container_data_path" text;

-- Mount points the daemon previously derived from the image name. Storing them
-- makes the mapping visible and editable instead of hardcoded.
UPDATE "games" SET "container_data_path" = '/home/steam/cs2-dedicated' WHERE "slug" = 'cs2' AND "container_data_path" IS NULL;
UPDATE "games" SET "container_data_path" = '/config' WHERE "slug" IN ('fivem', 'satisfactory') AND "container_data_path" IS NULL;
UPDATE "games" SET "container_data_path" = '/data' WHERE "slug" IN ('minecraft-java', 'minecraft-bedrock', 'terraria', 'rust') AND "container_data_path" IS NULL;

-- Shutdown budgets. Source servers quit instantly once they get the command;
-- ARK and Satisfactory need to flush a world save first.
UPDATE "games" SET "stop_timeout_seconds" = 60 WHERE "slug" IN ('minecraft-java', 'minecraft-bedrock');
UPDATE "games" SET "stop_timeout_seconds" = 120 WHERE "slug" = 'satisfactory';

-- ARK: Survival Evolved
INSERT INTO "games" (
  "slug",
  "name",
  "docker_image",
  "default_port",
  "config_files",
  "automation_rules",
  "startup_command",
  "stop_command",
  "stop_timeout_seconds",
  "container_data_path",
  "environment_vars",
  "created_at",
  "updated_at"
)
VALUES (
  'ark-se',
  'ARK: Survival Evolved',
  'hermsi/ark-server:latest',
  7777,
  '[
    {
      "path": "server/ShooterGame/Saved/Config/LinuxServer/GameUserSettings.ini",
      "parser": "properties",
      "editableKeys": [
        "SessionName",
        "ServerPassword",
        "ServerAdminPassword",
        "MaxPlayers",
        "ServerPVE",
        "ServerCrosshair",
        "ServerHardcore",
        "AllowThirdPersonPlayer",
        "ShowMapPlayerLocation",
        "GlobalVoiceChat",
        "ProximityChat",
        "NoTributeDownloads",
        "AllowAnyoneBabyImprintCuddle",
        "DifficultyOffset",
        "XPMultiplier",
        "TamingSpeedMultiplier",
        "HarvestAmountMultiplier",
        "DayCycleSpeedScale",
        "NightTimeSpeedScale",
        "PlayerCharacterWaterDrainMultiplier",
        "PlayerCharacterFoodDrainMultiplier",
        "StructureDamageMultiplier",
        "StructureResistanceMultiplier",
        "RCONEnabled",
        "RCONPort"
      ]
    },
    {
      "path": "server/ShooterGame/Saved/Config/LinuxServer/Game.ini",
      "parser": "properties"
    }
  ]'::jsonb,
  '[]'::jsonb,
  '',
  '',
  300,
  '/app',
  '[
    {
      "key": "SESSION_NAME",
      "default": "SourceGamePanel ARK Server",
      "description": "Server name shown in the ARK server browser",
      "required": true
    },
    {
      "key": "SERVER_MAP",
      "default": "TheIsland",
      "description": "Map to load (TheIsland, TheCenter, Ragnarok, Valguero, CrystalIsles, ...)",
      "required": true
    },
    {
      "key": "ADMIN_PASSWORD",
      "default": "",
      "description": "Admin/RCON password. Required — RCON console commands use it.",
      "required": true
    },
    {
      "key": "SERVER_PASSWORD",
      "default": "",
      "description": "Password players need to join. Leave empty for a public server.",
      "required": false
    },
    {
      "key": "MAX_PLAYERS",
      "default": "20",
      "description": "Maximum player count",
      "required": false
    },
    {
      "key": "UPDATE_ON_START",
      "label": "Update on start",
      "default": "false",
      "description": "Run a SteamCMD update every time the server starts",
      "required": false,
      "inputType": "boolean",
      "enabledLabel": "Aktif",
      "disabledLabel": "Pasif"
    },
    {
      "key": "BACKUP_ON_STOP",
      "label": "Backup on stop",
      "default": "false",
      "description": "Create a world backup during shutdown (makes stopping slower)",
      "required": false,
      "inputType": "boolean",
      "enabledLabel": "Aktif",
      "disabledLabel": "Pasif"
    },
    {
      "key": "WARN_ON_STOP",
      "label": "Warn players on stop",
      "default": "false",
      "description": "Broadcast a shutdown warning to connected players before stopping",
      "required": false,
      "inputType": "boolean",
      "enabledLabel": "Aktif",
      "disabledLabel": "Pasif"
    },
    {
      "key": "PRE_UPDATE_BACKUP",
      "label": "Backup before update",
      "default": "true",
      "description": "Back the world up before applying a SteamCMD update",
      "required": false,
      "inputType": "boolean",
      "enabledLabel": "Aktif",
      "disabledLabel": "Pasif"
    }
  ]'::jsonb,
  NOW(),
  NOW()
)
ON CONFLICT ("slug") DO NOTHING;
