name: C++ Build & Test

on:
  push:
    branches: [ main, develop ]
    paths:
      - 'dbal/cpp/**'
      - 'dbal/tools/cpp-build-assistant.cjs'
      - '.github/workflows/cpp-build.yml'
  pull_request:
    branches: [ main, develop ]
    paths:
      - 'dbal/cpp/**'
      - 'dbal/tools/cpp-build-assistant.cjs'
      - '.github/workflows/cpp-build.yml'
  workflow_dispatch:

permissions:
  contents: read

jobs:
  check-implementation:
    name: Check C++ Implementation Status
    runs-on: ubuntu-latest
    permissions:
      contents: read
    outputs:
      has_sources: ${{ steps.check.outputs.has_sources }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Check if C++ sources exist
        id: check
        run: |
          if [ -d "dbal/cpp/src" ] && [ "$(find dbal/cpp/src -name '*.cpp' | wc -l)" -gt 0 ]; then
            echo "has_sources=true" >> $GITHUB_OUTPUT
            echo "✓ C++ source files found"
          else
            echo "has_sources=false" >> $GITHUB_OUTPUT
            echo "⚠ C++ implementation not yet available - skipping build"
          fi

  build-linux:
    name: Build on Linux
    runs-on: ubuntu-latest
    needs: check-implementation
    if: needs.check-implementation.outputs.has_sources == 'true'
    
    strategy:
      matrix:
        build_type: [Release, Debug]
        compiler:
          - { cc: gcc, cxx: g++ }
          - { cc: clang, cxx: clang++ }
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
      
      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y cmake ninja-build ${{ matrix.compiler.cxx }}
          pip install conan
      
      - name: Setup Conan profile
        run: conan profile detect --force
      
      - name: Check C++ dependencies
        run: bun run cpp:check
      
      - name: Initialize Conanfile
        run: bun run cpp:init
      
      - name: Install Conan dependencies
        env:
          CMAKE_BUILD_TYPE: ${{ matrix.build_type }}
          CC: ${{ matrix.compiler.cc }}
          CXX: ${{ matrix.compiler.cxx }}
        run: bun run cpp:install
      
      - name: Configure CMake
        env:
          CMAKE_BUILD_TYPE: ${{ matrix.build_type }}
          CC: ${{ matrix.compiler.cc }}
          CXX: ${{ matrix.compiler.cxx }}
        run: |
          if [ "${{ matrix.build_type }}" = "Debug" ]; then
            bun run cpp:build -- configure --debug
          else
            bun run cpp:configure
          fi
      
      - name: Build C++ project
        env:
          CMAKE_BUILD_TYPE: ${{ matrix.build_type }}
        run: bun run cpp:build
      
      - name: Run C++ tests
        run: bun run cpp:test
      
      - name: Upload build artifacts
        if: matrix.build_type == 'Release' && matrix.compiler.cxx == 'g++'
        uses: actions/upload-artifact@v4
        with:
          name: dbal-daemon-linux
          path: |
            dbal/cpp/build/dbal_daemon
            dbal/cpp/build/*.so
          retention-days: 7

  build-macos:
    name: Build on macOS
    runs-on: macos-latest
    needs: check-implementation
    if: needs.check-implementation.outputs.has_sources == 'true'
    
    strategy:
      matrix:
        build_type: [Release, Debug]
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
      
      - name: Install system dependencies
        run: |
          brew install cmake ninja conan
      
      - name: Setup Conan profile
        run: conan profile detect --force
      
      - name: Check C++ dependencies
        run: bun run cpp:check
      
      - name: Full C++ build
        env:
          CMAKE_BUILD_TYPE: ${{ matrix.build_type }}
        run: |
          if [ "${{ matrix.build_type }}" = "Debug" ]; then
            node dbal/tools/cpp-build-assistant.cjs full --debug
          else
            bun run cpp:full
          fi
      
      - name: Run C++ tests
        run: bun run cpp:test
      
      - name: Upload build artifacts
        if: matrix.build_type == 'Release'
        uses: actions/upload-artifact@v4
        with:
          name: dbal-daemon-macos
          path: |
            dbal/cpp/build/dbal_daemon
            dbal/cpp/build/*.dylib
          retention-days: 7

  build-windows:
    name: Build on Windows
    runs-on: windows-latest
    needs: check-implementation
    if: needs.check-implementation.outputs.has_sources == 'true'
    
    strategy:
      matrix:
        build_type: [Release, Debug]
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
      
      - name: Install system dependencies
        run: |
          choco install cmake ninja -y
          pip install conan
      
      - name: Setup Conan profile
        run: conan profile detect --force
      
      - name: Check C++ dependencies
        run: bun run cpp:check
      
      - name: Full C++ build
        env:
          CMAKE_BUILD_TYPE: ${{ matrix.build_type }}
        shell: bash
        run: |
          if [ "${{ matrix.build_type }}" = "Debug" ]; then
            node dbal/tools/cpp-build-assistant.cjs full --debug
          else
            bun run cpp:full
          fi
      
      - name: Run C++ tests
        run: bun run cpp:test
      
      - name: Upload build artifacts
        if: matrix.build_type == 'Release'
        uses: actions/upload-artifact@v4
        with:
          name: dbal-daemon-windows
          path: |
            dbal/cpp/build/dbal_daemon.exe
            dbal/cpp/build/*.dll
          retention-days: 7

  code-quality:
    name: C++ Code Quality
    runs-on: ubuntu-latest
    needs: check-implementation
    if: needs.check-implementation.outputs.has_sources == 'true'
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
      
      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y cmake ninja-build cppcheck clang-format
          pip install conan
      
      - name: Setup Conan
        run: conan profile detect --force
      
      - name: Configure project
        run: bun run cpp:full
      
      - name: Run cppcheck
        run: |
          cppcheck --enable=all --inconclusive --error-exitcode=1 \
            --suppress=missingIncludeSystem \
            -I dbal/cpp/include \
            dbal/cpp/src/
        continue-on-error: true
      
      - name: Check formatting
        run: |
          find dbal/cpp/src dbal/cpp/include -name '*.cpp' -o -name '*.hpp' | \
          xargs clang-format --dry-run --Werror
        continue-on-error: true

  integration:
    name: Integration Test
    runs-on: ubuntu-latest
    needs: [check-implementation, build-linux]
    if: needs.check-implementation.outputs.has_sources == 'true'
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
      
      - name: Install dependencies
        run: bun install --frozen-lockfile
      
      - name: Download Linux build
        uses: actions/download-artifact@v4
        with:
          name: dbal-daemon-linux
          path: dbal/cpp/build/
      
      - name: Make daemon executable
        run: chmod +x dbal/cpp/build/dbal_daemon
      
      - name: Run integration tests
        run: |
          # Start C++ daemon
          ./dbal/cpp/build/dbal_daemon &
          DAEMON_PID=$!
          sleep 2
          
          # Run TypeScript integration tests
          bun run test:unit
          
          # Cleanup
          kill $DAEMON_PID
        continue-on-error: true
