name: Build and Publish Images

on:
  push:
    branches: [ main, master ]
  workflow_dispatch:

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Build and push backend image
        run: |
          docker build -t 192.168.31.100:3847/arrelin/wedding-backend:latest -t 192.168.31.100:3847/arrelin/wedding-backend:${{ gitea.sha }} ./backend
          docker push 192.168.31.100:3847/arrelin/wedding-backend:latest
          docker push 192.168.31.100:3847/arrelin/wedding-backend:${{ gitea.sha }}

      - name: Build and push frontend image
        run: |
          docker build -t 192.168.31.100:3847/arrelin/wedding-frontend:latest -t 192.168.31.100:3847/arrelin/wedding-frontend:${{ gitea.sha }} ./frontend
          docker push 192.168.31.100:3847/arrelin/wedding-frontend:latest
          docker push 192.168.31.100:3847/arrelin/wedding-frontend:${{ gitea.sha }}

      - name: Logout
        run: docker logout 192.168.31.100:3847

      - name: Trigger Coolify redeploy
        run: |
          curl -s -H "Authorization: Bearer ${{ secrets.COOLIFY_TOKEN }}" \
            "http://192.168.31.100:8000/api/v1/deploy?uuid=o8g00wggwks4gs488kwwg4sw&force=true"
