name: Godot CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  workflow_dispatch:

env:
  GODOT_VERSION: "4.6.3"

jobs:
  validate:
    name: Import + smoke test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install deps
        run: |
          apt-get update -qq
          apt-get install -y -qq wget unzip ca-certificates
          rm -rf /var/lib/apt/lists/*

      - name: Download Godot ${{ env.GODOT_VERSION }} headless
        run: |
          cd /tmp
          wget -q "https://github.com/godotengine/godot/releases/download/${GODOT_VERSION}-stable/Godot_v${GODOT_VERSION}-stable_linux.x86_64.zip"
          unzip -q "Godot_v${GODOT_VERSION}-stable_linux.x86_64.zip"
          mv "Godot_v${GODOT_VERSION}-stable_linux.x86_64" /usr/local/bin/godot
          chmod +x /usr/local/bin/godot
          godot --version

      - name: Import project
        run: |
          godot --headless --import 2>&1 | tee /tmp/import.log
          if grep -E "SCRIPT ERROR|Parse Error|^ERROR" /tmp/import.log; then
            echo "::error::Import has errors"
            exit 1
          fi

      - name: Smoke test main scene (30 frames)
        run: |
          godot --headless --quit-after 30 2>&1 | tee /tmp/run.log
          if grep -E "SCRIPT ERROR|Parse Error|^ERROR" /tmp/run.log; then
            echo "::error::Runtime has errors"
            exit 1
          fi
          if ! grep -q "Farming Simulator pre-alpha" /tmp/run.log; then
            echo "::error::Expected boot message not found"
            exit 1
          fi

      - name: Summary
        run: |
          echo "✅ Project imports cleanly"
          echo "✅ Main scene boots without errors"
          echo "✅ Expected viewport: 640x360"
