name: CI

on:
  push:
  pull_request:
  workflow_dispatch:

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  quality:
    name: Format, metadata, and checks
    runs-on: ubuntu-latest
    timeout-minutes: 20

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install MoonBit
        shell: bash
        run: |
          curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash
          echo "$HOME/.moon/bin" >> "$GITHUB_PATH"

      - name: Show MoonBit version
        shell: bash
        run: moon --version

      - name: Update package registry
        shell: bash
        run: moon update

      # Current MoonBit CLI does not support raw `moon fmt --deny-warn`
      # or `moon info --deny-warn`. The script uses supported strict
      # equivalents and runs deny-warn checks/tests for both targets.
      - name: Competition strict preflight
        shell: bash
        run: ./scripts/competition-check.sh

  build-and-test:
    name: Build and test (${{ matrix.target }})
    runs-on: ubuntu-latest
    timeout-minutes: 25

    strategy:
      fail-fast: false
      matrix:
        target:
          - native
          - wasm-gc

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install MoonBit
        shell: bash
        run: |
          curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash
          echo "$HOME/.moon/bin" >> "$GITHUB_PATH"

      - name: Show MoonBit version
        shell: bash
        run: moon --version

      - name: Update package registry
        shell: bash
        run: moon update

      - name: Build ${{ matrix.target }}
        shell: bash
        run: moon build --target ${{ matrix.target }}

      - name: Test ${{ matrix.target }}
        shell: bash
        run: moon test --target ${{ matrix.target }}

  wasm-demo:
    name: Build Wasm demo
    runs-on: ubuntu-latest
    timeout-minutes: 20

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install MoonBit
        shell: bash
        run: |
          curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash
          echo "$HOME/.moon/bin" >> "$GITHUB_PATH"

      - name: Show MoonBit version
        shell: bash
        run: moon --version

      - name: Update package registry
        shell: bash
        run: moon update

      - name: Build wasm-gc package
        shell: bash
        run: moon build wasm_demo/core --target wasm-gc --release

      - name: Build browser demo asset
        shell: bash
        run: ./wasm-demo/build.sh

      - name: Verify browser demo asset
        shell: bash
        run: test -s wasm-demo/public/moonpost_qc.wasm

  cli-smoke:
    name: CLI smoke tests
    runs-on: ubuntu-latest
    timeout-minutes: 20
    needs:
      - build-and-test

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install MoonBit
        shell: bash
        run: |
          curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash
          echo "$HOME/.moon/bin" >> "$GITHUB_PATH"

      - name: Show MoonBit version
        shell: bash
        run: moon --version

      - name: Update package registry
        shell: bash
        run: moon update

      - name: Build native CLI executable
        shell: bash
        run: moon build cmd/main --target native

      - name: Run timecode smoke test
        shell: bash
        run: |
          _build/native/debug/build/cmd/main/main.exe timecode to-frames 01:00:00:00 --fps 25 > /tmp/timecode.txt
          grep -q '01:00:00:00 @25fps = 90000 frames' /tmp/timecode.txt

      - name: Run subtitle conversion smoke test
        shell: bash
        run: |
          _build/native/debug/build/cmd/main/main.exe subtitle convert examples/good.srt --to webvtt > /tmp/good.vtt
          grep -q '^WEBVTT$' /tmp/good.vtt
          cat > /tmp/mixed.srt <<'EOF'
          1
          00:00:01,000 --> 00:00:03,000
          你好,世界!
          EOF
          _build/native/debug/build/cmd/main/main.exe subtitle normalize /tmp/mixed.srt --punctuation zh > /tmp/mixed-normalized.srt
          grep -q '你好，世界！' /tmp/mixed-normalized.srt

      - name: Run QC smoke test
        shell: bash
        run: |
          _build/native/debug/build/cmd/main/main.exe qc examples/bad.srt --fps 25 --profile streaming > /tmp/qc-report.txt
          grep -q 'summary: 3 errors, 8 warnings' /tmp/qc-report.txt
          grep -q 'ERROR E101 cue#1' /tmp/qc-report.txt
          cat > /tmp/mixed-style.srt <<'EOF'
          1
          00:00:01,000 --> 00:00:03,000
          你好, world！！
          EOF
          _build/native/debug/build/cmd/main/main.exe qc /tmp/mixed-style.srt --text-style bilingual > /tmp/qc-text-style.txt
          grep -q 'W501' /tmp/qc-text-style.txt
          grep -q 'W502' /tmp/qc-text-style.txt

      - name: Run QC fail-on-error smoke test
        shell: bash
        run: |
          set +e
          _build/native/debug/build/cmd/main/main.exe qc examples/bad.srt --fail-on-error > /tmp/qc-fail.txt
          status=$?
          set -e
          test "$status" -eq 1
          grep -q 'summary: 3 errors' /tmp/qc-fail.txt

      - name: Run bilingual CLI smoke test
        shell: bash
        run: |
          _build/native/debug/build/cmd/main/main.exe merge examples/good.srt examples/good.srt --tolerance 0ms > /tmp/merged.srt
          grep -q 'Hello, welcome to MoonBit.' /tmp/merged.srt
          _build/native/debug/build/cmd/main/main.exe split-bilingual examples/bilingual.srt --primary /tmp/primary.srt --secondary /tmp/secondary.srt
          grep -q 'MoonBit makes subtitle tooling compact.' /tmp/secondary.srt

      - name: Run Creator and Delivery smoke tests
        shell: bash
        run: |
          cat > /tmp/mixed.srt <<'EOF'
          1
          00:00:01,000 --> 00:00:03,000
          你好,世界!
          EOF
          cat > /tmp/mixed-style.srt <<'EOF'
          1
          00:00:01,000 --> 00:00:03,000
          你好, world！！
          EOF
          _build/native/debug/build/cmd/main/main.exe creator check /tmp/mixed-style.srt --profile bilingual > /tmp/creator-check.txt
          grep -q 'W501' /tmp/creator-check.txt
          _build/native/debug/build/cmd/main/main.exe creator clean /tmp/mixed.srt --profile douyin > /tmp/creator-clean.srt
          grep -q '你好，世界！' /tmp/creator-clean.srt
          _build/native/debug/build/cmd/main/main.exe delivery subtitle-check examples/bad.srt --profile ott-zh --fps 25 > /tmp/delivery-subtitle.txt
          grep -q 'summary: 3 errors, 8 warnings' /tmp/delivery-subtitle.txt
