name: CI/CD Pipeline

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

env:
  REGISTRY: ghcr.io
  OWNER: ${{ github.repository_owner }}

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version: '1.21'
          
      - name: Install dependencies
        run: go mod download
        
      - name: Run tests
        run: go test -v ./...
        
      - name: Run linter
        uses: golangci/golangci-lint-action@v3
        with:
          version: latest

  build:
    needs: test
    runs-on: ubuntu-latest
    strategy:
      matrix:
        component: [operator, portal, mom]
    permissions:
      contents: read
      packages: write
    steps:
      - uses: actions/checkout@v3
      
      - name: Install Podman
        run: |
          sudo apt-get update
          sudo apt-get install -y podman
          
      - name: Login to GitHub Container Registry
        run: |
          echo "${{ secrets.GITHUB_TOKEN }}" | podman login ghcr.io -u ${{ github.actor }} --password-stdin
          
      - name: Build and push
        run: |
          podman build -t ${{ env.REGISTRY }}/${{ env.OWNER }}/container-mom-${{ matrix.component }}:${{ github.sha }} -f build/Dockerfile.${{ matrix.component }} .
          podman tag ${{ env.REGISTRY }}/${{ env.OWNER }}/container-mom-${{ matrix.component }}:${{ github.sha }} ${{ env.REGISTRY }}/${{ env.OWNER }}/container-mom-${{ matrix.component }}:latest
          if [ "${{ github.event_name }}" != "pull_request" ]; then
            podman push ${{ env.REGISTRY }}/${{ env.OWNER }}/container-mom-${{ matrix.component }}:${{ github.sha }}
            podman push ${{ env.REGISTRY }}/${{ env.OWNER }}/container-mom-${{ matrix.component }}:latest
          fi

  deploy-dev:
    if: github.ref == 'refs/heads/main'
    needs: build
    runs-on: ubuntu-latest
    environment: development
    steps:
      - uses: actions/checkout@v3
      
      - name: Set up kubectl
        uses: azure/setup-kubectl@v3
        
      - name: Configure kubectl
        run: |
          echo "${{ secrets.KUBECONFIG }}" > kubeconfig.yaml
          chmod 600 kubeconfig.yaml
          
      - name: Deploy to development
        run: |
          export KUBECONFIG=kubeconfig.yaml
          kubectl apply -k deploy/overlays/development
          
      - name: Verify deployment
        run: |
          export KUBECONFIG=kubeconfig.yaml
          kubectl wait --for=condition=available --timeout=300s deployment -l app.kubernetes.io/name=container-mom -n container-mom

  deploy-prod:
    if: github.ref == 'refs/heads/main' && github.event_name == 'push'
    needs: deploy-dev
    runs-on: ubuntu-latest
    environment: production
    steps:
      - uses: actions/checkout@v3
      
      - name: Set up kubectl
        uses: azure/setup-kubectl@v3
        
      - name: Configure kubectl
        run: |
          echo "${{ secrets.KUBECONFIG }}" > kubeconfig.yaml
          chmod 600 kubeconfig.yaml
          
      - name: Deploy to production
        run: |
          export KUBECONFIG=kubeconfig.yaml
          kubectl apply -k deploy/overlays/production
          
      - name: Verify deployment
        run: |
          export KUBECONFIG=kubeconfig.yaml
          kubectl wait --for=condition=available --timeout=300s deployment -l app.kubernetes.io/name=container-mom -n container-mom 