//
//  RealNameAuthAPITests.swift
//  suixinkanTests
//

import XCTest
@testable import suixinkan

/// 实名认证 API 契约测试。
@MainActor
final class RealNameAuthAPITests: XCTestCase {
    func testInfoUsesAndroidGETPath() async throws {
        let response = Data(#"{"code":100000,"msg":"success","data":{"real_name_info":null}}"#.utf8)
        let session = MockURLSession(responses: [response])
        let api = ProfileAPI(client: APIClient(environment: .testing, session: session))

        _ = try await api.realNameInfo()

        let request = try XCTUnwrap(session.requests.first)
        XCTAssertEqual(request.httpMethod, "GET")
        XCTAssertEqual(request.url?.path, "/api/yf-handset-app/photog/real-name/info")
    }

    func testSMSCodeUsesPOSTWithoutBody() async throws {
        let response = Data(#"{"code":100000,"msg":"success","data":{}}"#.utf8)
        let session = MockURLSession(responses: [response])
        let api = ProfileAPI(client: APIClient(environment: .testing, session: session))

        try await api.realNameSmsVerifyCode()

        let request = try XCTUnwrap(session.requests.first)
        XCTAssertEqual(request.httpMethod, "POST")
        XCTAssertEqual(request.url?.path, "/api/yf-handset-app/photog/real-name/sms-verify-code")
        XCTAssertNil(request.httpBody)
    }

    func testSubmitUsesAndroidJSONFields() async throws {
        let response = Data(#"{"code":100000,"msg":"success","data":{}}"#.utf8)
        let session = MockURLSession(responses: [response])
        let api = ProfileAPI(client: APIClient(environment: .testing, session: session))
        let payload = RealNameAuthRequest(
            realName: "张三",
            idCardNo: "11010519491231002X",
            smsVerifyCode: "123456",
            startDate: "2026-07-13",
            endDate: "",
            isLongValid: 1,
            frontUrl: "front",
            backUrl: "back"
        )

        try await api.realNameSubmit(payload)

        let request = try XCTUnwrap(session.requests.first)
        let body = try XCTUnwrap(request.httpBody)
        let object = try XCTUnwrap(JSONSerialization.jsonObject(with: body) as? [String: Any])
        XCTAssertEqual(request.httpMethod, "POST")
        XCTAssertEqual(request.url?.path, "/api/yf-handset-app/photog/real-name/submit")
        XCTAssertEqual(object["real_name"] as? String, "张三")
        XCTAssertEqual(object["id_card_no"] as? String, "11010519491231002X")
        XCTAssertEqual(object["sms_verify_code"] as? String, "123456")
        XCTAssertEqual(object["is_long_valid"] as? Int, 1)
        XCTAssertEqual(object["front_url"] as? String, "front")
        XCTAssertEqual(object["back_url"] as? String, "back")
    }
}
