defaults:
  run:
    shell: bash

env:
  TZ: Asia/Shanghai

on:
  push:
  workflow_dispatch:
    inputs:
      run_cleanup:
        description: '是否运行 Surge 清理 Job'
        required: false
        type: boolean
        default: true

jobs:
  surge:
    runs-on: ubuntu-latest
    outputs:
      url: ${{ steps.surge_deploy.outputs.url }}
    steps:
      - name: ⚙️ 设置 Node 环境
        uses: yanhao98/composite-actions/setup-node-environment@3bf07469124a5a7b9a06b6d07be36a116c5aa49e
      - name: 🔨 构建项目
        run: pnpm run build-only
        env:
          VITE_BUILD_COMMIT: ${{ github.sha }}
      - name: 🚀 部署到 Surge
        id: surge_deploy
        if: ${{ github.actor != 'nektos/act' }} # https://nektosact.com/usage/index.html#skipping-steps
        uses: yanhao98/composite-actions/deploy-dist-to-surge@3bf07469124a5a7b9a06b6d07be36a116c5aa49e

  playwright:
    needs: surge
    runs-on: ubuntu-latest
    container: mcr.microsoft.com/playwright:v1.53.1-noble
    steps:
      - name: ⚙️ 设置 Node 环境
        uses: yanhao98/composite-actions/setup-node-environment@3bf07469124a5a7b9a06b6d07be36a116c5aa49e
      # - name: 📥 安装 Playwright 浏览器
      #   run: pnpm exec playwright install --with-deps
      - name: ▶️ 运行 Playwright 测试
        run: npx playwright test
        env:
          BASE_URL: ${{ needs.surge.outputs.url }}

  cleanup_surge:
    runs-on: ubuntu-latest
    needs: [surge, playwright]
    if: github.event_name == 'push' || github.event.inputs.run_cleanup == true
    steps:
      - name: 🧹 清理 Surge 部署
        run: npx surge teardown ${{ needs.surge.outputs.url }} --token ${{ env.SURGE_TOKEN }}
        env:
          SURGE_TOKEN: d843de16b331c626f10771245c56ed93
