# WarpEngine host configuration. to_prepare: re-runs after reloads, hence
# assignment (not <<) to stay idempotent.
Rails.application.config.to_prepare do
  WarpEngine.configure do |c|
    # Owner of the /build/* DB tokens. Switching to :database mode
    # (c.application_token_source = :database) must wait until CI uses DB
    # tokens — the flip invalidates UPDATE_SECRET immediately.
    c.application_token_source = :database
    c.application_token_owner_class = "AdminUser"

    # Woodpecker configuration extension (/build/config): the served platforms
    # and their builder images. An image bump is one line here, rolled out to
    # every repo by the deploy.
    c.ci_platforms = {
      "godot"      => { builder: "git.teletypegames.org/build/godot-builder:4.7.1" },
      "phaser"     => { builder: "git.teletypegames.org/build/phaser-builder:latest" },
      "love"       => { builder: "git.teletypegames.org/build/love-builder:latest" },
      "bevy"       => { builder: "git.teletypegames.org/build/bevy-builder:latest" },
      "c64"        => { builder: "git.teletypegames.org/build/c64-builder:latest" },
      "ebitengine" => { builder: "git.teletypegames.org/build/ebitengine-builder:latest" },
      "tic80"      => { builder: "git.teletypegames.org/build/tic80pro:latest" }
    }
    # Explicit URL: request.base_url would yield http:// behind the host nginx
    # (no X-Forwarded-Proto reaches Rails), and the resulting 301 makes the
    # pipeline's curl steps silently no-op.
    c.ci_update_server = "https://teletypegames.org"
    c.ci_extension_public_key_url = "https://ci.teletypegames.org/api/signature/public-key"

    # Woodpecker CI management (repo sync, secret provisioning, pipeline control)
    c.woodpecker_url        = ENV["WOODPECKER_URL"]        # e.g. "https://ci.teletypegames.org"
    c.woodpecker_api_token  = ENV["WOODPECKER_API_TOKEN"]  # Woodpecker PAT with admin access
    c.woodpecker_repo_owner = ENV["WOODPECKER_REPO_OWNER"] # forge org (e.g. "games")

    c.image_owners = [
      {
        label: "member",
        image_ids: -> { Member.where.not(image_id: nil).distinct.pluck(:image_id) },
        usage_label: ->(image) { "member" if Member.where(image_id: image.id).exists? }
      }
    ]
  end
end
