---
name: Container Integration Tests

on:
  workflow_call:
  pull_request:
    branches:
      - '*'
  workflow_dispatch:  # Allow manual trigger

jobs:
  minio-tests:
    name: MinIO Integration Tests
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v6

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: llvm-tools-preview

      - name: Cache cargo registry
        uses: actions/cache@v5
        with:
          path: ~/.cargo/registry
          key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-registry-

      - name: Cache cargo index
        uses: actions/cache@v5
        with:
          path: ~/.cargo/git
          key: ${{ runner.os }}-cargo-git-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-git-

      - name: Cache cargo build
        uses: actions/cache@v5
        with:
          path: target
          key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-build-target-

      - name: Pull MinIO Docker image
        run: docker pull minio/minio:latest

      - name: Verify Docker is running
        run: docker info

      - name: Start MinIO container
        run: |
          CONTAINER_NAME="s3m-test-minio"
          docker rm -f "$CONTAINER_NAME" 2>/dev/null || true
          docker run -d \
            --name "$CONTAINER_NAME" \
            -p 9000:9000 \
            -p 9001:9001 \
            -e MINIO_ROOT_USER=minioadmin \
            -e MINIO_ROOT_PASSWORD=minioadmin \
            minio/minio:latest server /data --console-address ":9001"

      - name: Wait for MinIO readiness
        run: |
          set -euo pipefail
          for _ in {1..30}; do
            if curl -sf http://127.0.0.1:9000/minio/health/live >/dev/null; then
              echo "MINIO_ENDPOINT=http://127.0.0.1:9000" >> "$GITHUB_ENV"
              echo "MINIO_ACCESS_KEY=minioadmin" >> "$GITHUB_ENV"
              echo "MINIO_SECRET_KEY=minioadmin" >> "$GITHUB_ENV"
              exit 0
            fi
            sleep 1
          done

          echo "MinIO did not become ready in time" >&2
          docker logs s3m-test-minio || true
          exit 1

      - name: Build s3m binary for e2e tests
        run: cargo build --bin s3m

      - name: Run integration test suite
        run: cargo test --tests -- --test-threads=1
        env:
          RUST_BACKTRACE: 1
          RUST_LOG: debug

      - name: Clean up containers
        if: always()
        run: |
          docker ps -aq | xargs -r docker stop
          docker ps -aq | xargs -r docker rm

  test-summary:
    name: Integration Tests Summary
    runs-on: ubuntu-latest
    needs: minio-tests
    if: always()

    steps:
      - name: Check test results
        run: |
          if [ "${{ needs.minio-tests.result }}" == "success" ]; then
            echo "✅ All container integration tests passed!"
            echo "- MinIO-backed integration suite completed"
          else
            echo "❌ Container integration tests failed"
            exit 1
          fi
