name: Tests

on:
  push:
    branches: [main, dev]
  pull_request:
    branches: [main]

env:
  ALLURE_SERVER: "http://10.80.0.6:5050"
  ALLURE_PROJECT: "lcc-${{ gitea.ref_name }}"

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Java 23
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '23'
          cache: 'maven'

      - name: Install Maven
        run: |
          apt-get update && apt-get install -y maven

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Build Frontend
        run: cd src/frontend && npm ci && BUILD_FOR_SPRING=true npm run build

      - name: Run Tests
        run: mvn verify -B --no-transfer-progress
        env:
          TESTCONTAINERS_RYUK_DISABLED: "true"

      - name: Prepare Allure Results
        if: always()
        run: |
          mkdir -p target/allure-results
          cat > target/allure-results/executor.json << EOF
          {
            "name": "Gitea Actions",
            "type": "gitea",
            "buildName": "#${{ gitea.run_number }}",
            "buildOrder": ${{ gitea.run_number }},
            "buildUrl": "${{ gitea.server_url }}/${{ gitea.repository }}/actions/runs/${{ gitea.run_id }}"
          }
          EOF

      - name: Upload to Allure
        if: always()
        run: |
          # Login
          curl -s -c cookies.txt \
            -X POST "${ALLURE_SERVER}/allure-docker-service/login" \
            -H 'Content-Type: application/json' \
            -d '{"username":"admin","password":"${{ secrets.ALLURE_PASSWORD }}"}'

          CSRF_TOKEN=$(grep csrf_access_token cookies.txt | awk '{print $7}')

          # Create project
          curl -s -o /dev/null -b cookies.txt \
            -H "X-CSRF-TOKEN: ${CSRF_TOKEN}" \
            -X POST "${ALLURE_SERVER}/allure-docker-service/projects" \
            -H "Content-Type: application/json" \
            -d '{"id":"'${ALLURE_PROJECT}'"}' || true

          # Clean old results
          curl -s -o /dev/null -b cookies.txt \
            -H "X-CSRF-TOKEN: ${CSRF_TOKEN}" \
            "${ALLURE_SERVER}/allure-docker-service/clean-results?project_id=${ALLURE_PROJECT}"

          # Build JSON payload with base64
          echo '{"results":[' > payload.json
          FIRST=true
          for f in target/allure-results/*; do
            if [ -f "$f" ]; then
              FILENAME=$(basename "$f")
              CONTENT=$(base64 -w 0 "$f")
              if [ "$FIRST" = true ]; then
                FIRST=false
              else
                echo ',' >> payload.json
              fi
              echo '{"file_name":"'"$FILENAME"'","content_base64":"'"$CONTENT"'"}' >> payload.json
            fi
          done
          echo ']}' >> payload.json

          # Upload via JSON
          curl -s -o /dev/null -b cookies.txt \
            -H "X-CSRF-TOKEN: ${CSRF_TOKEN}" \
            -H "Content-Type: application/json" \
            -X POST "${ALLURE_SERVER}/allure-docker-service/send-results?project_id=${ALLURE_PROJECT}" \
            -d @payload.json

          # Generate report
          curl -s -b cookies.txt \
            -H "X-CSRF-TOKEN: ${CSRF_TOKEN}" \
            "${ALLURE_SERVER}/allure-docker-service/generate-report?project_id=${ALLURE_PROJECT}"
          
          echo "✅ Allure upload complete"