name: Create Release and Upload Assets

on:
  workflow_dispatch:
  release:
    types: [created]

jobs:
  release:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '22'

      - name: Get version from package.json
        id: get_version
        run: |
          VERSION=$(node -p "require('./package.json').version")
          echo "::set-output name=version::$VERSION"

      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: "v${{ steps.get_version.outputs.version }}"
          release_name: "RustyHearts-API v${{ steps.get_version.outputs.version }}"
          draft: true
          prerelease: false

      - name: Upload Release Assets
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./dist
          asset_name: RustyHearts-API-v${{ steps.get_version.outputs.version }}.zip
          asset_content_type: application/zip
