name: Daily Update

env:
  TZ: Asia/Shanghai

on:
  workflow_dispatch:
  schedule:
    - cron: '30 19 * * *'

jobs:
  update:
    runs-on: ubuntu-latest

    steps:
      - name: Set up WARP
        uses: fscarmen/warp-on-actions@v1.1
        with:
          stack: dual # Optional. Support [ ipv4, ipv6, dual ]. Default is dual.

      - name: Checkout code
        uses: actions/checkout@v4
        with:
          ref: master

      - name: Set python
        id: setup-python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12.11'

      - name: Install uv
        uses: astral-sh/setup-uv@v6

      - name: Load cached venv
        id: load-cached-venv
        uses: actions/cache@v4
        with:
          path: logi.im/api/.venv
          key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('logi.im/api/uv.lock') }}

      - name: Install python dependencies
        if: steps.load-cached-venv.outputs.cache-hit != 'true'
        run: |
          cd logi.im/api
          uv sync

      - name: Setup node
        uses: actions/setup-node@v4
        with:
          node-version: 20.12.0
          registry-url: https://registry.npmjs.org/

      - name: Generate data
        run: |
          cd logi.im/api
          timeout --preserve-status -k 1m 5m uv run python index.py

      # - name: Update version
      #   run: node --experimental-modules index.mjs

      - name: Commit files
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git commit -am "Update api data"
          git push origin master

      # - name: Publish package
      #   run: |
      #     npm version patch
      #     npm publish
      #   env:
      #     NODE_AUTH_TOKEN: ${{secrets.npm_token}}

      # - name: Refresh cdn
      #   run: |
      #     cd logi.im/api
      #     timeout --preserve-status -k 1m 5m uv run python index.py r
