# ============================================================================
# GitHub Actions Workflow für Docker Hub Publishing
# ============================================================================

name: Docker Build and Publish

on:
  push:
    branches:
      - main
      - master
    tags:
      - 'v*'
  pull_request:
    branches:
      - main
      - master
  # Manuelle Ausführung
  workflow_dispatch:
    inputs:
      proton_version:
        description: 'Proton-GE Version (z.B. GE-Proton9-20)'
        required: false
        default: 'GE-Proton9-20'

env:
  REGISTRY: docker.io
  IMAGE_NAME: ${{ github.repository_owner }}/palworld-proton-server
  DEFAULT_PROTON_VERSION: GE-Proton9-20

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      # ============================================================================
      # Checkout
      # ============================================================================
      - name: Checkout repository
        uses: actions/checkout@v4

      # ============================================================================
      # Docker Buildx Setup (für Multi-Arch)
      # ============================================================================
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      # ============================================================================
      # Docker Hub Login
      # ============================================================================
      - name: Log in to Docker Hub
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      # ============================================================================
      # Metadata extrahieren
      # ============================================================================
      - name: Extract metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
          tags: |
            type=ref,event=branch
            type=ref,event=pr
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}
            type=sha,prefix=,suffix=,format=short
            # Latest nur für main/master
            type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') || github.ref == format('refs/heads/{0}', 'master') }}

      # ============================================================================
      # Proton-Version bestimmen
      # ============================================================================
      - name: Determine Proton Version
        id: proton
        run: |
          if [ -n "${{ github.event.inputs.proton_version }}" ]; then
            echo "version=${{ github.event.inputs.proton_version }}" >> $GITHUB_OUTPUT
          else
            echo "version=${{ env.DEFAULT_PROTON_VERSION }}" >> $GITHUB_OUTPUT
          fi

      # ============================================================================
      # Docker Image bauen und pushen
      # ============================================================================
      - name: Build and push Docker image
        uses: docker/build-push-action@v5
        with:
          context: ./docker
          file: ./docker/Dockerfile
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          platforms: linux/amd64
          build-args: |
            PROTON_VERSION=${{ steps.proton.outputs.version }}
            STEAM_APP_ID=2394010
          cache-from: type=gha
          cache-to: type=gha,mode=max

      # ============================================================================
      # Image-Details ausgeben
      # ============================================================================
      - name: Output image details
        run: |
          echo "Image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}"
          echo "Tags: ${{ steps.meta.outputs.tags }}"
          echo "Proton Version: ${{ steps.proton.outputs.version }}"

  # ============================================================================
  # Optional: Docker Scout für Sicherheits-Scanning
  # ============================================================================
  security-scan:
    runs-on: ubuntu-latest
    needs: build-and-push
    if: github.event_name != 'pull_request'
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Run Docker Scout
        uses: docker/scout-action@v1
        with:
          command: quickview,cves
          image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
          only-severities: critical,high
          write-comment: false
