#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
模块名称：create_remaining_module_cases.py
模块描述：补充剩余空壳模块的UI自动化用例（信息发布、信息管理、其他分类）

作者：czj
创建日期：2026-07-22
最后修改日期：2026-07-22
"""
import asyncio
import httpx
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

BASE_URL = "http://localhost:8001"
TARGET_URL = "https://192.168.5.44"

# 模块ID
XINXIFABU_MODULE_ID = "module_parent_xinxifabu"       # 信息发布
XINXIGUANLI_MODULE_ID = "module_parent_xinxiguanli"   # 信息管理
QITA_MODULE_ID = "module_parent_qita"                  # 其他分类

DEFAULT_CONFIG = {
    "timeout": 120000,
    "screenshot": True,
    "auto_login": True,
    "step_retry_count": 2,
}


def build_all_cases():
    """构建剩余空壳模块用例"""
    cases = []

    # ==================== 信息发布用例 ====================

    # 用例1: 消息通知-页面访问验证
    cases.append({
        "name": "消息通知-页面访问验证",
        "description": "验证通过功能中心访问消息通知页面，确认页面正常加载",
        "module_id": XINXIFABU_MODULE_ID,
        "priority": "high",
        "order": 100,
        "config": DEFAULT_CONFIG,
        "steps": [
            {"order": 1, "name": "等待首页加载", "action": "wait", "params": {"selector": ".block", "timeout": 15000}},
            {"order": 2, "name": "点击功能中心图标", "action": "click", "params": {"selector": ".home_nav_left"}},
            {"order": 3, "name": "等待抽屉打开", "action": "wait", "params": {"selector": ".el-drawer", "timeout": 10000}},
            {"order": 4, "name": "点击消息通知", "action": "click", "params": {"selector": ".el-drawer >> text='消息通知'"}},
            {"order": 5, "name": "等待页面加载", "action": "wait", "params": {"selector": "body", "timeout": 15000}},
            {"order": 6, "name": "等待页面渲染", "action": "wait", "params": {"selector": "body", "timeout": 3000}},
            {"order": 7, "name": "验证页面内容存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-table, .el-card, .content, .app-container"}},
        ],
    })

    # ==================== 信息管理用例 ====================

    # 用例2: 文件列表-页面访问验证
    cases.append({
        "name": "文件列表-页面访问验证",
        "description": "验证通过功能中心访问文件列表页面，确认页面正常加载",
        "module_id": XINXIGUANLI_MODULE_ID,
        "priority": "high",
        "order": 100,
        "config": DEFAULT_CONFIG,
        "steps": [
            {"order": 1, "name": "等待首页加载", "action": "wait", "params": {"selector": ".block", "timeout": 15000}},
            {"order": 2, "name": "点击功能中心图标", "action": "click", "params": {"selector": ".home_nav_left"}},
            {"order": 3, "name": "等待抽屉打开", "action": "wait", "params": {"selector": ".el-drawer", "timeout": 10000}},
            {"order": 4, "name": "点击文件列表", "action": "click", "params": {"selector": ".el-drawer >> text='文件列表'"}},
            {"order": 5, "name": "等待页面加载", "action": "wait", "params": {"selector": "body", "timeout": 15000}},
            {"order": 6, "name": "等待页面渲染", "action": "wait", "params": {"selector": "body", "timeout": 3000}},
            {"order": 7, "name": "验证页面内容存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-table, .el-card, .content, .app-container"}},
        ],
    })

    # 用例3: 下载列表-页面访问验证
    cases.append({
        "name": "下载列表-页面访问验证",
        "description": "验证通过功能中心访问下载列表页面，确认页面正常加载",
        "module_id": XINXIGUANLI_MODULE_ID,
        "priority": "medium",
        "order": 101,
        "config": DEFAULT_CONFIG,
        "steps": [
            {"order": 1, "name": "等待首页加载", "action": "wait", "params": {"selector": ".block", "timeout": 15000}},
            {"order": 2, "name": "点击功能中心图标", "action": "click", "params": {"selector": ".home_nav_left"}},
            {"order": 3, "name": "等待抽屉打开", "action": "wait", "params": {"selector": ".el-drawer", "timeout": 10000}},
            {"order": 4, "name": "点击下载列表", "action": "click", "params": {"selector": ".el-drawer >> text='下载列表'"}},
            {"order": 5, "name": "等待页面加载", "action": "wait", "params": {"selector": "body", "timeout": 15000}},
            {"order": 6, "name": "等待页面渲染", "action": "wait", "params": {"selector": "body", "timeout": 3000}},
            {"order": 7, "name": "验证页面内容存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-table, .el-card, .content, .app-container"}},
        ],
    })

    # ==================== 其他分类用例 ====================

    # 用例4: 历史记录-页面访问验证
    cases.append({
        "name": "历史记录-页面访问验证",
        "description": "验证通过功能中心访问历史记录页面，确认页面正常加载",
        "module_id": QITA_MODULE_ID,
        "priority": "high",
        "order": 100,
        "config": DEFAULT_CONFIG,
        "steps": [
            {"order": 1, "name": "等待首页加载", "action": "wait", "params": {"selector": ".block", "timeout": 15000}},
            {"order": 2, "name": "点击功能中心图标", "action": "click", "params": {"selector": ".home_nav_left"}},
            {"order": 3, "name": "等待抽屉打开", "action": "wait", "params": {"selector": ".el-drawer", "timeout": 10000}},
            {"order": 4, "name": "点击历史记录", "action": "click", "params": {"selector": ".el-drawer >> text='历史记录'"}},
            {"order": 5, "name": "等待页面加载", "action": "wait", "params": {"selector": "body", "timeout": 15000}},
            {"order": 6, "name": "等待页面渲染", "action": "wait", "params": {"selector": "body", "timeout": 3000}},
            {"order": 7, "name": "验证页面内容存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-table, .el-tabs, .el-card, .content, .app-container"}},
        ],
    })

    # 用例5: 会议审批-页面访问验证
    cases.append({
        "name": "会议审批-页面访问验证",
        "description": "验证通过功能中心访问会议审批页面，确认页面正常加载",
        "module_id": QITA_MODULE_ID,
        "priority": "medium",
        "order": 101,
        "config": DEFAULT_CONFIG,
        "steps": [
            {"order": 1, "name": "等待首页加载", "action": "wait", "params": {"selector": ".block", "timeout": 15000}},
            {"order": 2, "name": "点击功能中心图标", "action": "click", "params": {"selector": ".home_nav_left"}},
            {"order": 3, "name": "等待抽屉打开", "action": "wait", "params": {"selector": ".el-drawer", "timeout": 10000}},
            {"order": 4, "name": "点击会议审批", "action": "click", "params": {"selector": ".el-drawer >> text='会议审批'"}},
            {"order": 5, "name": "等待页面加载", "action": "wait", "params": {"selector": "body", "timeout": 15000}},
            {"order": 6, "name": "等待页面渲染", "action": "wait", "params": {"selector": "body", "timeout": 3000}},
            {"order": 7, "name": "验证页面内容存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-table, .el-card, .content, .app-container"}},
        ],
    })

    return cases


async def create_case(client: httpx.AsyncClient, case_data: dict):
    """创建单个测试用例"""
    try:
        response = await client.post(f"{BASE_URL}/api/cases", json=case_data, timeout=30.0)
        if response.status_code in (200, 201):
            result = response.json()
            print(f"  ✅ {case_data['name']} (ID: {result['id']})")
            return result
        else:
            print(f"  ❌ {case_data['name']} - {response.status_code}")
            return None
    except Exception as e:
        print(f"  ❌ {case_data['name']} - {e}")
        return None


async def main():
    print("=" * 60)
    print("补充剩余空壳模块用例（信息发布、信息管理、其他分类）")
    print("=" * 60)

    cases = build_all_cases()
    print(f"\n计划创建: {len(cases)} 个用例")

    async with httpx.AsyncClient() as client:
        for i, case in enumerate(cases, 1):
            print(f"\n[{i}/{len(cases)}] ", end="")
            await create_case(client, case)

    print("\n" + "=" * 60)
    print("✅ 用例创建完成")
    print("=" * 60)


if __name__ == "__main__":
    asyncio.run(main())
