# Build Next.js console and produce deploy artifacts
#
name: Build

on:
  push:
    branches: ["main"]
  release:
    types: [published]
  workflow_dispatch:

permissions:
  contents: read
  pages: write
  id-token: write

concurrency:
  group: "pages"
  cancel-in-progress: false

env:
  NODE_VERSION: "22"

jobs:
  build:
    permissions:
      contents: write
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Setup PNPM
        uses: pnpm/action-setup@v6
        with:
          cache: true

      - name: Setup Node
        uses: actions/setup-node@v6
        with:
          node-version: ${{ env.NODE_VERSION }}

      - name: Install dependencies
        run: pnpm ci

      - name: Build Next.js
        run: pnpm build

      - name: Zip files
        run: |
          set -euo pipefail
          mkdir -p artifacts
          ls -al ./out
          (
            cd ./out
            zip -r ../artifacts/rustfs-console-latest.zip .
          )
          if [ "${{ github.event_name }}" = "release" ]; then
            mkdir -p ./artifacts/${{ github.event.release.tag_name }}
            (
              cd ./out
              zip -r ../artifacts/${{ github.event.release.tag_name }}/rustfs-console-${{ github.event.release.tag_name }}.zip .
            )
          fi

      - uses: actions/upload-artifact@v7
        name: Upload artifacts
        with:
          path: ./artifacts/rustfs-console-latest.zip

      - name: Upload to Cloudflare R2
        env:
          R2_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }}
          R2_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }}
          R2_ENDPOINT: ${{ secrets.R2_ENDPOINT }}
          R2_BUCKET: ${{ secrets.R2_BUCKET }}
          AWS_EC2_METADATA_DISABLED: true
        shell: bash
        run: |
          set -euo pipefail

          if [[ -z "$R2_ACCESS_KEY_ID" || -z "$R2_SECRET_ACCESS_KEY" || -z "$R2_ENDPOINT" || -z "$R2_BUCKET" ]]; then
            echo "⚠️ R2 credentials or endpoint missing, skipping artifact upload"
            exit 0
          fi

          if ! command -v aws >/dev/null 2>&1; then
            echo "❌ aws CLI not found on runner; cannot upload to R2"
            exit 1
          fi

          export AWS_ACCESS_KEY_ID="$R2_ACCESS_KEY_ID"
          export AWS_SECRET_ACCESS_KEY="$R2_SECRET_ACCESS_KEY"
          export AWS_DEFAULT_REGION="auto"

          R2_PATH="s3://${R2_BUCKET}/artifacts/console/"

          echo "📤 Uploading ./artifacts to $R2_PATH..."
          aws s3 cp ./artifacts "$R2_PATH" \
            --recursive \
            --endpoint-url "$R2_ENDPOINT" \
            --only-show-errors

          echo "✅ Upload completed successfully"

      - name: Upload release asset `versioned`
        if: ${{ github.event_name == 'release' }}
        uses: softprops/action-gh-release@v3
        with:
          files: ./artifacts/${{ github.event.release.tag_name }}/rustfs-console-${{ github.event.release.tag_name }}.zip
