# -*- coding: utf-8 -*-
"""第八次执行：验证 BUG 8+9+10+11 修复。"""
import json
import sys
from pathlib import Path

import requests
from dotenv import load_dotenv

load_dotenv(Path(__file__).resolve().parents[2] / ".env")

BASE = "http://localhost:8000"
TOKEN_FILE = Path(__file__).parent / ".monitor_token"
REFRESH_TOKEN = (
    "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
    "eyJzdWIiOiIxIiwic2l0ZV9pZCI6Mjc5MDY4NTQxNTQ0MzI2OSwidHlwZSI6InJlZnJlc2giLCJleHAiOjE3NzIyNjM0NjN9."
    "XYoda5lfxNtTSAGWoLlYhS9cA-hTK9iqK0SqUyn2KV4"
)

resp = requests.post(f"{BASE}/api/auth/refresh", json={"refresh_token": REFRESH_TOKEN}, timeout=10)
if resp.status_code != 200:
    print(f"刷新失败: {resp.status_code} {resp.text}")
    sys.exit(1)
token = resp.json()["access_token"]
TOKEN_FILE.write_text(token, encoding="utf-8")
print("token 已刷新")

headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}

payload = {
    "tasks": [
        "ODS_FETCH", "DWD_LOAD_FROM_ODS",
        "DWS_ASSISTANT_DAILY", "DWS_ASSISTANT_MONTHLY",
        "DWS_ASSISTANT_CUSTOMER", "DWS_ASSISTANT_SALARY",
        "DWS_ASSISTANT_FINANCE",
        "DWS_MEMBER_CONSUMPTION", "DWS_MEMBER_VISIT",
        "DWS_GOODS_STOCK_DAILY", "DWS_GOODS_STOCK_WEEKLY", "DWS_GOODS_STOCK_MONTHLY",
        "DWS_FINANCE_DAILY", "DWS_FINANCE_RECHARGE",
        "DWS_FINANCE_INCOME_STRUCTURE", "DWS_FINANCE_DISCOUNT_DETAIL",
        "DWS_WINBACK_INDEX", "DWS_NEWCONV_INDEX", "DWS_RELATION_INDEX",
    ],
    "flow": "api_full",
    "processing_mode": "full_window",
    "date_range": {"start": "2025-11-01", "end": "2026-02-20"},
    "window_days": 30,
    "force_full": True,
}

r = requests.post(f"{BASE}/api/execution/run", headers=headers, json=payload, timeout=30)
print(f"status={r.status_code}")
data = r.json()
print(json.dumps(data, ensure_ascii=False, indent=2))

if r.status_code == 200:
    eid = data.get("execution_id") or data.get("id")
    print(f"\nexecution_id: {eid}")
