name: CI - Main (API)

on:
  pull_request:
  push:
    branches:
      - main

permissions:
  contents: write
  pull-requests: write

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
  test-api:
    name: Test API
    defaults:
      run:
        working-directory: api
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v5
        with:
          fetch-depth: 0

      - name: Install pnpm
        uses: pnpm/action-setup@v4
        with:
          run_install: false

      - name: Install Node
        uses: actions/setup-node@v5
        with:
          node-version-file: ".nvmrc"
          cache: 'pnpm'

      - name: Cache APT Packages
        uses: awalsh128/cache-apt-pkgs-action@v1.5.3
        with:
          packages: bash procps python3 libvirt-dev jq zstd git build-essential libvirt-daemon-system php-cli
          version: 1.0

      - name: PNPM Install
        run: pnpm install --frozen-lockfile

      - name: Lint
        run: pnpm run lint

      - name: Type Check
        run: pnpm run type-check

      - name: Setup libvirt
        run: |
          # Create required groups (if they don't already exist)
          sudo groupadd -f libvirt
          sudo groupadd -f kvm

          # Create libvirt user if not present, and add it to the kvm group
          sudo useradd -m -s /bin/bash -g libvirt libvirt || true
          sudo usermod -aG kvm libvirt || true

          # Set up libvirt directories and permissions
          sudo mkdir -p /var/run/libvirt /var/log/libvirt /etc/libvirt
          sudo chown root:libvirt /var/run/libvirt /var/log/libvirt
          sudo chmod g+w /var/run/libvirt /var/log/libvirt

          # Configure libvirt by appending required settings
          sudo tee -a /etc/libvirt/libvirtd.conf > /dev/null <<EOF
          unix_sock_group = "libvirt"
          unix_sock_rw_perms = "0770"
          auth_unix_rw = "none"
          EOF

          # Add the current user to libvirt and kvm groups (note: this change won't apply to the current session)
          sudo usermod -aG libvirt,kvm $USER

          sudo mkdir -p /var/run/libvirt
          sudo chown root:libvirt /var/run/libvirt
          sudo chmod 775 /var/run/libvirt


          # Start libvirtd in the background
          sudo /usr/sbin/libvirtd --daemon

          # Wait a bit longer for libvirtd to start
          sleep 5

          # Verify libvirt is running using sudo to bypass group membership delays
          sudo virsh list --all || true

      - name: Build UI Package First
        run: |
          echo "🔧 Building UI package for web tests dependency..."
          cd ../unraid-ui && pnpm run build

      - name: Run Tests Concurrently
        run: |
          set -e

          # Run all tests in parallel with labeled output and coverage generation
          echo "🚀 Starting API coverage tests..."
          pnpm run coverage > api-test.log 2>&1 &
          API_PID=$!

          echo "🚀 Starting Connect plugin tests..."
          (cd ../packages/unraid-api-plugin-connect && pnpm test --coverage 2>/dev/null || pnpm test) > connect-test.log 2>&1 &
          CONNECT_PID=$!

          echo "🚀 Starting Shared package tests..."
          (cd ../packages/unraid-shared && pnpm test --coverage 2>/dev/null || pnpm test) > shared-test.log 2>&1 &
          SHARED_PID=$!

          echo "🚀 Starting Web package coverage tests..."
          (cd ../web && (pnpm test --coverage || pnpm test)) > web-test.log 2>&1 &
          WEB_PID=$!

          echo "🚀 Starting UI package coverage tests..."
          (cd ../unraid-ui && pnpm test --coverage 2>/dev/null || pnpm test) > ui-test.log 2>&1 &
          UI_PID=$!

          echo "🚀 Starting Plugin tests..."
          (cd ../plugin && pnpm test) > plugin-test.log 2>&1 &
          PLUGIN_PID=$!

          # Wait for all processes and capture exit codes
          wait $API_PID && echo "✅ API tests completed" || { echo "❌ API tests failed"; API_EXIT=1; }
          wait $CONNECT_PID && echo "✅ Connect tests completed" || { echo "❌ Connect tests failed"; CONNECT_EXIT=1; }
          wait $SHARED_PID && echo "✅ Shared tests completed" || { echo "❌ Shared tests failed"; SHARED_EXIT=1; }
          wait $WEB_PID && echo "✅ Web tests completed" || { echo "❌ Web tests failed"; WEB_EXIT=1; }
          wait $UI_PID && echo "✅ UI tests completed" || { echo "❌ UI tests failed"; UI_EXIT=1; }
          wait $PLUGIN_PID && echo "✅ Plugin tests completed" || { echo "❌ Plugin tests failed"; PLUGIN_EXIT=1; }

          # Display all outputs
          echo "📋 API Test Results:" && cat api-test.log
          echo "📋 Connect Plugin Test Results:" && cat connect-test.log  
          echo "📋 Shared Package Test Results:" && cat shared-test.log
          echo "📋 Web Package Test Results:" && cat web-test.log
          echo "📋 UI Package Test Results:" && cat ui-test.log
          echo "📋 Plugin Test Results:" && cat plugin-test.log

          # Exit with error if any test failed
          if [[ ${API_EXIT:-0} -eq 1 || ${CONNECT_EXIT:-0} -eq 1 || ${SHARED_EXIT:-0} -eq 1 || ${WEB_EXIT:-0} -eq 1 || ${UI_EXIT:-0} -eq 1 || ${PLUGIN_EXIT:-0} -eq 1 ]]; then
            exit 1
          fi

      - name: Upload all coverage reports to Codecov
        uses: codecov/codecov-action@v5
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: ./coverage/coverage-final.json,../web/coverage/coverage-final.json,../unraid-ui/coverage/coverage-final.json,../packages/unraid-api-plugin-connect/coverage/coverage-final.json,../packages/unraid-shared/coverage/coverage-final.json
          fail_ci_if_error: false

  build-artifacts:
    name: Build All Artifacts
    uses: ./.github/workflows/build-artifacts.yml
    secrets:
      VITE_ACCOUNT: ${{ secrets.VITE_ACCOUNT }}
      VITE_CONNECT: ${{ secrets.VITE_CONNECT }}
      VITE_UNRAID_NET: ${{ secrets.VITE_UNRAID_NET }}
      VITE_CALLBACK_KEY: ${{ secrets.VITE_CALLBACK_KEY }}
      UNRAID_BOT_GITHUB_ADMIN_TOKEN: ${{ secrets.UNRAID_BOT_GITHUB_ADMIN_TOKEN }}

  release-please:
    name: Release Please
    runs-on: ubuntu-latest
    # Only run on pushes to main AND after tests pass
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    needs:
      - test-api
      - build-artifacts
    permissions:
      contents: write
      pull-requests: write
    steps:
      - name: Checkout
        uses: actions/checkout@v5
        with:
          fetch-depth: 0

      - id: release
        uses: googleapis/release-please-action@v4
    outputs:
      releases_created: ${{ steps.release.outputs.releases_created || 'false' }}
      tag_name: ${{ steps.release.outputs.tag_name || '' }}

  build-plugin-staging-pr:
    name: Build and Deploy Plugin
    needs:
      - build-artifacts
      - test-api
    uses: ./.github/workflows/build-plugin.yml
    with:
      RELEASE_CREATED: 'false'
      TAG: ${{ github.event.pull_request.number && format('PR{0}', github.event.pull_request.number) || '' }}
      BUCKET_PATH: ${{ github.event.pull_request.number && format('unraid-api/tag/PR{0}', github.event.pull_request.number) || 'unraid-api' }}
      BASE_URL: "https://preview.dl.unraid.net/unraid-api"
      BUILD_NUMBER: ${{ needs.build-artifacts.outputs.build_number }}
    secrets:
      CF_ACCESS_KEY_ID: ${{ secrets.CF_ACCESS_KEY_ID }}
      CF_SECRET_ACCESS_KEY: ${{ secrets.CF_SECRET_ACCESS_KEY }}
      CF_BUCKET_PREVIEW: ${{ secrets.CF_BUCKET_PREVIEW }}
      CF_ENDPOINT: ${{ secrets.CF_ENDPOINT }}

  build-plugin-production:
    if: ${{ needs.release-please.outputs.releases_created == 'true' }}
    name: Build and Deploy Production Plugin
    needs:
      - release-please
      - build-artifacts
    uses: ./.github/workflows/build-plugin.yml
    with:
      RELEASE_CREATED: 'true'
      RELEASE_TAG: ${{ needs.release-please.outputs.tag_name }}
      TAG: ""
      BUCKET_PATH: unraid-api
      BASE_URL: "https://stable.dl.unraid.net/unraid-api"
      BUILD_NUMBER: ${{ needs.build-artifacts.outputs.build_number }}
      TRIGGER_PRODUCTION_RELEASE: true
    secrets:
      CF_ACCESS_KEY_ID: ${{ secrets.CF_ACCESS_KEY_ID }}
      CF_SECRET_ACCESS_KEY: ${{ secrets.CF_SECRET_ACCESS_KEY }}
      CF_BUCKET_PREVIEW: ${{ secrets.CF_BUCKET_PREVIEW }}
      CF_ENDPOINT: ${{ secrets.CF_ENDPOINT }}
      UNRAID_BOT_GITHUB_ADMIN_TOKEN: ${{ secrets.UNRAID_BOT_GITHUB_ADMIN_TOKEN }}
