name: build-and-deploy

on:
  push:
    branches: [ main ]

jobs:
#───────────────── filtra qué dirs cambiaron ─────────────────
  filter:
    runs-on: docker
    outputs:
      agent:  ${{ steps.paths.outputs.agent }}
      api:    ${{ steps.paths.outputs.api }}
      mcp:    ${{ steps.paths.outputs.mcp }}
      ui:     ${{ steps.paths.outputs.ui }}
      worker: ${{ steps.paths.outputs.worker }}
    steps:
      - uses: actions/checkout@v3
      - id: paths
        uses: dorny/paths-filter@v3
        with:
          filters: |
            agent:  "agent/**"
            api:    "api/**"
            mcp:    "mcp/**"
            ui:     "ui/**"
            worker: "worker/**"

#───────────────── build & push ─────────────────
  build:
    needs: filter
    runs-on: docker
    env:
      REG: gitea.interno.com/nucleo000
    steps:
      - uses: actions/checkout@v3
      - uses: docker/setup-buildx-action@v2
      - uses: docker/login-action@v2
        with:
          registry: gitea.interno.com
          username: nucleo000           # ⬅ ponelo en secrets
          password: 7bc7b2fcd283bd6a251bef3ede368b7f897c919d # ⬅ idem          username: ${{ secrets.REGISTRY_USER }}

      - name: Build+push agent
        if: needs.filter.outputs.agent == 'true'
        run: |
          docker build -f agent/Dockerfile -t $REG/planilla-agent:${{ github.sha }} -t $REG/planilla-agent:latest agent
          docker push $REG/planilla-agent:${{ github.sha }}
          docker push $REG/planilla-agent:latest

      - name: Build+push api
        if: needs.filter.outputs.api == 'true'
        run: |
          docker build -f api/Dockerfile -t $REG/planilla-api:${{ github.sha }} -t $REG/planilla-api:latest api
          docker push $REG/planilla-api:${{ github.sha }}
          docker push $REG/planilla-api:latest

      - name: Build+push mcp
        if: needs.filter.outputs.mcp == 'true'
        run: |
          docker build -f mcp/Dockerfile -t $REG/planilla-mcp:${{ github.sha }} -t $REG/planilla-mcp:latest mcp
          docker push $REG/planilla-mcp:${{ github.sha }}
          docker push $REG/planilla-mcp:latest

      - name: Build+push ui
        if: needs.filter.outputs.ui == 'true'
        run: |
          docker build -f ui/Dockerfile -t $REG/planilla-ui:${{ github.sha }} -t $REG/planilla-ui:latest ui
          docker push $REG/planilla-ui:${{ github.sha }}
          docker push $REG/planilla-ui:latest

      - name: Build+push worker
        if: needs.filter.outputs.worker == 'true'
        run: |
          docker build -f worker/Dockerfile -t $REG/planilla-worker:${{ github.sha }} -t $REG/planilla-worker:latest worker
          docker push $REG/planilla-worker:${{ github.sha }}
          docker push $REG/planilla-worker:latest

#───────────────── deploy ─────────────────
  deploy:
    needs: build
    runs-on: docker
    env:
      REG: gitea.interno.com/nucleo000
    steps:
      - uses: actions/checkout@v3
      - name: Login to registry
        run: docker login gitea.interno.com -u nucleo000 -p 7bc7b2fcd283bd6a251bef3ede368b7f897c919d

      - name: Pull fresh images used in compose
        run: docker compose pull

      - name: Clean up stack
        run: docker compose --project-name planilla down

      - name: Update stack
        run: docker compose --project-name planilla up -d --remove-orphans --wait
