name: Test Docker Compose Setup

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

env:
  COMPOSE_PROJECT_NAME: uptime-kuma-test

jobs:
  test-compose:
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v3

    - name: Create test environment file
      run: |
        cat > .env << EOF
        DISCORD_TOKEN=test_token_12345
        GUILD_ID=123456789012345678
        CHANNEL_ID=123456789012345678
        CLIENT_ID=123456789012345678
        UPDATE_TIME=60
        UPTIME_KUMA_URL=https://test.example.com/metrics
        UPTIME_KUMA_API_KEY=test_api_key_12345
        EOF

    - name: Build Docker images
      run: |
        docker-compose build --no-cache

    - name: Start services
      run: |
        docker-compose up -d
        sleep 30

    - name: Check service health
      run: |
        # Check if web backend is responding
        curl -f http://localhost:8080/back-end.php || exit 1
        
        # Check container status
        docker-compose ps
        
        # Check logs for errors
        docker-compose logs --tail=50

    - name: Test web backend endpoint
      run: |
        # Test the PHP endpoint (should fail gracefully with test data)
        response=$(curl -s -w "%{http_code}" http://localhost:8080/back-end.php)
        echo "Response: $response"
        # Accept both 200 (success) and 500 (expected failure with test data)
        if [[ "$response" =~ ^(200|500)$ ]]; then
          echo "✅ Web backend endpoint is responding"
        else
          echo "❌ Web backend endpoint failed"
          exit 1
        fi

    - name: Verify container logs
      run: |
        # Check Discord bot logs for startup
        docker-compose logs discord-bot | grep -E "(Bot is online|Error)" || echo "No startup messages found"
        
        # Check web backend logs
        docker-compose logs web-backend | grep -E "(Apache|PHP)" || echo "No backend messages found"

    - name: Cleanup
      if: always()
      run: |
        docker-compose down -v
        docker system prune -f

  lint-dockerfiles:
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Run hadolint on Dockerfiles
      uses: hadolint/hadolint-action@v3.1.0
      with:
        dockerfile: Bot/Dockerfile
        failure-threshold: warning

    - name: Run hadolint on Web Dockerfile
      uses: hadolint/hadolint-action@v3.1.0
      with:
        dockerfile: Web/Dockerfile
        failure-threshold: warning

  validate-compose:
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Validate docker-compose.yml
      run: |
        docker-compose config --quiet
        echo "✅ docker-compose.yml is valid"

    - name: Check for required environment variables
      run: |
        # Check if all required env vars are referenced in docker-compose.yml
        required_vars=("DISCORD_TOKEN" "GUILD_ID" "CHANNEL_ID" "CLIENT_ID" "UPTIME_KUMA_URL" "UPTIME_KUMA_API_KEY")
        
        for var in "${required_vars[@]}"; do
          if grep -q "\${$var}" docker-compose.yml; then
            echo "✅ $var is referenced in docker-compose.yml"
          else
            echo "❌ $var is missing from docker-compose.yml"
            exit 1
          fi
        done
