name: auto-author (stub)

on:
  schedule:
    - cron: "0 14 * * *"   # 每天 14:00 UTC(可改)
  workflow_dispatch:
    inputs:
      dry_run:
        description: "试跑(不创建 Issue)"
        type: boolean
        default: false

permissions:
  contents: read
  issues: write

jobs:
  stub:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"

      - name: Install deps
        run: |
          pip install requests

      - name: Run stub author
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          REPO: ${{ github.repository }}       # e.g. tcq20256/puck.chat
          SERIES_NAME: ${{ vars.SERIES_NAME }} # 可选变量
          DRY_RUN: ${{ inputs.dry_run }}
        run: |
          python - <<'PY'
          import os, sys, requests, datetime, textwrap

          REPO_FULL = os.environ.get("REPO")           # "owner/repo"
          TOKEN = os.environ.get("GITHUB_TOKEN")
          SERIES = os.environ.get("SERIES_NAME") or "雾钟巷·蝶梦"
          DRY = str(os.environ.get("DRY_RUN","false")).lower()=="true"
          if not REPO_FULL or not TOKEN:
              print("Missing env REPO/GITHUB_TOKEN", file=sys.stderr); sys.exit(1)

          GH = "https://api.github.com"
          hdr = {"Authorization": f"token {TOKEN}", "Accept":"application/vnd.github+json"}

          # 今天是否已发(带 auto 标签)
          today = datetime.datetime.utcnow().date().isoformat()
          r = requests.get(f"{GH}/repos/{REPO_FULL}/issues",
                           headers=hdr, params={"state":"open","labels":"auto"}, timeout=30)
          r.raise_for_status()
          for it in r.json():
              if (it.get("created_at","") or "")[:10] == today:
                  print("Already posted today. Exit."); sys.exit(0)

          # 组装占位稿
          cn_now = (datetime.datetime.utcnow()
                    + datetime.timedelta(hours=8)).strftime("%Y-%m-%d %H:%M")
          title = f"{SERIES}|占位稿 {today}"
          body = textwrap.dedent(f"""
          > 这是自动写作占位稿(无 API 版),用于验证“自动创建 Issue → 发布站点”的流水线。
          >
          > 生成时间(CST+08:00):**{cn_now}**

          ## 小节
          - 这是一段占位内容。
          - 你可以手动编辑这条 Issue:增加正文、图片或去掉 `draft` 标签,即可作为正式发布。

          _占位标签:#auto #draft #novel_
          """).strip()

          labels = ["auto","draft","novel"]  # draft=半自动,确认后可去掉
          if DRY:
              print("[DRY RUN] Would create issue:", title)
              print(body[:400]+"...")
              sys.exit(0)

          # 创建 Issue
          payload = {"title": title, "body": body, "labels": labels}
          cr = requests.post(f"{GH}/repos/{REPO_FULL}/issues",
                             headers=hdr, json=payload, timeout=30)
          cr.raise_for_status()
          url = cr.json().get("html_url")
          print("Created:", url)
          PY
❤️ 转载文章请注明出处,谢谢!❤️