name: build-and-push

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: docker
    steps:
      - uses: actions/checkout@v3

      - uses: docker/setup-buildx-action@v2

      - name: Login to registry
        uses: docker/login-action@v2
        with:
          registry: gitea.interno.com
          username: nucleo000           # ⬅ ponelo en secrets
          password: 7bc7b2fcd283bd6a251bef3ede368b7f897c919d # ⬅ idem

      # ──────────────── builds ────────────────
      # - name: Build root image
      #   run: |
      #     docker build -f ./Dockerfile \
      #       -t gitea.interno.com/nucleo000/planilla:${{ github.sha }} \
      #       -t gitea.interno.com/nucleo000/planilla:latest .

      - name: Build AGENT image
        run: |
          docker build -f ./agent/Dockerfile \
            -t gitea.interno.com/nucleo000/planilla-agent:${{ github.sha }} \
            -t gitea.interno.com/nucleo000/planilla-agent:latest \
            ./agent

      - name: Build API image
        run: |
          docker build -f ./api/Dockerfile \
            -t gitea.interno.com/nucleo000/planilla-api:${{ github.sha }} \
            -t gitea.interno.com/nucleo000/planilla-api:latest \
            ./api

      - name: Build MCP image
        run: |
          docker build -f ./mcp/Dockerfile \
            -t gitea.interno.com/nucleo000/planilla-mcp:${{ github.sha }} \
            -t gitea.interno.com/nucleo000/planilla-mcp:latest \
            ./mcp

      - name: Build UI image
        run: |
          docker build -f ./ui/Dockerfile \
            -t gitea.interno.com/nucleo000/planilla-ui:${{ github.sha }} \
            -t gitea.interno.com/nucleo000/planilla-ui:latest \
            ./ui

      - name: Build WORKER image
        run: |
          docker build -f ./worker/Dockerfile \
            -t gitea.interno.com/nucleo000/planilla-worker:${{ github.sha }} \
            -t gitea.interno.com/nucleo000/planilla-worker:latest \
            ./worker

      # ──────────────── push loop ────────────────
      - name: Push images
        run: |
          for img in planilla-agent \
                     planilla-api \
                     planilla-mcp \
                     planilla-ui \
                     planilla-worker; do
            docker push gitea.interno.com/nucleo000/$img:${{ github.sha }}
            docker push gitea.interno.com/nucleo000/$img:latest
          done

  deploy:
    runs-on: docker
    needs: build
    steps:
      - uses: actions/checkout@v3

      - name: Login to registry
        run: docker login gitea.interno.com -u nucleo000 -p 7bc7b2fcd283bd6a251bef3ede368b7f897c919d

      - name: Pull latest images
        run: |
          for img in planilla-agent \
                     planilla-api \
                     planilla-mcp \
                     planilla-ui \
                     planilla-worker; do
            docker pull gitea.interno.com/nucleo000/$img:latest
          done

      - name: Teardown previous stack
        run: docker compose down --remove-orphans || true

      - name: Start all services fresh
        run: docker compose up -d --build --force-recreate --remove-orphans
