name: Dev Release

on:
  push:
    branches: [dev]
  workflow_dispatch:

permissions:
  contents: write

env:
  CORE_VERSION: dev

jobs:
  build:
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            platform: linux/amd64
          - os: ubuntu-latest
            platform: linux/arm64
          - os: macos-latest
            platform: darwin/universal
          - os: windows-latest
            platform: windows/amd64
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4

      - name: Build
        uses: host-uk/build@dev
        with:
          build-name: core
          build-platform: ${{ matrix.platform }}
          build: true
          package: true
          sign: false

  release:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Download artifacts
        uses: actions/download-artifact@v4
        with:
          path: dist
          merge-multiple: true

      - name: Prepare release files
        run: |
          mkdir -p release
          cp dist/* release/ 2>/dev/null || true
          ls -la release/

      - name: Delete existing dev release
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: gh release delete dev -y || true

      - name: Delete existing dev tag
        run: git push origin :refs/tags/dev || true

      - name: Create dev release
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release create dev \
            --title "Development Build" \
            --notes "Latest development build from the dev branch.

          **Commit:** ${{ github.sha }}
          **Built:** $(date -u +'%Y-%m-%d %H:%M:%S UTC')

          ## Installation

          \`\`\`bash
          # macOS/Linux
          curl -fsSL https://github.com/host-uk/core/releases/download/dev/core-linux-amd64 -o core
          chmod +x core && sudo mv core /usr/local/bin/
          \`\`\`

          This is a pre-release for testing. Use tagged releases for production." \
            --prerelease \
            --target dev \
            release/*
