from __future__ import annotations

import json
import re
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
INPUT = ROOT / "runtime" / "testcase_excel_rows.json"
OUTPUT = ROOT / "runtime" / "testcase_excel_sorted_rows.json"


EXACT_ORDER = {
    "A-SAAS-001": 100,
    "A-SAAS-003": 110,
    "A-SAAS-004": 120,
    "A-MEMBER-001": 130,
    "A-AUTH-001": 200,
    "A-PUSH-001": 210,
    "A-PUSH-003": 220,
    "A-ORDER-001": 300,
    "B-SOURCE-DOUYIN-001": 310,
    "B-PRICE-PERCENT-001": 320,
    "B-PRICE-SKU-001": 321,
    "A-TASK-001": 400,
    "B-DISPATCH-ASSIGN-001": 410,
    "B-DISPATCH-AUTO-001": 411,
    "B-WYJ-SELF-FULFILL-001": 420,
    "A-DELIVERY-001": 500,
    "A-DELIVERY-002": 510,
    "A-DELIVERY-003": 520,
    "B-WZG-AFTER-ACCEPT-001": 540,
    "B-WZG-AFTER-ACCEPT-002": 541,
    "B-WZG-AFTER-ACCEPT-003": 542,
    "B-WZG-AFTER-ACCEPT-004": 543,
    "B-MULTI-001": 600,
    "B-MULTI-002": 601,
    "B-MULTI-003": 602,
    "A-ORDER-002": 700,
    "B-CANCEL-002": 710,
    "B-CANCEL-003": 711,
    "B-CANCEL-004": 712,
    "B-CANCEL-005": 713,
    "B-CANCEL-APPROVAL-001": 720,
    "B-CANCEL-APPROVAL-002": 721,
    "B-PENALTY-001": 800,
    "B-PENALTY-002": 801,
    "B-SETTLE-PAYCENTER-001": 900,
    "B-SETTLE-CONFIG-REJECT-001": 902,
    "B-SETTLE-PLAYER-ACCOUNT-001": 910,
    "B-SETTLE-PLAYER-ACCOUNT-REJECT-001": 911,
    "B-SETTLE-L1L2-AUDIT-001": 920,
    "B-SETTLE-PLAYER-APPLY-001": 930,
    "B-SETTLE-WYJ-EMP-BILL-INIT-001": 931,
    "B-SETTLE-WANYOUJI-PAY-001": 940,
    "B-SETTLE-WANYOUJI-REJECT-001": 941,
    "B-SETTLE-EMP-SUMMARY-REJECT-001": 942,
    "B-SETTLE-BATCH-AUDIT-PARTIAL-REJECT-001": 943,
    "B-SETTLE-PAY-FAIL-001": 944,
    "B-SETTLE-WYJ-APPLY-001": 960,
    "B-SETTLE-WYJ-CONFIRM-PAYINFO-001": 961,
    "B-SETTLE-WZG-TO-WYJ-PAY-001": 962,
    "B-SETTLE-WZG-TO-WYJ-REJECT-001": 963,
    "B-SETTLE-WYJ-CONFIRM-MERCHANT-PAY-001": 964,
}


PREFIX_ORDER = [
    (re.compile(r"^S-MAIN-"), 1000),
    (re.compile(r"^S-SUB-"), 1010),
    (re.compile(r"^S-COMBO-"), 1020),
    (re.compile(r"^F-"), 9000),
]


def sort_key(row: dict) -> tuple[int, int, str]:
    case_id = str(row["values"][0] or "")
    original_row = int(row["row"])
    if case_id in EXACT_ORDER:
        return (EXACT_ORDER[case_id], original_row, case_id)
    for pattern, base in PREFIX_ORDER:
        if pattern.match(case_id):
            return (base, original_row, case_id)
    return (8000, original_row, case_id)


def main() -> None:
    payload = json.loads(INPUT.read_text(encoding="utf-8-sig"))
    rows = [
        row
        for row in payload["rows"]
        if not str(row["values"][0] or "").startswith("F-")
    ]
    rows = sorted(rows, key=sort_key)
    OUTPUT.parent.mkdir(parents=True, exist_ok=True)
    OUTPUT.write_text(json.dumps({"rows": rows}, ensure_ascii=False, indent=2), encoding="utf-8")
    for index, row in enumerate(rows, start=2):
        print(f"{index}|{row['values'][0]}|{row['values'][3]}|{row['values'][4]}")


if __name__ == "__main__":
    main()
