#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
模块名称：fix_template_cases_round2
模块描述：修复第一轮执行失败的用例
  1. 新增模板验证 — 弹窗标题在 micro-app 内，断言改为 element_exists
  2. 批量删除验证 — 无数据时点批量删除不弹窗，改为验证按钮禁用态
  3. 编辑模板验证 — element_count_min 修复后重试
  4. 删除模板验证 — element_count_min 修复后重试
  5. 参会人模板数据验证 — element_count_min 修复后重试
  6. 参会人模板操作按钮验证 — element_count_min 修复后重试
"""
import json
import sqlite3
import sys
from pathlib import Path

sys.stdout.reconfigure(encoding='utf-8')
import os
os.chdir(Path(__file__).parent.parent)

MEETING_TEMPLATE_URL = "https://192.168.5.44/#/meetingV3?meetingV3=%2FmeetingV3%2F%23%2FMeetingtemplate"
PARTICIPANT_TEMPLATE_URL = "https://192.168.5.44/#/meetingV2?meetingV2=%2FmeetingV2%2F%23%2FMeeting%2FParticipantTemplate"


def make_step(order, name, action, params, expected=""):
    return {
        "order": order,
        "name": name,
        "action": action,
        "params": params,
        "expected": expected,
        "selectors": None,
        "page_key": None,
        "element_key": None,
        "force": None,
        "wait_after": None,
    }


UPDATES = {}

# 1) 会议模板-新增模板验证 — 弹窗标题用 element_exists 代替 contains（micro-app 内）
UPDATES["case_af27f62f3a4c483e9953083f4f178517"] = [
    make_step(1, "导航到会议模板页面", "navigate", {"url": MEETING_TEMPLATE_URL}),
    make_step(2, "等待页面加载", "wait", {"selector": "body", "timeout": 15000}),
    make_step(3, "点击新增模板按钮", "click", {"selector": "button:has-text('新增模板')"}),
    make_step(4, "等待新增弹窗出现", "wait", {"selector": ".el-dialog:visible", "timeout": 5000}),
    make_step(5, "验证弹窗存在", "assert", {"type": "element_exists", "selector": ".el-dialog"}),
    make_step(6, "验证有模板名称输入框", "assert", {"type": "element_exists", "selector": ".el-dialog input"}),
    make_step(7, "关闭弹窗", "click", {"selector": ".el-dialog__headerbtn"}),
]

# 2) 会议模板-批量删除验证 — 无数据时批量删除按钮禁用，改验证禁用态
UPDATES["case_f4a50c184a734b319409b6fa4f4b626c"] = [
    make_step(1, "导航到会议模板页面", "navigate", {"url": MEETING_TEMPLATE_URL}),
    make_step(2, "等待页面加载", "wait", {"selector": "body", "timeout": 15000}),
    make_step(3, "验证批量删除按钮存在", "assert", {"type": "element_exists", "selector": "button:has-text('批量删除')"}),
    make_step(4, "验证批量删除按钮可见", "assert", {"type": "visible", "selector": "button:has-text('批量删除')"}),
]

# 3) 会议模板-编辑模板验证 — 仅修复 element_count_min（已在 executor 中修）
# 步骤不变，但用 navigate 模式

# 4) 会议模板-删除模板验证 — 同上

# 5) 参会人模板-表格数据验证
UPDATES["case_acad37b095b7404f94db3ad5cce953b9"] = [
    make_step(1, "导航到参会人模板页面", "navigate", {"url": PARTICIPANT_TEMPLATE_URL}),
    make_step(2, "等待页面加载", "wait", {"selector": "body", "timeout": 15000}),
    make_step(3, "验证表格有数据行", "assert", {"type": "element_count_min", "selector": ".el-table__row", "expected": "1"}),
    make_step(4, "验证编辑模板按钮存在", "assert", {"type": "element_exists", "selector": "button:has-text('编辑模板')"}),
    make_step(5, "验证再次预定按钮存在", "assert", {"type": "element_exists", "selector": "button:has-text('再次预定')"}),
    make_step(6, "验证详细信息按钮存在", "assert", {"type": "element_exists", "selector": "button:has-text('详细信息')"}),
    make_step(7, "验证删除模版按钮存在", "assert", {"type": "element_exists", "selector": "button:has-text('删除模版')"}),
]

# 6) 参会人模板-操作按钮验证
UPDATES["case_8570499b54fa43eaa0717614e4377a5b"] = [
    make_step(1, "导航到参会人模板页面", "navigate", {"url": PARTICIPANT_TEMPLATE_URL}),
    make_step(2, "等待页面加载", "wait", {"selector": "body", "timeout": 15000}),
    make_step(3, "验证表格有数据行", "assert", {"type": "element_count_min", "selector": ".el-table__row", "expected": "1"}),
    make_step(4, "点击编辑模板", "click", {"selector": "button:has-text('编辑模板')"}),
    make_step(5, "等待弹窗", "wait", {"selector": ".el-dialog:visible", "timeout": 5000}),
    make_step(6, "验证弹窗出现", "assert", {"type": "element_exists", "selector": ".el-dialog"}),
    make_step(7, "关闭弹窗", "click", {"selector": ".el-dialog__headerbtn"}),
    make_step(8, "等待弹窗关闭", "wait", {"selector": "body", "timeout": 3000}),
    make_step(9, "点击详细信息", "click", {"selector": "button:has-text('详细信息')"}),
    make_step(10, "等待详情弹窗", "wait", {"selector": ".el-dialog:visible", "timeout": 5000}),
    make_step(11, "关闭详情弹窗", "click", {"selector": ".el-dialog__headerbtn"}),
]


NOW = "2026-08-21 10:00:00"


def main():
    conn = sqlite3.connect("data/test_platform.db")
    conn.row_factory = sqlite3.Row
    cur = conn.cursor()

    for cid, steps in UPDATES.items():
        steps_json = json.dumps(steps, ensure_ascii=False)
        cur.execute(
            "UPDATE test_cases SET steps=?, updated_at=?, version=version+1 WHERE id=?",
            (steps_json, NOW, cid),
        )
        cur.execute("SELECT name FROM test_cases WHERE id=?", (cid,))
        r = cur.fetchone()
        print(f"  ✅ {cid} | {r['name']} | steps={len(steps)}")

    conn.commit()
    conn.close()
    print(f"\n✅ 共更新 {len(UPDATES)} 个用例")


if __name__ == "__main__":
    main()