---
name: release
on:
  workflow_dispatch:

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  bump-version:
    name: Bump Version and Push Tag
    runs-on: ubuntu-latest
    permissions:
      contents: write
    outputs:
      version: ${{ env.NBGV_SemVer2 }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Determine Version
        uses: dotnet/nbgv@master
        id: ngbv
        with:
          setAllVars: true
  release:
    runs-on: ${{ matrix.os }}
    permissions:
      contents: write
      packages: write
      attestations: write
      id-token: write
    strategy:
      matrix:
        os: [ubuntu-latest]
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      # https://github.com/marketplace/actions/nerdbank-gitversioning
      - name: Nerdbank.GitVersioning
        uses: dotnet/nbgv@v0.4.2
        with:
          setAllVars: true

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: "8.3"

      - uses: olegtarasov/get-tag@v2.1.2
        id: tagName

      # Configure Caching
      - name: Get composer cache directory
        id: composer-cache
        run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

      - name: Cache dependencies
        uses: actions/cache@v4
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: ${{ runner.os }}-composer-

        # Dependencies
      - name: "Install Release Dependencies"
        run: |
          rm -rf vendor
          composer install --no-dev --prefer-dist --no-interaction --verbose
          composer dump-autoload
          sudo chmod +x ./.github/scripts/*

      - name: Install NPM dependencies
        run: npm install

      - name: Compile assets
        run: npm run build

      - name: Get version
        run: .github/scripts/version.sh

      - name: Build Distro
        run: .github/scripts/build.sh

      # Dependencies
      - name: "List files"
        run: |
          find . -maxdepth 2  -type f

      - name: Create Release
        uses: ncipollo/release-action@v1
        with:
          artifacts: "dist/*.*"
          allowUpdates: true
          generateReleaseNotes: true
          name: ${{ github.ref_name }}
          tag: ${{ github.ref_name }}
          token: ${{ secrets.GITHUB_TOKEN }}

      - uses: BetaHuhn/do-spaces-action@v2
        id: spaces
        with:
          access_key: ${{ secrets.SPACE_ACCESS_KEY}}
          secret_key: ${{ secrets.SPACE_SECRET_KEY }}
          space_name: ${{ secrets.SPACE_NAME }}
          space_region: ${{ secrets.SPACE_REGION }}
          source: dist
          cdn_domain: phpvms.cdn.vmslabs.net
          permission: public-read

      # Push to Docker
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
          flavor: |
            latest=auto
          tags: |
            type=ref,event=branch
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}

      - name: Build and push
        id: push
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

      - name: Generate artifact attestation
        uses: actions/attest-build-provenance@v2
        with:
          subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
          subject-digest: ${{ steps.push.outputs.digest }}
          push-to-registry: true

      - name: Discord notification
        uses: Ilshidur/action-discord@0.3.2
        env:
          DISCORD_WEBHOOK: ${{ secrets.V8_DISCORD_WEBHOOK }}
        with:
          args: ${{ steps.discord.outputs.discord_msg }}
