name: Build and Deploy
on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    container:
      image: catthehacker/ubuntu:act-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Debug Environment
        run: |
          echo "User: $(whoami)"
          echo "PWD: $(pwd)"
          echo "LS /usr/bin/docker: $(ls -l /usr/bin/docker || echo 'Not Found')"
          echo "Which Docker: $(which docker || echo 'Not Found in PATH')"
          env

      - name: Login to Gitea Container Registry
        uses: docker/login-action@v2
        with:
          registry: gitea.blyzer.com.br
          username: ${{ secrets.REGISTRY_USERNAME }}
          password: ${{ secrets.REGISTRY_TOKEN }}

      - name: Build and Push Docker Image
        uses: docker/build-push-action@v4
        with:
          context: .
          push: true
          tags: |
            gitea.blyzer.com.br/${{ gitea.repository }}:latest
            gitea.blyzer.com.br/${{ gitea.repository }}:${{ gitea.sha }}

      - name: Deploy to Portainer
        run: |
          # Requires PORTAINER_WEBHOOK secret (e.g., https://portainer.domain.com/api/stacks/webhooks/...)
          if [ -n "${{ secrets.PORTAINER_WEBHOOK }}" ]; then
            echo "Triggering Portainer Webhook..."
            curl -X POST "${{ secrets.PORTAINER_WEBHOOK }}"
          else
            echo "PORTAINER_WEBHOOK not set. Skipping deployment trigger."
          fi
