name: publish
run-name: "${{ format('release {0}', inputs.bump) }}"

on:
  workflow_dispatch:
    inputs:
      bump:
        description: "Bump major, minor, or patch"
        required: true
        type: choice
        options:
          - major
          - minor
          - patch
      version:
        description: "Override version (optional)"
        required: false
        type: string

concurrency: ${{ github.workflow }}-${{ github.ref }}

permissions:
  contents: write
  id-token: write

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - name: Install dependencies
        run: bun install
        env:
          BUN_INSTALL_ALLOW_SCRIPTS: "@ast-grep/napi"

      - name: Run tests
        run: bun test

  typecheck:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - name: Install dependencies
        run: bun install
        env:
          BUN_INSTALL_ALLOW_SCRIPTS: "@ast-grep/napi"

      - name: Type check
        run: bun run typecheck

  publish:
    runs-on: ubuntu-latest
    needs: [test, typecheck]
    if: github.repository == 'code-yeongyu/oh-my-opencode'
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - run: git fetch --force --tags

      - uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - uses: actions/setup-node@v4
        with:
          node-version: "24"

      - name: Upgrade npm for OIDC trusted publishing
        run: npm install -g npm@latest

      - name: Configure npm registry
        run: npm config set registry https://registry.npmjs.org

      - name: Install dependencies
        run: bun install
        env:
          BUN_INSTALL_ALLOW_SCRIPTS: "@ast-grep/napi"

      - name: Debug environment
        run: |
          echo "=== Bun version ==="
          bun --version
          echo "=== Node version ==="
          node --version
          echo "=== Current directory ==="
          pwd
          echo "=== List src/ ==="
          ls -la src/
          echo "=== package.json scripts ==="
          cat package.json | jq '.scripts'

      - name: Build
        run: |
          echo "=== Running bun build (main) ==="
          bun build src/index.ts src/google-auth.ts --outdir dist --target bun --format esm --external @ast-grep/napi
          echo "=== Running bun build (CLI) ==="
          bun build src/cli/index.ts --outdir dist/cli --target bun --format esm
          echo "=== Running tsc ==="
          tsc --emitDeclarationOnly
          echo "=== Running build:schema ==="
          bun run build:schema
      
      - name: Verify build output
        run: |
          echo "=== dist/ contents ==="
          ls -la dist/
          echo "=== dist/cli/ contents ==="
          ls -la dist/cli/
          test -f dist/index.js || (echo "ERROR: dist/index.js not found!" && exit 1)
          test -f dist/cli/index.js || (echo "ERROR: dist/cli/index.js not found!" && exit 1)

      - name: Publish
        run: bun run script/publish.ts
        env:
          BUMP: ${{ inputs.bump }}
          VERSION: ${{ inputs.version }}
          CI: true
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_CONFIG_PROVENANCE: true

      - name: Delete draft release
        run: gh release delete next --yes 2>/dev/null || echo "No draft release to delete"
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Merge to master
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          VERSION=$(jq -r '.version' package.json)
          git checkout master
          git reset --hard "v${VERSION}"
          git push -f origin master
