name: Docker Compose Build

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

jobs:
  test-docker:
    runs-on: ubuntu-latest

    services:
      docker:
        image: docker:26.0.0
        options: --privileged

    steps:
      - uses: actions/checkout@v2

      - name: Set permissions and run install.sh
        run: |
          chmod +x install.sh
          ./install.sh build --verbose

      - name: Set up Docker Compose
        run: |
          sed -i 's/25\:25/2525\:25/g' docker-compose.yml
          docker compose -f docker-compose.yml up -d

      - name: Test if services are responding
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 5
          max_attempts: 5
          command: |
            sleep 5

            # Array of endpoints to test
            declare -A endpoints=(
              ["WASM"]="https://localhost:443"
              ["WebApi"]="https://localhost:443/api"
              ["Admin"]="https://localhost:443/admin/user/login"
            )

            failed=false

            # Test HTTP endpoints
            for name in "${!endpoints[@]}"; do
              url="${endpoints[$name]}"
              echo "Testing $name at $url"

              # Store both response body and HTTP code
              response=$(curl -k -s -w "\nHTTP_CODE=%{http_code}" "$url")
              http_code=$(echo "$response" | grep "HTTP_CODE=" | cut -d= -f2)
              body=$(echo "$response" | sed '$d')  # Remove the last line (HTTP_CODE)

              if [ "$http_code" -ne 200 ]; then
                echo "❌ $name failed with HTTP $http_code at $url"
                echo "Response body:"
                echo "$body"
                failed=true
              else
                echo "✅ $name responded with HTTP 200"
              fi
            done

            # Test SMTP
            echo "Testing SmtpService at localhost:2525"
            if ! nc -zv localhost 2525 2>&1 | grep -q 'succeeded'; then
              echo "❌ SmtpService failed to respond on port 2525"
              failed=true
            else
              echo "✅ SmtpService responded successfully"
            fi

            # Exit with error if any service failed
            if [ "$failed" = true ]; then
              # Get container logs
              echo "Container Logs admin:"
              docker compose logs admin
              echo "Container Logs api:"
              docker compose logs api
              echo "Container Logs client:"
              docker compose logs client
              echo "Container Logs smtp:"
              docker compose logs smtp
              echo "Container Logs reverse-proxy:"
              docker compose logs reverse-proxy

              # Restart containers for next test in case of failure
              docker compose restart
              exit 1
            fi

      - name: Test install.sh reset-password output
        run: |
          output=$(./install.sh reset-password)
          if ! echo "$output" | grep -E '.*New admin password: [A-Za-z0-9+/=]{8,}.*'; then
            echo "Password reset output format is incorrect"
            echo "Expected: 'New admin password: <at least 8 base64 chars>'"
            echo "Actual: $output"
            exit 1
          fi
