name: CD Pipeline

on:
  push:
    branches: [main]
  workflow_dispatch:

jobs:
  deploy-staging:
    name: Deploy to Staging
    runs-on: ubuntu-latest
    environment: staging
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup kubectl
        uses: azure/setup-kubectl@v3
      
      - name: Setup Helm
        uses: azure/setup-helm@v3
      
      - name: Configure kubectl
        run: |
          echo "${{ secrets.KUBECONFIG_STAGING }}" | base64 -d > kubeconfig
          export KUBECONFIG=./kubeconfig
      
      - name: Deploy to Kubernetes
        run: |
          export KUBECONFIG=./kubeconfig
          kubectl apply -f gitops/apps/api/
          kubectl apply -f gitops/apps/frontend/
          kubectl apply -f gitops/apps/portal/
      
      - name: Wait for deployment
        run: |
          export KUBECONFIG=./kubeconfig
          kubectl rollout status deployment/api -n sankofa
          kubectl rollout status deployment/frontend -n sankofa
          kubectl rollout status deployment/portal -n sankofa

  deploy-production:
    name: Deploy to Production
    runs-on: ubuntu-latest
    environment: production
    needs: [deploy-staging]
    if: github.ref == 'refs/heads/main'
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup kubectl
        uses: azure/setup-kubectl@v3
      
      - name: Configure kubectl
        run: |
          echo "${{ secrets.KUBECONFIG_PRODUCTION }}" | base64 -d > kubeconfig
          export KUBECONFIG=./kubeconfig
      
      - name: Deploy to Kubernetes
        run: |
          export KUBECONFIG=./kubeconfig
          kubectl apply -f gitops/apps/api/
          kubectl apply -f gitops/apps/frontend/
          kubectl apply -f gitops/apps/portal/
      
      - name: Wait for deployment
        run: |
          export KUBECONFIG=./kubeconfig
          kubectl rollout status deployment/api -n sankofa
          kubectl rollout status deployment/frontend -n sankofa
          kubectl rollout status deployment/portal -n sankofa
      
      - name: Run smoke tests
        run: |
          # Smoke tests would go here
          echo "Running smoke tests..."

