name: CI/CD Pipeline

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main, develop ]
  release:
    types: [ published ]

env:
  NODE_VERSION: '18'
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  build-and-test:
    name: Build and Test
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
    
    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: ${{ env.NODE_VERSION }}
    
    - name: Install dependencies
      run: |
        if [ -f package-lock.json ]; then
          npm ci
        else
          npm install
        fi
    
    - name: Run linting
      run: npm run lint
    
    - name: Run type checking
      run: npx tsc --noEmit
    
    - name: Build project
      run: npm run build
    
    - name: Run unit and component tests
      run: npm run test:ci
    
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v3
    
    - name: Build Docker image (validation only)
      uses: docker/build-push-action@v5
      with:
        context: .
        push: false
        tags: ${{ env.IMAGE_NAME }}:test
        cache-from: type=gha
        cache-to: type=gha,mode=max
        platforms: linux/amd64,linux/arm64



  publish-npm:
    name: Publish to NPM
    runs-on: ubuntu-latest
    needs: [build-and-test]
    if: github.event_name == 'release' && github.event.action == 'published'
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
    
    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: ${{ env.NODE_VERSION }}
        registry-url: 'https://registry.npmjs.org'
    
    - name: Install dependencies
      run: |
        if [ -f package-lock.json ]; then
          npm ci
        else
          npm install
        fi
    
    - name: Extract version from GitHub tag
      id: version
      run: |
        # Remove 'v' prefix from tag (e.g., v1.0.0-rc.1 -> 1.0.0-rc.1)
        VERSION=${GITHUB_REF#refs/tags/v}
        echo "version=$VERSION" >> $GITHUB_OUTPUT
        echo "Publishing version: $VERSION"
    
    - name: Update package.json version
      run: |
        npm version ${{ steps.version.outputs.version }} --no-git-tag-version
        echo "Updated package.json to version ${{ steps.version.outputs.version }}"
    
    - name: Build project
      run: npm run build
    
    - name: Publish to NPM
      run: |
        if [[ "${{ steps.version.outputs.version }}" == *"-rc."* ]]; then
          echo "Publishing as release candidate with 'rc' tag"
          npm publish --tag rc
        elif [[ "${{ steps.version.outputs.version }}" == *"-beta."* ]]; then
          echo "Publishing as beta with 'beta' tag"
          npm publish --tag beta
        elif [[ "${{ steps.version.outputs.version }}" == *"-alpha."* ]]; then
          echo "Publishing as alpha with 'alpha' tag"
          npm publish --tag alpha
        else
          echo "Publishing as stable release with 'latest' tag"
          npm publish
        fi
      env:
        NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

  publish-docker:
    name: Publish Docker Image
    runs-on: ubuntu-latest
    needs: [build-and-test]
    if: github.event_name == 'release' && github.event.action == 'published'
    
    permissions:
      contents: read
      packages: write
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
    
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v3
    
    - name: Log in to Container Registry
      uses: docker/login-action@v3
      with:
        registry: ${{ env.REGISTRY }}
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}
    
    - name: Extract metadata
      id: meta
      uses: docker/metadata-action@v5
      with:
        images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
        tags: |
          type=ref,event=branch
          type=ref,event=pr
          type=semver,pattern={{version}}
          type=semver,pattern={{major}}.{{minor}}
          type=semver,pattern={{major}}
          type=raw,value=latest,enable={{is_default_branch}}
    
    - name: Build and push Docker image
      uses: docker/build-push-action@v5
      with:
        context: .
        push: true
        tags: ${{ steps.meta.outputs.tags }}
        labels: ${{ steps.meta.outputs.labels }}
        cache-from: type=gha
        cache-to: type=gha,mode=max
        platforms: linux/amd64,linux/arm64