name: Test Database Replay

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

env:
  GOWORK: off
  CGO_ENABLED: "0"
  GOPRIVATE: github.com/luxfi/*
  GONOSUMDB: github.com/luxfi/*

jobs:
  test-zapdb-replay:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Set up Go
      uses: actions/setup-go@v6
      with:
        go-version-file: go.mod
        check-latest: true

    - name: Configure Git for private modules
      run: git config --global url."https://${{ github.token }}@github.com/".insteadOf "https://github.com/"

    - name: Build luxd with database support
      run: |
        echo "Building luxd (CGO_ENABLED=0)..."
        go build -trimpath -o ./build/luxd ./main
        ./build/luxd --version

    - name: Generate test staking keys
      run: |
        # Create test staking keys directory
        mkdir -p test-keys
        echo "Created test-keys directory for staking keys"

    - name: Test database types
      run: |
        # Test that each database type can be initialized
        for db_type in zapdb badgerdb memdb; do
          echo "Testing $db_type..."
          timeout 10s ./build/luxd \
            --network-id=96369 \
            --db-type=$db_type \
            --data-dir=/tmp/test-$db_type \
            --http-port=9630 \
            --staking-port=9631 \
            --log-level=info \
            --sybil-protection-enabled=false \
            --api-admin-enabled=true || true

          # Check if database was created
          if [ "$db_type" != "memdb" ]; then
            ls -la /tmp/test-$db_type/db/ || true
          fi

          # Clean up
          rm -rf /tmp/test-$db_type
        done

    - name: Test genesis database replay
      run: |
        # This would test the genesis-db flag with a sample database
        # In a real CI environment, you'd have a test database available
        echo "Testing genesis-db flag..."

        # Create a mock test to verify the flag is accepted
        timeout 5s ./build/luxd \
          --network-id=96369 \
          --db-type=zapdb \
          --genesis-db=/tmp/mock-genesis-db \
          --genesis-db-type=zapdb \
          --data-dir=/tmp/test-replay \
          --http-port=9630 \
          --staking-port=9631 \
          --log-level=info \
          --sybil-protection-enabled=false \
          --api-admin-enabled=true 2>&1 | grep -E "(genesis-db|Genesis)" || true

  test-database-factory:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v4

    - name: Set up Go
      uses: actions/setup-go@v6
      with:
        go-version-file: go.mod
        check-latest: true

    - name: Configure Git for private modules
      run: git config --global url."https://${{ github.token }}@github.com/".insteadOf "https://github.com/"

    - name: Test database factory
      run: |
        # Run unit tests for the database factory
        go test -v ./internal/database/...

    - name: Test database implementations
      run: |
        # Test each database implementation
        go test -v ./internal/database/...
