name: Release

on:
  push:
    tags:
      - 'v*'

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

      - name: Extract version from build.gradle.kts
        id: get_version
        run: |
          VERSION=$(grep '    version = ' build.gradle.kts | sed 's/.*version = "\(.*\)"/\1/')
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "paper_jar_name=LunaticChat-$VERSION.jar" >> $GITHUB_OUTPUT
          echo "velocity_jar_name=LunaticChat-$VERSION-velocity.jar" >> $GITHUB_OUTPUT
          echo "folia_jar_name=LunaticChat-$VERSION.jar" >> $GITHUB_OUTPUT
          echo "Extracted version: $VERSION"

      - name: Validate tag matches version
        run: |
          TAG_VERSION=${GITHUB_REF#refs/tags/v}
          GRADLE_VERSION="${{ steps.get_version.outputs.version }}"

          if [ "$TAG_VERSION" != "$GRADLE_VERSION" ]; then
            echo "Error: Tag version ($TAG_VERSION) does not match build.gradle.kts version ($GRADLE_VERSION)"
            exit 1
          fi
          echo "Version validation passed: $TAG_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@v3

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

      - 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@v6
        with:
          name: LunaticChat-v${{ needs.validate.outputs.version }}
          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: beta
      MODRINTH_GAME_VERSIONS: "1.21.x"
      MODRINTH_MC_VERSION: "1.21.11"
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Download build artifact
        uses: actions/download-artifact@v7
        with:
          name: LunaticChat-v${{ needs.validate.outputs.version }}

      - name: Get current date
        id: get_date
        run: echo "current_date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT

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

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

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

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

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

