name: Build release and push to Github

on:
  push:
    branches: [ main ]

env:
  CACHE_ZIP_FILENAME: cache.zip
  ASSETS_FOLDER: data/operator
  RELEASE_FOLDER: release
  CACHE_BASE_KEY: akassets

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Use PNPM
        uses: pnpm/action-setup@v4
        with:
          version: latest
          run_install: false
      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: latest
          cache: 'pnpm'
      - name: Install dependencies
        run: pnpm i
      - name: Restore cached assets
        id: cache-akassets-restore
        uses: actions/cache@v4
        with:
          path: |
            ${{ env.CACHE_ZIP_FILENAME }}
          key: ${{ env.CACHE_BASE_KEY }}-${{ hashFiles('offical_update.json') }}
          restore-keys: |
            ${{ env.CACHE_BASE_KEY }}
      - name: Unzip assets
        run: |
          if test -f ${{ env.CACHE_ZIP_FILENAME }}; then
            unzip -qq ${{ env.CACHE_ZIP_FILENAME }} -d .
          fi
        shell: bash
      - name: Download Assets
        run: pnpm run cf:download
      - name: Build all
        run: pnpm run operator:build-all
      - name: Build directory
        run: pnpm run directory:build
      - name: Publish to Cloudflare Pages
        uses: cloudflare/pages-action@v1
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          projectName: aklive2d
          directory: ${{ env.RELEASE_FOLDER}}
          wranglerVersion: '3'
      - name: Zip assets
        run: zip -qq -9 -r ${{ env.CACHE_ZIP_FILENAME }} ${{ env.ASSETS_FOLDER }}
      - name: Save assets
        id: cache-akassets-save
        uses: actions/cache/save@v4
        with:
          path: |
            ${{ env.CACHE_ZIP_FILENAME }}
          key: ${{ env.CACHE_BASE_KEY }}-${{ hashFiles('offical_update.json') }}
