name: CI
on:
  push: { branches: [ main ] }
  pull_request:
  release: { types: [ published ] }

jobs:
  generate-go:
    name: Generate Go
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-go@v6
        with: { go-version: '1.25' }
      - run: make install-golangci-lint generate-go
      - run: git diff --exit-code

  generate-ui:
    name: Generate UI
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: oven-sh/setup-bun@v2
      - run: make install-ui generate-ui
      - run: git diff --exit-code

  fmt-go:
    name: Fmt Go
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-go@v6
        with: { go-version: '1.25' }
      - run: make install-golangci-lint fmt-go
      - run: git diff --exit-code

  fmt-ui:
    name: Fmt UI
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: oven-sh/setup-bun@v2
      - run: make install-ui fmt-ui

  lint-go:
    name: Lint Go
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-go@v6
        with: { go-version: '1.25' }
      - run: make install-golangci-lint lint-go

  lint-ui:
    name: Lint UI
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: oven-sh/setup-bun@v2
      - run: make install-ui lint-ui

  build-ui:
    name: Build UI
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: oven-sh/setup-bun@v2
      - run: make install-ui build-ui

  test-go:
    name: Test Go
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-go@v6
        with: { go-version: '1.25' }
      - run: make test-coverage
      - uses: codecov/codecov-action@v5
        with:
          files: ./coverage.out
          token: ${{ secrets.CODECOV_TOKEN }}

  test-ui:
    name: Test UI
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-go@v6
        with: { go-version: '1.25' }
      - uses: oven-sh/setup-bun@v2
      - run: make install-ui test-ui

  test-playwright:
    name: Test Playwright
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-go@v6
        with: { go-version: '1.25' }
      - uses: oven-sh/setup-bun@v2
      - run: make install-ui build-ui install-playwright test-playwright

  list-upgrade-test-folders:
    name: List Upgrade Test Folders
    runs-on: ubuntu-latest
    outputs:
      folders: ${{ steps.set-dirs.outputs.matrix }}
    steps:
      - uses: actions/checkout@v6
      - id: set-dirs
        run: |
          FOLDERS=$(ls -d ./testing/data/*/ | xargs -n 1 basename | jq -R . | jq -c -s .)
          echo "matrix=$FOLDERS" >> $GITHUB_OUTPUT

  test-upgrade-playwright:
    name: Test Playwright Upgrade
    needs: list-upgrade-test-folders
    runs-on: ubuntu-latest
    strategy:
      matrix:
        folder: ${{ fromJson(needs.list-upgrade-test-folders.outputs.folders) }}
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-go@v6
        with: { go-version: '1.25' }
      - uses: oven-sh/setup-bun@v2
      - run: mkdir -p catalyst_data
      - run: cp testing/data/${{ matrix.folder }}/data.db catalyst_data/data.db
      - run: make install-ui build-ui install-playwright test-playwright