name: Build and Deploy SQDC Dashboard

on:
  push:
    branches:
      - main
      - dashboard
  pull_request:
    branches:
      - main
      - dashboard

jobs:
  build:
    name: Build Docker Image
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '18'
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: npm test -- --passWithNoTests

      - name: Build application
        run: npm run build

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Login to Container Registry
        run: |
          echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login ${{ secrets.REGISTRY_URL }} -u ${{ secrets.REGISTRY_USER }} --password-stdin

      - name: Build and Push Docker image
        run: |
          docker build -t ${{ secrets.REGISTRY_URL }}/sortifal/pfee:${{ github.sha }} .
          docker tag ${{ secrets.REGISTRY_URL }}/sortifal/pfee:${{ github.sha }} ${{ secrets.REGISTRY_URL }}/sortifal/pfee:latest
          docker push ${{ secrets.REGISTRY_URL }}/sortifal/pfee:${{ github.sha }}
          docker push ${{ secrets.REGISTRY_URL }}/sortifal/pfee:latest

  deploy:
    name: Deploy to Kubernetes
    runs-on: ubuntu-latest
    needs: build
    if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/dashboard'
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up kubectl
        uses: azure/setup-kubectl@v3
        with:
          version: 'latest'

      - name: Configure kubectl
        run: |
          mkdir -p $HOME/.kube
          echo "${{ secrets.KUBE_CONFIG }}" > $HOME/.kube/config

      - name: Create registry credentials secret
        run: |
          kubectl create secret docker-registry registry-credentials \
            --docker-server=${{ secrets.REGISTRY_URL }} \
            --docker-username=${{ secrets.REGISTRY_USER }} \
            --docker-password=${{ secrets.REGISTRY_PASSWORD }} \
            -n sqdc-dashboard \
            --dry-run=client -o yaml | kubectl apply -f -

      - name: Deploy to Kubernetes
        run: |
          kubectl apply -f k8s/namespace.yaml
          kubectl apply -f k8s/deployment.yaml
          kubectl apply -f k8s/service.yaml
          kubectl apply -f k8s/ingress.yaml

      - name: Update deployment with new image
        run: |
          kubectl set image deployment/sqdc-dashboard dashboard=${{ secrets.REGISTRY_URL }}/sortifal/pfee:${{ github.sha }} -n sqdc-dashboard
          kubectl rollout status deployment/sqdc-dashboard -n sqdc-dashboard --timeout=5m

      - name: Verify deployment
        run: |
          kubectl get pods -n sqdc-dashboard
          kubectl get svc -n sqdc-dashboard
          kubectl get ingress -n sqdc-dashboard

  notify:
    name: Notify Deployment Status
    runs-on: ubuntu-latest
    needs: [build, deploy]
    if: always()
    steps:
      - name: Deployment Status
        run: |
          if [ "${{ needs.deploy.result }}" == "success" ]; then
            echo "✅ Deployment successful!"
          else
            echo "❌ Deployment failed!"
            exit 1
          fi
