name: CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build_plugin:
    runs-on: ubuntu-24.04
    permissions:
      contents: read
      pull-requests: write
      issues: write
    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: Check ktlint
        run: ./gradlew ktlintCheck --no-daemon

      - name: Run tests
        run: ./gradlew test jacocoTestReport --no-daemon

      - name: Upload coverage to Codecov
        if: always()
        uses: codecov/codecov-action@v6
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: |
            engine/build/reports/jacoco/test/jacocoTestReport.xml,
            platform-paper/build/reports/jacoco/test/jacocoTestReport.xml,
            platform-velocity/build/reports/jacoco/test/jacocoTestReport.xml
          fail_ci_if_error: false

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

      - name: Generate Dokka
        run: ./gradlew :dokka:dokkaGenerate --no-daemon

      - name: Upload build artifacts
        uses: actions/upload-artifact@v7
        if: success()
        with:
          name: LunaticChat-${{ github.sha }}
          path: |
            platform-paper/build/libs/LunaticChat-*.jar
            platform-velocity/build/libs/LunaticChat-*-velocity.jar
          retention-days: 7

  build_docs:
    runs-on: ubuntu-24.04
    defaults:
      run:
        working-directory: "./docs"
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

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

      - name: Install dependencies
        run: bun install

      - name: Check formats
        run: bun run format:check

      - name: Check lints
        run: bun run lint

      - name: Build documentation
        run: bun run build
