name: Test CLI Options

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

jobs:
  test-cli-options:
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        include:
          # Default template combinations
          - template: "default"
            rpc: true
            tanstackQuery: false
            router: "none"
            linter: "eslint"
            test_name: "Default + RPC + No TanStack Query + No Router + ESLint"
          - template: "default"
            rpc: true
            tanstackQuery: false
            router: "none"
            linter: "biome"
            test_name: "Default + RPC + No TanStack Query + No Router + Biome"
          - template: "default"
            rpc: false
            tanstackQuery: false
            router: "none"
            linter: "eslint"
            test_name: "Default + No RPC + No TanStack Query + No Router + ESLint"
          - template: "default"
            rpc: false
            tanstackQuery: false
            router: "none"
            linter: "biome"
            test_name: "Default + No RPC + No TanStack Query + No Router + Biome"
          - template: "default"
            rpc: true
            tanstackQuery: true
            router: "none"
            linter: "eslint"
            test_name: "Default + RPC + TanStack Query + No Router + ESLint"
          - template: "default"
            rpc: true
            tanstackQuery: true
            router: "none"
            linter: "biome"
            test_name: "Default + RPC + TanStack Query + No Router + Biome"
          - template: "default"
            rpc: false
            tanstackQuery: true
            router: "none"
            linter: "eslint"
            test_name: "Default + No RPC + TanStack Query + No Router + ESLint"
          - template: "default"
            rpc: false
            tanstackQuery: true
            router: "none"
            linter: "biome"
            test_name: "Default + No RPC + TanStack Query + No Router + Biome"

          # Tailwind template combinations
          - template: "tailwind"
            rpc: true
            tanstackQuery: false
            router: "none"
            linter: "eslint"
            test_name: "Tailwind + RPC + No TanStack Query + No Router + ESLint"
          - template: "tailwind"
            rpc: true
            tanstackQuery: false
            router: "none"
            linter: "biome"
            test_name: "Tailwind + RPC + No TanStack Query + No Router + Biome"
          - template: "tailwind"
            rpc: false
            tanstackQuery: false
            router: "none"
            linter: "eslint"
            test_name: "Tailwind + No RPC + No TanStack Query + No Router + ESLint"
          - template: "tailwind"
            rpc: false
            tanstackQuery: false
            router: "none"
            linter: "biome"
            test_name: "Tailwind + No RPC + No TanStack Query + No Router + Biome"
          - template: "tailwind"
            rpc: true
            tanstackQuery: true
            router: "none"
            linter: "eslint"
            test_name: "Tailwind + RPC + TanStack Query + No Router + ESLint"
          - template: "tailwind"
            rpc: true
            tanstackQuery: true
            router: "none"
            linter: "biome"
            test_name: "Tailwind + RPC + TanStack Query + No Router + Biome"
          - template: "tailwind"
            rpc: false
            tanstackQuery: true
            router: "none"
            linter: "eslint"
            test_name: "Tailwind + No RPC + TanStack Query + No Router + ESLint"
          - template: "tailwind"
            rpc: false
            tanstackQuery: true
            router: "none"
            linter: "biome"
            test_name: "Tailwind + No RPC + TanStack Query + No Router + Biome"

          # Shadcn template combinations
          - template: "shadcn"
            rpc: true
            tanstackQuery: false
            router: "none"
            linter: "eslint"
            test_name: "Shadcn + RPC + No TanStack Query + No Router + ESLint"
          - template: "shadcn"
            rpc: true
            tanstackQuery: false
            router: "none"
            linter: "biome"
            test_name: "Shadcn + RPC + No TanStack Query + No Router + Biome"
          - template: "shadcn"
            rpc: false
            tanstackQuery: false
            router: "none"
            linter: "eslint"
            test_name: "Shadcn + No RPC + No TanStack Query + No Router + ESLint"
          - template: "shadcn"
            rpc: false
            tanstackQuery: false
            router: "none"
            linter: "biome"
            test_name: "Shadcn + No RPC + No TanStack Query + No Router + Biome"
          - template: "shadcn"
            rpc: true
            tanstackQuery: true
            router: "none"
            linter: "eslint"
            test_name: "Shadcn + RPC + TanStack Query + No Router + ESLint"
          - template: "shadcn"
            rpc: true
            tanstackQuery: true
            router: "none"
            linter: "biome"
            test_name: "Shadcn + RPC + TanStack Query + No Router + Biome"
          - template: "shadcn"
            rpc: false
            tanstackQuery: true
            router: "none"
            linter: "eslint"
            test_name: "Shadcn + No RPC + TanStack Query + No Router + ESLint"
          - template: "shadcn"
            rpc: false
            tanstackQuery: true
            router: "none"
            linter: "biome"
            test_name: "Shadcn + No RPC + TanStack Query + No Router + Biome"

          # React Router
          # Default template combinations
          - template: "default"
            rpc: true
            tanstackQuery: false
            router: "reactrouter"
            linter: "eslint"
            test_name: "Default + RPC + No TanStack Query + React Router + ESLint"
          - template: "default"
            rpc: true
            tanstackQuery: false
            router: "reactrouter"
            linter: "biome"
            test_name: "Default + RPC + No TanStack Query + React Router + Biome"
          - template: "default"
            rpc: false
            tanstackQuery: false
            router: "reactrouter"
            linter: "eslint"
            test_name: "Default + No RPC + No TanStack Query + React Router + ESLint"
          - template: "default"
            rpc: false
            tanstackQuery: false
            router: "reactrouter"
            linter: "biome"
            test_name: "Default + No RPC + No TanStack Query + React Router + Biome"
          - template: "default"
            rpc: true
            tanstackQuery: true
            router: "reactrouter"
            linter: "eslint"
            test_name: "Default + RPC + TanStack Query + React Router + ESLint"
          - template: "default"
            rpc: true
            tanstackQuery: true
            router: "reactrouter"
            linter: "biome"
            test_name: "Default + RPC + TanStack Query + React Router + Biome"
          - template: "default"
            rpc: false
            tanstackQuery: true
            router: "reactrouter"
            linter: "eslint"
            test_name: "Default + No RPC + TanStack Query + React Router + ESLint"
          - template: "default"
            rpc: false
            tanstackQuery: true
            router: "reactrouter"
            linter: "biome"
            test_name: "Default + No RPC + TanStack Query + React Router + Biome"

          # Tailwind template combinations
          - template: "tailwind"
            rpc: true
            tanstackQuery: false
            router: "reactrouter"
            linter: "eslint"
            test_name: "Tailwind + RPC + No TanStack Query + React Router + ESLint"
          - template: "tailwind"
            rpc: true
            tanstackQuery: false
            router: "reactrouter"
            linter: "biome"
            test_name: "Tailwind + RPC + No TanStack Query + React Router + Biome"
          - template: "tailwind"
            rpc: false
            tanstackQuery: false
            router: "reactrouter"
            linter: "eslint"
            test_name: "Tailwind + No RPC + No TanStack Query + React Router + ESLint"
          - template: "tailwind"
            rpc: false
            tanstackQuery: false
            router: "reactrouter"
            linter: "biome"
            test_name: "Tailwind + No RPC + No TanStack Query + React Router + Biome"
          - template: "tailwind"
            rpc: true
            tanstackQuery: true
            router: "reactrouter"
            linter: "eslint"
            test_name: "Tailwind + RPC + TanStack Query + React Router + ESLint"
          - template: "tailwind"
            rpc: true
            tanstackQuery: true
            router: "reactrouter"
            linter: "biome"
            test_name: "Tailwind + RPC + TanStack Query + React Router + Biome"
          - template: "tailwind"
            rpc: false
            tanstackQuery: true
            router: "reactrouter"
            linter: "eslint"
            test_name: "Tailwind + No RPC + TanStack Query + React Router + ESLint"
          - template: "tailwind"
            rpc: false
            tanstackQuery: true
            router: "reactrouter"
            linter: "biome"
            test_name: "Tailwind + No RPC + TanStack Query + React Router + Biome"

          # Shadcn template combinations
          - template: "shadcn"
            rpc: true
            tanstackQuery: false
            router: "reactrouter"
            linter: "eslint"
            test_name: "Shadcn + RPC + No TanStack Query + React Router + ESLint"
          - template: "shadcn"
            rpc: true
            tanstackQuery: false
            router: "reactrouter"
            linter: "biome"
            test_name: "Shadcn + RPC + No TanStack Query + React Router + Biome"
          - template: "shadcn"
            rpc: false
            tanstackQuery: false
            router: "reactrouter"
            linter: "eslint"
            test_name: "Shadcn + No RPC + No TanStack Query + React Router + ESLint"
          - template: "shadcn"
            rpc: false
            tanstackQuery: false
            router: "reactrouter"
            linter: "biome"
            test_name: "Shadcn + No RPC + No TanStack Query + React Router + Biome"
          - template: "shadcn"
            rpc: true
            tanstackQuery: true
            router: "reactrouter"
            linter: "eslint"
            test_name: "Shadcn + RPC + TanStack Query + React Router + ESLint"
          - template: "shadcn"
            rpc: true
            tanstackQuery: true
            router: "reactrouter"
            linter: "biome"
            test_name: "Shadcn + RPC + TanStack Query + React Router + Biome"
          - template: "shadcn"
            rpc: false
            tanstackQuery: true
            router: "reactrouter"
            linter: "eslint"
            test_name: "Shadcn + No RPC + TanStack Query + React Router + ESLint"
          - template: "shadcn"
            rpc: false
            tanstackQuery: true
            router: "reactrouter"
            linter: "biome"
            test_name: "Shadcn + No RPC + TanStack Query + React Router + Biome"

          # TanStack Router
          # Default template combinations
          - template: "default"
            rpc: true
            tanstackQuery: false
            router: "tanstackrouter"
            linter: "eslint"
            test_name: "Default + RPC + No TanStack Query + TanStack Router + ESLint"
          - template: "default"
            rpc: true
            tanstackQuery: false
            router: "tanstackrouter"
            linter: "biome"
            test_name: "Default + RPC + No TanStack Query + TanStack Router + Biome"
          - template: "default"
            rpc: false
            tanstackQuery: false
            router: "tanstackrouter"
            linter: "eslint"
            test_name: "Default + No RPC + No TanStack Query + TanStack Router + ESLint"
          - template: "default"
            rpc: false
            tanstackQuery: false
            router: "tanstackrouter"
            linter: "biome"
            test_name: "Default + No RPC + No TanStack Query + TanStack Router + Biome"
          - template: "default"
            rpc: true
            tanstackQuery: true
            router: "tanstackrouter"
            linter: "eslint"
            test_name: "Default + RPC + TanStack Query + TanStack Router + ESLint"
          - template: "default"
            rpc: true
            tanstackQuery: true
            router: "tanstackrouter"
            linter: "biome"
            test_name: "Default + RPC + TanStack Query + TanStack Router + Biome"
          - template: "default"
            rpc: false
            tanstackQuery: true
            router: "tanstackrouter"
            linter: "eslint"
            test_name: "Default + No RPC + TanStack Query + TanStack Router + ESLint"
          - template: "default"
            rpc: false
            tanstackQuery: true
            router: "tanstackrouter"
            linter: "biome"
            test_name: "Default + No RPC + TanStack Query + TanStack Router + Biome"

          # Tailwind template combinations
          - template: "tailwind"
            rpc: true
            tanstackQuery: false
            router: "tanstackrouter"
            linter: "eslint"
            test_name: "Tailwind + RPC + No TanStack Query + TanStack Router + ESLint"
          - template: "tailwind"
            rpc: true
            tanstackQuery: false
            router: "tanstackrouter"
            linter: "biome"
            test_name: "Tailwind + RPC + No TanStack Query + TanStack Router + Biome"
          - template: "tailwind"
            rpc: false
            tanstackQuery: false
            router: "tanstackrouter"
            linter: "eslint"
            test_name: "Tailwind + No RPC + No TanStack Query + TanStack Router + ESLint"
          - template: "tailwind"
            rpc: false
            tanstackQuery: false
            router: "tanstackrouter"
            linter: "biome"
            test_name: "Tailwind + No RPC + No TanStack Query + TanStack Router + Biome"
          - template: "tailwind"
            rpc: true
            tanstackQuery: true
            router: "tanstackrouter"
            linter: "eslint"
            test_name: "Tailwind + RPC + TanStack Query + TanStack Router + ESLint"
          - template: "tailwind"
            rpc: true
            tanstackQuery: true
            router: "tanstackrouter"
            linter: "biome"
            test_name: "Tailwind + RPC + TanStack Query + TanStack Router + Biome"
          - template: "tailwind"
            rpc: false
            tanstackQuery: true
            router: "tanstackrouter"
            linter: "eslint"
            test_name: "Tailwind + No RPC + TanStack Query + TanStack Router + ESLint"
          - template: "tailwind"
            rpc: false
            tanstackQuery: true
            router: "tanstackrouter"
            linter: "biome"
            test_name: "Tailwind + No RPC + TanStack Query + TanStack Router + Biome"

          # Shadcn template combinations
          - template: "shadcn"
            rpc: true
            tanstackQuery: false
            router: "tanstackrouter"
            linter: "eslint"
            test_name: "Shadcn + RPC + No TanStack Query + TanStack Router + ESLint"
          - template: "shadcn"
            rpc: true
            tanstackQuery: false
            router: "tanstackrouter"
            linter: "biome"
            test_name: "Shadcn + RPC + No TanStack Query + TanStack Router + Biome"
          - template: "shadcn"
            rpc: false
            tanstackQuery: false
            router: "tanstackrouter"
            linter: "eslint"
            test_name: "Shadcn + No RPC + No TanStack Query + TanStack Router + ESLint"
          - template: "shadcn"
            rpc: false
            tanstackQuery: false
            router: "tanstackrouter"
            linter: "biome"
            test_name: "Shadcn + No RPC + No TanStack Query + TanStack Router + Biome"
          - template: "shadcn"
            rpc: true
            tanstackQuery: true
            router: "tanstackrouter"
            linter: "eslint"
            test_name: "Shadcn + RPC + TanStack Query + TanStack Router + ESLint"
          - template: "shadcn"
            rpc: true
            tanstackQuery: true
            router: "tanstackrouter"
            linter: "biome"
            test_name: "Shadcn + RPC + TanStack Query + TanStack Router + Biome"
          - template: "shadcn"
            rpc: false
            tanstackQuery: true
            router: "tanstackrouter"
            linter: "eslint"
            test_name: "Shadcn + No RPC + TanStack Query + TanStack Router + ESLint"
          - template: "shadcn"
            rpc: false
            tanstackQuery: true
            router: "tanstackrouter"
            linter: "biome"
            test_name: "Shadcn + No RPC + TanStack Query + TanStack Router + Biome"

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - name: Install dependencies
        run: bun install

      - name: Build CLI
        run: bun run build

      - name: Create test project - ${{ matrix.test_name }}
        run: |
          # Create project with specified options
          echo "Creating project with options:"
          echo "Template: ${{ matrix.template }}"
          echo "RPC: ${{ matrix.rpc }}"
          echo "TanStack Query: ${{ matrix.tanstackQuery }}"
          echo "Router: ${{ matrix.router }}"
          echo "Linter: ${{ matrix.linter }}"

          # Build the command with conditional flags
          cmd="./dist/index.js test-project-${{ matrix.template }}-${{ matrix.rpc }}-${{ matrix.tanstackQuery }}-${{ matrix.router }}-${{ matrix.linter }} --yes --template ${{ matrix.template }}"
          
          if [ "${{ matrix.rpc }}" = "true" ]; then
            cmd="$cmd --rpc"
          fi
          
          if [ "${{ matrix.tanstackQuery }}" = "true" ]; then
            cmd="$cmd --tsquery"
          fi
          
          if [ "${{ matrix.router }}" != "none" ]; then
            cmd="$cmd --router ${{ matrix.router }}"
          fi
          
          cmd="$cmd --linter ${{ matrix.linter }}"
          
          echo "Running: $cmd"
          eval $cmd

      - name: Install project dependencies
        run: |
          cd test-project-${{ matrix.template }}-${{ matrix.rpc }}-${{ matrix.tanstackQuery }}-${{ matrix.router }}-${{ matrix.linter }}
          bun install

      - name: Build test project
        run: |
          cd test-project-${{ matrix.template }}-${{ matrix.rpc }}-${{ matrix.tanstackQuery }}-${{ matrix.router }}-${{ matrix.linter }}
          bun run build

      - name: Verify build outputs
        run: |
          cd test-project-${{ matrix.template }}-${{ matrix.rpc }}-${{ matrix.tanstackQuery }}-${{ matrix.router }}-${{ matrix.linter }}

          # Check that dist directories exist
          if [ ! -d "client/dist" ]; then
            echo "❌ Client build failed - dist directory not found"
            exit 1
          fi

          if [ ! -d "server/dist" ]; then
            echo "❌ Server build failed - dist directory not found"
            exit 1
          fi

          # Check for expected files
          if [ ! -f "client/dist/index.html" ]; then
            echo "❌ Client build incomplete - index.html not found"
            exit 1
          fi

          if [ ! -f "server/dist/index.js" ]; then
            echo "❌ Server build incomplete - index.js not found"
            exit 1
          fi

          echo "✅ Build verification passed for ${{ matrix.test_name }}"

      - name: Run linter on generated project
        run: |
          cd test-project-${{ matrix.template }}-${{ matrix.rpc }}-${{ matrix.tanstackQuery }}-${{ matrix.router }}-${{ matrix.linter }}

          if [ "${{ matrix.linter }}" = "eslint" ]; then
            # Check if ESLint config exists and run it
            if [ -f ".eslintrc.json" ] || [ -f ".eslintrc.js" ] || [ -f "eslint.config.js" ]; then
              echo "Running ESLint..."
              bun run lint || echo "ESLint warnings/errors found but continuing..."
            fi
          elif [ "${{ matrix.linter }}" = "biome" ]; then
            # Check if Biome config exists and run it
            if [ -f "biome.json" ]; then
              echo "Running Biome..."
              bun run lint || echo "Biome warnings/errors found but continuing..."
            fi
          fi

      - name: Cleanup test project
        if: always()
        run: |
          rm -rf test-project-${{ matrix.template }}-${{ matrix.rpc }}-${{ matrix.tanstackQuery }}-${{ matrix.router }}-${{ matrix.linter }}
