from __future__ import annotations

from pathlib import Path

from openpyxl import load_workbook


ROOT = Path(__file__).resolve().parents[1]

FLOW_032_SUMMARY = {
    "链路名称": "丸友集已接单未发打手-子单发起售后-取消子单售后",
    "链路类型": "已接单未发打手子单售后取消",
    "起点状态": "丸友集已接单；未发打手；丸掌柜主单服务中/子单代练中",
    "终点状态": "恢复子单售后前状态；子单不残留售后中",
    "用例编号序列": "S-FLOW-007 -> S-FLOW-014 -> S-FLOW-058 -> S-FLOW-036",
    "用例链路（编号+标题）": "S-FLOW-007(【丸掌柜】主单待接单状态/子单待接单-已推单状态-创建主单已推单) -> S-FLOW-014(丸友集抢单大厅待接单阶段-丸友集确认接单) -> S-FLOW-058(丸友集已接单/未发单到打手抢单大厅阶段&&丸掌柜主单服务中状态/子单代练中状态-丸掌柜子单发起售后) -> S-FLOW-036(【丸友集】子单售后中状态-拒绝/取消子单售后)",
    "涉及模块": "上游发单 -> 丸掌柜发起售后",
    "适用场景": "验证工作室接单但未发打手阶段，丸掌柜从子单列表行内入口发起子单售后后取消。",
    "数据复用建议": "同一订单可先取消子单售后，再按页面规则继续后续处理。",
    "注意事项/缺口": "同 FLOW-011，需要页面确认实际状态名；取消对象必须是子单售后记录。",
}

FLOW_032_STEPS = [
    {
        "步骤序号": 1,
        "用例编号": "S-FLOW-007",
        "所属模块": "上游发单",
        "用例标题": "【丸掌柜】主单待接单状态/子单待接单-已推单状态-创建主单已推单",
        "前置条件": "丸掌柜账号可创建外来订单；手工录单页面选择推单，并存在可用丸友集推单策略。",
        "预期结果": "1. 丸掌柜主单订单状态为待接单。\n2. 丸掌柜子单状态为待接单-已推单。\n3. 丸友集抢单大厅可以查询到该订单，丸友集父单状态为待接单。\n4. 打手侧查询不到此单。",
    },
    {
        "步骤序号": 2,
        "用例编号": "S-FLOW-014",
        "所属模块": "上游发单",
        "用例标题": "丸友集抢单大厅待接单阶段-丸友集确认接单",
        "前置条件": "丸掌柜订单已推送到丸友集抢单大厅，丸友集尚未确认接单。",
        "预期结果": "1. 丸友集父单从抢单大厅进入订单列表，状态为已接单或页面展示的承接状态。\n2. 丸掌柜主单订单状态仍为待接单或页面同步后的待下游处理状态。\n3. 丸掌柜子单状态仍不应被误改为已完成。\n4. 打手侧仍查询不到此单。",
    },
    {
        "步骤序号": 3,
        "用例编号": "S-FLOW-058",
        "所属模块": "丸掌柜发起售后",
        "用例标题": "丸友集已接单/未发单到打手抢单大厅阶段&&丸掌柜主单服务中状态/子单代练中状态-丸掌柜子单发起售后",
        "前置条件": "丸友集已在抢单大厅确认接单，订单进入丸友集订单列表，尚未点击立即发单给打手；丸掌柜订单详情主单状态为服务中或代练中，子单汇总状态为代练中，子单列表存在目标子单且该行展示子单售后入口。",
        "预期结果": "1. 丸掌柜通过子单列表目标子单的行内入口成功发起子单售后。\n2. 目标子单进入售后中或页面对应售后处理状态。\n3. 丸掌柜订单进入售后中或页面实际售后聚合状态。\n4. 丸友集父单进入售后中或页面对应售后处理状态。\n5. 售后原因、售后金额和凭证在该子单售后记录中保存成功。",
    },
    {
        "步骤序号": 4,
        "用例编号": "S-FLOW-036",
        "所属模块": "丸掌柜取消售后",
        "用例标题": "丸友集已接单/未发单到打手抢单大厅阶段&&丸掌柜主单服务中状态/子单售后中状态-丸掌柜取消子单售后",
        "前置条件": "已执行 S-FLOW-058，通过子单列表行内入口发起子单售后，当前目标子单处于售后中且未处理完成。",
        "预期结果": "1. 目标子单售后被取消，目标子单恢复到发起子单售后前的代练中或页面实际可继续处理状态。\n2. 丸掌柜订单不再展示该子单售后中。\n3. 丸友集父单恢复到售后发起前的可见性和业务状态。\n4. 售后取消原因被记录。",
    },
]


def update_workbook(path: Path) -> None:
    wb = load_workbook(path)
    if "链路流程线" in wb.sheetnames:
        ws = wb["链路流程线"]
        headers = {str(ws.cell(1, col).value or ""): col for col in range(1, ws.max_column + 1)}
        for row in range(2, ws.max_row + 1):
            if str(ws.cell(row, headers["链路编号"]).value or "") == "FLOW-032":
                for header, value in FLOW_032_SUMMARY.items():
                    col = headers.get(header)
                    if col:
                        ws.cell(row, col).value = value
                break
    if "链路步骤明细" in wb.sheetnames:
        ws = wb["链路步骤明细"]
        headers = {str(ws.cell(1, col).value or ""): col for col in range(1, ws.max_column + 1)}
        rows = [
            row
            for row in range(2, ws.max_row + 1)
            if str(ws.cell(row, headers["链路编号"]).value or "") == "FLOW-032"
        ]
        for row, values in zip(rows, FLOW_032_STEPS):
            for header, value in values.items():
                col = headers.get(header)
                if col:
                    ws.cell(row, col).value = value
        for row in rows[len(FLOW_032_STEPS) :]:
            for col in range(1, ws.max_column + 1):
                ws.cell(row, col).value = None
    wb.save(path)


def main() -> None:
    update_workbook(ROOT / "订单状态流转_链路流程线整理.xlsx")


if __name__ == "__main__":
    main()
