name: Release

on:
  push:
    tags:
      - 'v*'

jobs:
  validate:
    runs-on: ubuntu-24.04
    outputs:
      paper_version: ${{ steps.get_version.outputs.paper_version }}
      velocity_version: ${{ steps.get_version.outputs.velocity_version }}
      paper_jar_name: ${{ steps.get_version.outputs.paper_jar_name }}
      velocity_jar_name: ${{ steps.get_version.outputs.velocity_jar_name }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Extract versions from gradle.properties
        id: get_version
        run: |
          PAPER_VERSION=$(grep '^paperVersion=' gradle.properties | cut -d'=' -f2)
          VELOCITY_VERSION=$(grep '^velocityVersion=' gradle.properties | cut -d'=' -f2)

          echo "paper_version=$PAPER_VERSION" >> $GITHUB_OUTPUT
          echo "velocity_version=$VELOCITY_VERSION" >> $GITHUB_OUTPUT
          echo "paper_jar_name=LunaticChat-$PAPER_VERSION.jar" >> $GITHUB_OUTPUT
          echo "velocity_jar_name=LunaticChat-$VELOCITY_VERSION-velocity.jar" >> $GITHUB_OUTPUT
          echo "Paper version: $PAPER_VERSION, Velocity version: $VELOCITY_VERSION"

      - name: Check if release already exists
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          TAG_NAME=${GITHUB_REF#refs/tags/}
          if gh release view "$TAG_NAME" &> /dev/null; then
            echo "Error: Release $TAG_NAME already exists"
            exit 1
          fi
          echo "Release validation passed: $TAG_NAME does not exist yet"

  build:
    runs-on: ubuntu-24.04
    needs: validate
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Setup Development Environment
        uses: jdx/mise-action@v4

      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v6

      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      - name: Build with shadowJar
        run: ./gradlew :platform-paper:shadowJar :platform-velocity:shadowJar --parallel --no-daemon

      - name: Upload build artifacts
        uses: actions/upload-artifact@v7
        with:
          name: LunaticChat-${{ github.ref_name }}
          path: |
            platform-paper/build/libs/${{ needs.validate.outputs.paper_jar_name }}
            platform-velocity/build/libs/${{ needs.validate.outputs.velocity_jar_name }}
          retention-days: 90

  release:
    runs-on: ubuntu-24.04
    needs: [validate, build]
    permissions:
      contents: write
    env:
      MODRINTH_CHANNEL: release
      MODRINTH_PAPER_GAME_VERSIONS: "26.1.x"
      MODRINTH_VELOCITY_GAME_VERSIONS: |-
        1.21.x
        26.1.x
      MODRINTH_MC_VERSION: "26.1"
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Download build artifact
        uses: actions/download-artifact@v8
        with:
          name: LunaticChat-${{ github.ref_name }}

      - name: Create GitHub Release
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          TAG_NAME=${GITHUB_REF#refs/tags/}
          PAPER_VERSION=${{ needs.validate.outputs.paper_version }}
          VELOCITY_VERSION=${{ needs.validate.outputs.velocity_version }}
          PAPER_JAR_NAME=${{ needs.validate.outputs.paper_jar_name }}
          VELOCITY_JAR_NAME=${{ needs.validate.outputs.velocity_jar_name }}

          cat > /tmp/release-notes.md <<EOF
          LunaticChat ${TAG_NAME} has been released.

          | Platform | Version |
          |----------|---------|
          | Paper/Folia | ${PAPER_VERSION} |
          | Velocity | ${VELOCITY_VERSION} |

          ## Download

          - [GitHub](https://github.com/m1sk9/LunaticChat/releases/tag/${TAG_NAME})
          - Modrinth: [Paper/Folia](https://modrinth.com/plugin/lunaticchat/version/${PAPER_VERSION}), [Velocity](https://modrinth.com/plugin/lunaticchat/version/${VELOCITY_VERSION}-velocity)

          ## What's new

          ### Highlights

          ### New Features

          ### Improvements

          ### Changes

          ### Bug Fixes

          ### Notes

          EOF

          gh release create "$TAG_NAME" \
            "platform-paper/build/libs/$PAPER_JAR_NAME" \
            "platform-velocity/build/libs/$VELOCITY_JAR_NAME" \
            --title "$TAG_NAME" \
            --draft \
            --notes-file /tmp/release-notes.md

      - name: Publish to Modrinth (Paper/Folia)
        uses: cloudnode-pro/modrinth-publish@0be4916ad5f081d936eb5615aa35ce3f0949979c # v2
        with:
          token: ${{ secrets.MODRINTH_TOKEN }}
          name: v${{ needs.validate.outputs.paper_version }} (Paper/Folia v${{ env.MODRINTH_MC_VERSION }})
          project: ${{ secrets.MODRINTH_PROJECT_ID }}
          version: ${{ needs.validate.outputs.paper_version }}
          channel: ${{ env.MODRINTH_CHANNEL }}
          changelog: |-
            Please refer to the [GitHub release](https://github.com/m1sk9/LunaticChat/releases/tag/${{ github.ref_name }}) for update details.
          loaders: |-
            paper
            folia
          game-versions: ${{ env.MODRINTH_PAPER_GAME_VERSIONS }}
          files: platform-paper/build/libs/${{ needs.validate.outputs.paper_jar_name }}

      - name: Publish to Modrinth (Velocity)
        uses: cloudnode-pro/modrinth-publish@0be4916ad5f081d936eb5615aa35ce3f0949979c # v2
        with:
          token: ${{ secrets.MODRINTH_TOKEN }}
          name: v${{ needs.validate.outputs.velocity_version }} (Velocity)
          project: ${{ secrets.MODRINTH_PROJECT_ID }}
          version: ${{ needs.validate.outputs.velocity_version }}-velocity
          channel: ${{ env.MODRINTH_CHANNEL }}
          changelog: |-
            Please refer to the [GitHub release](https://github.com/m1sk9/LunaticChat/releases/tag/${{ github.ref_name }}) for update details.
          loaders: velocity
          game-versions: ${{ env.MODRINTH_VELOCITY_GAME_VERSIONS }}
          files: platform-velocity/build/libs/${{ needs.validate.outputs.velocity_jar_name }}
