from __future__ import annotations

from copy import copy
from datetime import datetime
from pathlib import Path
import sys

import openpyxl


ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
    sys.path.insert(0, str(ROOT))
CASE_BOOK = ROOT / "测试用例" / "SaaS_E2E测试用例_单Sheet用例测试列表.xlsx"
FLOW_BOOK = ROOT / "测试用例" / "订单状态流转_链路流程线整理.xlsx"


CASE_UPDATES = {
    "S-FLOW-054": {
        "跨端/状态断言": "1. 处罚记录的主单号、来源单号、子单号、责任方、金额在丸掌柜侧一致。\n"
        "2. 处罚只作用于目标子单，不误影响同主单其他子单。\n"
        "3. 后续由 S-FLOW-055 或 S-FLOW-056 验证丸友集处理结果。",
        "可组合/被引用关系": "承接 S-FLOW-049 后的已验收/验收成功子单；后续分支为 S-FLOW-055/S-FLOW-056；被 FLOW-030、FLOW-045 引用。",
        "自动化状态": "已自动化",
        "备注": "2026-07-02 修正：S-FLOW-049 后接验收成功子单处罚，后续分支为 S-FLOW-055/S-FLOW-056。",
    },
    "S-FLOW-055": {
        "用例标题": "【丸友集】验收成功子单处罚-同意赔偿",
        "预期结果": "1. 丸友集侧目标处罚/申诉记录处理完成或显示已完成/已通过。\n"
        "2. 处罚金额等于申请处罚金额。\n"
        "3. 丸掌柜侧子单处罚单同步为已完成或同意赔偿结果。",
        "跨端/状态断言": "丸友集申诉工单、丸掌柜子单处罚单按丸掌柜主单号/来源单号可定位同一记录；处理状态和金额一致。",
        "可组合/被引用关系": "承接 S-FLOW-054；FLOW-030 处罚同意赔偿分支引用。",
        "自动化状态": "已自动化",
        "备注": "2026-07-02 修正：作为 S-FLOW-054 后续同意赔偿分支，不再仅作为 S-FLOW-063 别名。",
    },
    "S-FLOW-056": {
        "用例标题": "【丸友集】验收成功子单处罚-驳回处罚",
        "预期结果": "1. 丸友集侧目标处罚/申诉记录处理完成或显示已拒绝/已驳回。\n"
        "2. 处罚金额为0或页面对应未赔偿结果。\n"
        "3. 丸掌柜侧子单处罚单同步为已拒绝或驳回结果。",
        "跨端/状态断言": "丸友集申诉工单、丸掌柜子单处罚单按丸掌柜主单号/来源单号可定位同一记录；驳回状态和金额结果一致。",
        "可组合/被引用关系": "承接 S-FLOW-054；FLOW-045 处罚驳回分支引用。",
        "自动化状态": "已自动化",
        "备注": "2026-07-02 修正：作为 S-FLOW-054 后续驳回处罚分支，不再仅作为 S-FLOW-064 别名。",
    },
}


def headers(ws) -> dict[str, int]:
    return {str(ws.cell(1, col).value or ""): col for col in range(1, ws.max_column + 1)}


def copy_row_style(ws, src_row: int, dst_row: int) -> None:
    for col in range(1, ws.max_column + 1):
        src = ws.cell(src_row, col)
        dst = ws.cell(dst_row, col)
        if src.has_style:
            dst._style = copy(src._style)
        dst.font = copy(src.font)
        dst.fill = copy(src.fill)
        dst.border = copy(src.border)
        dst.alignment = copy(src.alignment)
        dst.number_format = src.number_format


def delete_rows_by_first_col(ws, keys: set[str]) -> None:
    for row in range(ws.max_row, 1, -1):
        if str(ws.cell(row, 1).value or "") in keys:
            ws.delete_rows(row)


def update_case_book() -> None:
    wb = openpyxl.load_workbook(CASE_BOOK)
    ws = wb["订单状态流转"]
    h = headers(ws)
    for row in range(2, ws.max_row + 1):
        case_id = str(ws.cell(row, h["用例编号"]).value or "")
        updates = CASE_UPDATES.get(case_id)
        if not updates:
            continue
        for key, value in updates.items():
            if key in h:
                ws.cell(row, h[key]).value = value
    wb.save(CASE_BOOK)


def sync_flow_book_from_generator() -> None:
    from scripts.generate_order_status_flow_lines_excel import main as generate_flow_lines

    generate_flow_lines()
    wb = openpyxl.load_workbook(FLOW_BOOK)
    if "说明" not in wb.sheetnames:
        ws = wb.create_sheet("说明")
        ws.append(["类型", "内容"])
    wb["说明"].append(["更新记录", f"{datetime.now():%Y-%m-%d %H:%M:%S} FLOW-030 改为 S-FLOW-049 -> S-FLOW-054 -> S-FLOW-055；新增 FLOW-045 处罚驳回分支。"])
    wb.save(FLOW_BOOK)


def main() -> None:
    update_case_book()
    sync_flow_book_from_generator()
    print("FLOW-030/FLOW-045 penalty branches synced")


if __name__ == "__main__":
    main()
