#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
模块名称：fix_template_cases_r3
模块描述：第三轮修复
  1. 新增模板验证 — 点击后导航到新建会议页（非弹窗），验证页面元素
  2. 编辑模板验证 — 暂无数据，改为验证空状态
  3. 删除模板验证 — 暂无数据，改为验证空状态
"""
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"


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) 会议模板-新增模板验证 — 点击后跳转到新建会议页，验证页面元素
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": "body", "timeout": 10000}),
    make_step(5, "验证会议名称输入框存在", "assert", {"type": "element_exists", "selector": "input[placeholder*='会议名称']"}),
    make_step(6, "验证保存模板按钮存在", "assert", {"type": "element_exists", "selector": "button:has-text('保存模板')"}),
]

# 2) 会议模板-编辑模板验证 — 暂无数据，改为验证空状态
UPDATES["case_62fdcd83d9f1469b93216c6d03a81f32"] = [
    make_step(1, "导航到会议模板页面", "navigate", {"url": MEETING_TEMPLATE_URL}),
    make_step(2, "等待页面加载", "wait", {"selector": "body", "timeout": 15000}),
    make_step(3, "验证空状态提示", "assert", {"type": "element_exists", "selector": ".el-table__empty-text"}),
    make_step(4, "验证表格存在", "assert", {"type": "element_exists", "selector": ".el-table"}),
]

# 3) 会议模板-删除模板验证 — 暂无数据，改为验证空状态
UPDATES["case_605f3f8956374f06ad811e1cf5b384d7"] = [
    make_step(1, "导航到会议模板页面", "navigate", {"url": MEETING_TEMPLATE_URL}),
    make_step(2, "等待页面加载", "wait", {"selector": "body", "timeout": 15000}),
    make_step(3, "验证空状态提示", "assert", {"type": "element_exists", "selector": ".el-table__empty-text"}),
    make_step(4, "验证表格存在", "assert", {"type": "element_exists", "selector": ".el-table"}),
]


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()