name: Pipeline

on:
  push:
    branches: [main, ci]
  pull_request:
    branches: [main]
  workflow_dispatch:

jobs:
  docker:
    runs-on: ubuntu-latest
    outputs:
      image_tags: ${{ steps.meta.outputs.tags || steps.meta-pr.outputs.tags }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 1

      - name: Setup environment
        run: |
          echo "OWNER_LOWERCASE=${GITHUB_REPOSITORY_OWNER,,}" >> ${GITHUB_ENV}

      - name: Inject slug/short variables
        uses: rlespinasse/github-slug-action@v5.0.0

      - name: Setup Docker
        uses: docker/setup-buildx-action@v3
        with:
          install: true
          driver-opts: |
            image=moby/buildkit:master
            network=host

      - name: Login to Registry
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v3
        with:
          registry: git.littlevibe.net
          username: ${{ github.actor }}
          password: ${{ secrets.LITTLEVIBE_ACCESS_TOKEN }}

      - name: Configure Docker Metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: git.littlevibe.net/${{ env.OWNER_LOWERCASE }}/www
          tags: |
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }}
            type=raw,value=sha-${{ env.GITHUB_SHA_SHORT }}
          labels: |
            maintainer=${{ github.repository_owner }}
            org.opencontainers.image.source="https://git.littlevibe.net/${{ github.repository_owner }}/www"

      - name: Build and Push
        id: build-and-push
        uses: docker/build-push-action@v6
        with:
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
          build-args: |
            GIT_SHA=${{ env.GITHUB_SHA }}
            DOCKER_LABEL=sha-${{ env.GITHUB_SHA_SHORT }}

      - name: Notify Build Status
        if: always()
        run: |
          STATUS_EMOJI="${{ job.status == 'success' && '✅' || '❌' }}"
          STATUS_TEXT="${{ job.status == 'success' && 'white_checkmark' || 'x' }}"

          curl \
            -H "Authorization: Bearer ${{ secrets.NTFY_API_TOKEN }}" \
            -H "Title: Docker Build: $STATUS_EMOJI" \
            -H "Tags: whale,cd,${STATUS_TEXT}" \
            -H "Priority: ${{ job.status == 'success' && '3' || '5' }}" \
            -H "Click: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" \
            -H "Markdown: yes" \
            -d "### Build Details
            - **Repository**: \`${{ github.repository }}\`
            - **Branch/Tag**: \`${{ github.ref_name }}\`
            - **Commit**: [\`${GITHUB_SHA:0:7}\`](${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }})
            - **Image Tags**: \`${{ steps.meta.outputs.tags }}\`

            ${STATUS_EMOJI} Build **${{ job.status }}**

            [View Run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
            [View Container](${{ github.server_url }}/${{ env.OWNER_LOWERCASE }}/-/packages/container/${{ github.repository }})" \
            ${{ secrets.NTFY_URL }}

  deploy:
    needs: docker
    if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
    runs-on: ubuntu-latest
    concurrency: deploy-group    # optional: ensure only one action runs at a time
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 1

      - name: Deploy to Fly.io
        uses: superfly/flyctl-actions/setup-flyctl@master
      - run: flyctl deploy --local-only
        env:
          FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}

      - name: Notify Deploy Status
        if: always()
        run: |
          STATUS_EMOJI="${{ job.status == 'success' && '✅' || '❌' }}"
          STATUS_TEXT="${{ job.status == 'success' && 'white_checkmark' || 'x' }}"

          curl \
            -H "Authorization: Bearer ${{ secrets.NTFY_API_TOKEN }}" \
            -H "Title: Fly.io Deployment: $STATUS_EMOJI" \
            -H "Tags: rocket,cd,${STATUS_TEXT}" \
            -H "Priority: ${{ job.status == 'success' && '3' || '5' }}" \
            -H "Click: https://fly.io/apps/${{ github.event.repository.name }}" \
            -H "Markdown: yes" \
            -d "### Deployment Details
            - **Application**: \`${{ github.event.repository.name }}\`
            - **Environment**: \`production\`
            - **Branch**: \`${{ github.ref_name }}\`
            - **Commit**: [\`${GITHUB_SHA:0:7}\`](${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }})

            ${STATUS_EMOJI} Deployment **${{ job.status }}**

            [View App](https://fly.io/apps/${{ github.event.repository.name }})
            [View Logs](https://fly.io/apps/${{ github.event.repository.name }}/monitoring)" \
            ${{ secrets.NTFY_URL }}
