name: Web Deploy 
on:
  push:
    branches: ["main"] #, "develop"] # develop 브랜치 추가하면 변경
    paths:
      - '**'
      - '!**/*.md'
      - '!.gitignore'
      - '!.eas/**'
      - '!.github/**'
      - '.github/workflows/deploy-web.yml'  # 현재 워크플로우는 포함
      # 네이티브 변경 사항
      - '!android/**'
      - '!ios/**'
      - '!*.native.*'

concurrency:
  group: 'deploy-${{ github.ref_name }}' # 브랜치별 독립적 실행
  cancel-in-progress: true

permissions:
  contents: read
  deployments: write

jobs:
  publish:
    name: Publish to Cloudflare Pages (dev) # (${{ github.ref_name }})     # develop 브랜치 추가하면 변경
    runs-on: ubuntu-latest      
    environment: dev # ${{ github.ref_name == 'main' && 'prod' || 'dev' }} # develop 브랜치 추가하면 변경
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v5

      - name: Setup Node.js
        uses: actions/setup-node@v5
        with:  
          node-version: 20.x
          cache: 'npm'

      - name: Install dependencies
        run: npm ci --prefer-offline

      - name: Build project
        run: npx expo export --platform web --output-dir dist
        env:
          PROJECT_ID: ${{ secrets.PROJECT_ID }}
          CLIENT_URL: ${{ secrets.CLIENT_URL }}
          API_BASE_URL: ${{ secrets.API_BASE_URL }}
          # Google OAuth
          GOOGLE_CLIENT_ID_WEB: ${{ secrets.GOOGLE_CLIENT_ID_WEB }}
          # Apple OAuth
          APPLE_CLIENT_ID: ${{ secrets.APPLE_CLIENT_ID }}

      - name: Publish to Cloudflare Pages
        uses: cloudflare/wrangler-action@v3
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          command: pages deploy dist --project-name=${{ vars.CLOUDFLARE_PROJECT_NAME }} --commit-message="Deploy ${{ github.sha }}" --commit-hash=${{ github.sha }} --commit-dirty=true
          gitHubToken: ${{ secrets.GITHUB_TOKEN }}
