# -*- coding: utf-8 -*-
import json
import pytest
import os
import allure
from pathlib import Path
from api_auto_framework.core.request_wrapper import RequestWrapper
from api_auto_framework.utils.data_loader import load_test_data
from api_auto_framework.utils.assertion_tools import cookie
from api_auto_framework.utils.handle_path import testData_path


TEST_DATA = Path(testData_path) / "test_Baidu_Campaign_getList.yaml"


@allure.feature("百度")
@allure.story("媒体账户")
@allure.title("主面板查询计划列表")
@pytest.mark.generated
def test_Baidu_Campaign_getList():
    """Automatically generated test case: 主面板查询计划列表"""
    # 加载测试数据
    test_data = load_test_data(TEST_DATA)[0]
    # 准备请求参数
    request_kwargs = {
        "method": test_data['method'],
        "url": test_data['url'],
        "headers": test_data.get('headers'),
        "cookies": cookie()  # 新增cookies参数
    }
    # 智能选择请求体处理方式
    body = test_data.get('body')
    if body is not None:
        if isinstance(body, (dict, list)):
            request_kwargs["json"] = body
        else:
            request_kwargs["data"] = body
    with allure.step("准备请求数据"):
        allure.attach(f"URL: https://cli1.mobgi.com/Baidu/Campaign/getList", "请求URL")
        allure.attach(json.dumps(body), "请求体", allure.attachment_type.JSON)
    # 发送请求并获取响应
    with allure.step(f"发送请求:test_Baidu_Campaign_getList"):
        response = RequestWrapper.send_request(**request_kwargs)
    with allure.step("处理请求"):
        result = RequestWrapper.formatted_response(response)
    with allure.step("响应结果"):
        allure.attach(str(result["status_code"]), "响应状态", allure.attachment_type.TEXT)
        allure.attach(json.dumps(result["headers"]), "响应头", allure.attachment_type.JSON)
        allure.attach(json.dumps(result["body"], indent=2, ensure_ascii=False), "响应内容", allure.attachment_type.JSON)

    # 基础状态码断言
    try:
        assert result["status_code"] == test_data["expected"][
            "status_code"], f"状态码不匹配: {result['status_code']} ≠ {test_data['expected']['status_code']}"
        assert result["body"]["code"] == 0, f"业务码非零: {result['body']['code']}"
        assert result["body"]["message"] == "成功", f"消息不符: {result['body']['message']}"
    except AssertionError as e:
        allure.attach(str(e), "断言失败详情", allure.attachment_type.TEXT)
        raise
    # assert result["status_code"] == test_data["expected"]["status_code"], \
    #     f"期望状态码 {test_data['expected']['status_code']}，实际得到 {result['status_code']}"
    # assert result["body"]["code"] == 0
    # assert result["body"]["message"] == "成功"
