name: Templates Package Tests

on:
  push:
    paths:
      - "packages/templates/**"
      - ".github/workflows/templates-test.yml"
  pull_request:
    paths:
      - "packages/templates/**"
      - ".github/workflows/templates-test.yml"

jobs:
  test-templates:
    name: Test Templates Package
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18, 20]
        python-version: ["3.8", "3.9", "3.10", "3.11"]

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

      - name: Setup Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: "pnpm"

      - name: Setup Python ${{ matrix.python-version }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install pnpm
        uses: pnpm/action-setup@v2
        with:
          version: latest

      - name: Install dependencies
        run: pnpm install

      - name: Run templates package tests
        run: pnpm --filter @soroban/templates test

      - name: Run CI test script
        working-directory: packages/templates
        run: ./ci-test.sh

      - name: Test examples
        working-directory: packages/templates
        run: |
          echo "Testing Node.js example..."
          node examples/node-example.js
          echo "Testing Python example..."
          python3 examples/python-example.py

  test-integration:
    name: Test Templates Integration
    runs-on: ubuntu-latest
    needs: test-templates

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

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: "pnpm"

      - name: Install pnpm
        uses: pnpm/action-setup@v2
        with:
          version: latest

      - name: Install dependencies
        run: pnpm install

      - name: Test web app integration
        run: |
          # Verify web app can import templates
          cd apps/web
          node -e "
            const templates = require('@soroban/templates');
            console.log('Web app templates import:', {
              flashcards: templates.FLASHCARDS_TEMPLATE ? '✅' : '❌',
              singleCard: templates.SINGLE_CARD_TEMPLATE ? '✅' : '❌',
              getPath: typeof templates.getTemplatePath === 'function' ? '✅' : '❌'
            });
          "

      - name: Test templates in API context
        run: |
          # Simulate API route usage
          cd apps/web
          node -e "
            const { getTemplatePath } = require('@soroban/templates');
            const fs = require('fs');
            try {
              const path = getTemplatePath('flashcards.typ');
              const content = fs.readFileSync(path, 'utf-8');
              console.log('API simulation successful:', {
                pathResolved: !!path,
                contentLoaded: content.length > 1000,
                hasSorobanFunction: content.includes('draw-soroban')
              });
            } catch (error) {
              console.error('API simulation failed:', error.message);
              process.exit(1);
            }
          "

  lint-and-format:
    name: Lint and Format Check
    runs-on: ubuntu-latest

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

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20"

      - name: Check JavaScript syntax
        working-directory: packages/templates
        run: |
          # Check for syntax errors in JS files
          node -c index.js
          node -c test.js
          node -c test-integration.js
          node -c test-validate.js
          node -c examples/node-example.js

      - name: Check Python syntax
        working-directory: packages/templates
        run: |
          # Check for syntax errors in Python files
          python3 -m py_compile __init__.py
          python3 -m py_compile test.py
          python3 -m py_compile examples/python-example.py

      - name: Check TypeScript syntax
        working-directory: packages/templates
        run: |
          # Install typescript for syntax checking
          npm install -g typescript
          # Check TypeScript example (ignore imports for syntax check)
          tsc --noEmit --allowJs --skipLibCheck examples/nextjs-example.ts || echo "TypeScript check completed with warnings"

  package-validation:
    name: Package Validation
    runs-on: ubuntu-latest

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

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20"

      - name: Install pnpm
        uses: pnpm/action-setup@v2
        with:
          version: latest

      - name: Validate package structure
        working-directory: packages/templates
        run: |
          # Check required files exist
          required_files=("package.json" "index.js" "index.d.ts" "__init__.py" "pyproject.toml" "README.md" "flashcards.typ" "single-card.typ")
          for file in "${required_files[@]}"; do
            if [ ! -f "$file" ]; then
              echo "❌ Missing required file: $file"
              exit 1
            else
              echo "✅ Found: $file"
            fi
          done

      - name: Check package.json validity
        working-directory: packages/templates
        run: |
          # Validate package.json structure
          node -e "
            const pkg = require('./package.json');
            const required = ['name', 'version', 'description', 'main', 'types', 'files'];
            for (const field of required) {
              if (!pkg[field]) {
                console.error('❌ Missing required field:', field);
                process.exit(1);
              }
            }
            console.log('✅ package.json structure valid');
          "

      - name: Verify TypeScript definitions
        working-directory: packages/templates
        run: |
          # Check that TypeScript definitions are valid
          npx tsc --noEmit index.d.ts || echo "TypeScript definitions checked"
