# -*- coding: utf-8 -*-
"""第六次提交执行（BUG 5+6+7 修复后）。"""
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"
REFRESH_TOKEN = (
    "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
    "eyJzdWIiOiIxIiwic2l0ZV9pZCI6Mjc5MDY4NTQxNTQ0MzI2OSwidHlwZSI6InJlZnJlc2giLCJleHAiOjE3NzIyNjM0NjN9."
    "XYoda5lfxNtTSAGWoLlYhS9cA-hTK9iqK0SqUyn2KV4"
)

# 刷新 token
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}")
    sys.exit(1)
token = resp.json()["access_token"]
Path(__file__).parent.joinpath(".monitor_token").write_text(token, encoding="utf-8")
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}

payload = {
    "tasks": [
        "DWS_ASSISTANT_DAILY", "DWS_ASSISTANT_MONTHLY", "DWS_ASSISTANT_CUSTOMER",
        "DWS_ASSISTANT_SALARY", "DWS_ASSISTANT_FINANCE",
        "ODS_SETTLEMENT_RECORDS", "ODS_PAYMENT", "ODS_REFUND",
        "DWS_BUILD_ORDER_SUMMARY", "DWS_MEMBER_CONSUMPTION", "DWS_MEMBER_VISIT",
        "ODS_GOODS_CATEGORY", "ODS_STORE_GOODS", "ODS_STORE_GOODS_SALES",
        "ODS_TENANT_GOODS", "ODS_PLATFORM_COUPON", "ODS_GROUP_PACKAGE",
        "ODS_GROUP_BUY_REDEMPTION", "ODS_INVENTORY_STOCK", "ODS_INVENTORY_CHANGE",
        "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", "DWD_LOAD_FROM_ODS",
    ],
    "flow": "api_full",
    "processing_mode": "full_window",
    "window_mode": "custom",
    "window_start": "2025-11-01",
    "window_end": "2026-02-20",
    "window_split": "month",
    "window_split_days": 30,
    "force_full": True,
    "dry_run": False,
    "lookback_hours": 24,
    "overlap_seconds": 600,
}
r = requests.post(f"{BASE}/api/execution/run", headers=headers, json=payload, timeout=30)
if r.status_code not in (200, 201):
    print(f"提交失败: {r.status_code} {r.text[:300]}")
    sys.exit(1)

data = r.json()
eid = data.get("execution_id") or data.get("id")
print(f"提交成功: execution_id={eid}")
print(json.dumps(data, ensure_ascii=False, indent=2))
