# GitHub Actions CD Template
# Purpose: Continuous Deployment workflow for staging and production
# Generated by: AIOX-FullStack @github-devops agent
#
# Usage: Copy to .github/workflows/cd.yml and customize

name: CD

on:
  push:
    branches: [main]
    tags: ['v*']
  workflow_dispatch:
    inputs:
      environment:
        description: 'Deployment environment'
        required: true
        default: 'staging'
        type: choice
        options:
          - staging
          - production

env:
  NODE_VERSION: '20.x'
  # Configure your deployment targets
  # VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
  # VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}

jobs:
  # ============================================================================
  # DETERMINE ENVIRONMENT
  # ============================================================================
  setup:
    name: Setup Deployment
    runs-on: ubuntu-latest
    outputs:
      environment: ${{ steps.set-env.outputs.environment }}
      version: ${{ steps.set-version.outputs.version }}
    steps:
      - name: Determine environment
        id: set-env
        run: |
          if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
            echo "environment=${{ github.event.inputs.environment }}" >> $GITHUB_OUTPUT
          elif [[ "${{ github.ref }}" == refs/tags/v* ]]; then
            echo "environment=production" >> $GITHUB_OUTPUT
          else
            echo "environment=staging" >> $GITHUB_OUTPUT
          fi

      - name: Set version
        id: set-version
        run: |
          if [[ "${{ github.ref }}" == refs/tags/v* ]]; then
            echo "version=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
          else
            echo "version=${{ github.sha }}" >> $GITHUB_OUTPUT
          fi

  # ============================================================================
  # BUILD FOR DEPLOYMENT
  # ============================================================================
  build:
    name: Build for ${{ needs.setup.outputs.environment }}
    runs-on: ubuntu-latest
    needs: setup
    environment: ${{ needs.setup.outputs.environment }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Build project
        run: npm run build
        env:
          NODE_ENV: production
          # Add environment-specific build vars
          # NEXT_PUBLIC_API_URL: ${{ vars.API_URL }}

      - name: Upload build artifacts
        uses: actions/upload-artifact@v4
        with:
          name: build-${{ needs.setup.outputs.environment }}
          path: dist/
          retention-days: 7

  # ============================================================================
  # DEPLOY TO STAGING
  # ============================================================================
  deploy-staging:
    name: Deploy to Staging
    runs-on: ubuntu-latest
    needs: [setup, build]
    if: needs.setup.outputs.environment == 'staging'
    environment:
      name: staging
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Download build artifacts
        uses: actions/download-artifact@v4
        with:
          name: build-staging
          path: dist/

      # Vercel deployment example
      # - name: Deploy to Vercel
      #   id: deploy
      #   uses: amondnet/vercel-action@v25
      #   with:
      #     vercel-token: ${{ secrets.VERCEL_TOKEN }}
      #     vercel-org-id: ${{ secrets.VERCEL_ORG_ID }}
      #     vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }}

      - name: Deploy (placeholder)
        id: deploy
        run: |
          echo "Deploying to staging..."
          echo "url=https://staging.example.com" >> $GITHUB_OUTPUT
          echo "::notice::Deployed to staging at https://staging.example.com"

  # ============================================================================
  # DEPLOY TO PRODUCTION
  # ============================================================================
  deploy-production:
    name: Deploy to Production
    runs-on: ubuntu-latest
    needs: [setup, build]
    if: needs.setup.outputs.environment == 'production'
    environment:
      name: production
      url: ${{ steps.deploy.outputs.url }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Download build artifacts
        uses: actions/download-artifact@v4
        with:
          name: build-production
          path: dist/

      # Vercel production deployment
      # - name: Deploy to Vercel Production
      #   id: deploy
      #   uses: amondnet/vercel-action@v25
      #   with:
      #     vercel-token: ${{ secrets.VERCEL_TOKEN }}
      #     vercel-org-id: ${{ secrets.VERCEL_ORG_ID }}
      #     vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }}
      #     vercel-args: '--prod'

      - name: Deploy (placeholder)
        id: deploy
        run: |
          echo "Deploying to production..."
          echo "url=https://example.com" >> $GITHUB_OUTPUT
          echo "::notice::Deployed to production at https://example.com"

  # ============================================================================
  # DATABASE MIGRATIONS (Optional)
  # ============================================================================
  # migrations:
  #   name: Run Database Migrations
  #   runs-on: ubuntu-latest
  #   needs: [setup, deploy-staging, deploy-production]
  #   if: always() && (needs.deploy-staging.result == 'success' || needs.deploy-production.result == 'success')
  #   environment: ${{ needs.setup.outputs.environment }}
  #   steps:
  #     - name: Checkout code
  #       uses: actions/checkout@v4
  #
  #     - name: Run migrations
  #       run: npm run db:migrate
  #       env:
  #         DATABASE_URL: ${{ secrets.DATABASE_URL }}

  # ============================================================================
  # POST-DEPLOYMENT VERIFICATION
  # ============================================================================
  verify:
    name: Verify Deployment
    runs-on: ubuntu-latest
    needs: [setup, deploy-staging, deploy-production]
    if: always() && (needs.deploy-staging.result == 'success' || needs.deploy-production.result == 'success')
    steps:
      - name: Health check
        run: |
          # Add your health check endpoint
          # curl -f https://${{ needs.setup.outputs.environment }}.example.com/health
          echo "Deployment verification passed"

      - name: Notify on success
        if: success()
        run: |
          echo "::notice::Successfully deployed to ${{ needs.setup.outputs.environment }}"

      - name: Notify on failure
        if: failure()
        run: |
          echo "::error::Deployment verification failed"
