name: Deploy CULTSCALE OS (Worker)

on:
  push:
    branches:
      - main
    paths:
      - 'portal/**'
      - '.github/workflows/deploy-cultscale-os.yml'
  workflow_dispatch:

jobs:
  deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      deployments: write

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

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '22'
          cache: 'npm'
          cache-dependency-path: portal/package-lock.json

      - name: Install portal dependencies
        working-directory: portal
        run: npm ci

      - name: Deploy Worker
        uses: cloudflare/wrangler-action@v3
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          workingDirectory: portal
          wranglerVersion: "4.60.0"
          command: |
            d1 migrations apply cultscale-os --remote
            deploy --config wrangler.toml
          gitHubToken: ${{ secrets.GITHUB_TOKEN }}
