//
//  ProfileModelsTests.swift
//  suixinkanTests
//

import XCTest
@testable import suixinkan

/// Profile 模型解码测试。
final class ProfileModelsTests: XCTestCase {
    func testUserInfoResponseDecodesSnakeCaseFields() throws {
        let json = """
        {
          "avatar": "https://cdn.example.com/a.jpg",
          "real_name": "张三",
          "phone": "13800138000",
          "nickname": "小张",
          "role_name": "摄影师",
          "status": 1,
          "status_name": "正常"
        }
        """.data(using: .utf8)!

        let info = try JSONDecoder().decode(UserInfoResponse.self, from: json)

        XCTAssertEqual(info.realName, "张三")
        XCTAssertEqual(info.nickname, "小张")
        XCTAssertEqual(info.roleName, "摄影师")
        XCTAssertEqual(info.statusName, "正常")
    }

    func testBankCardInfoDecodesAuditStatusAsString() throws {
        let json = """
        {
          "real_name": "张三",
          "bank_name": "中国银行",
          "branch_name": "南京支行",
          "card_number": "6222021234567890",
          "audit_status": "2",
          "audit_status_label": "审核通过",
          "reject_reason": ""
        }
        """.data(using: .utf8)!

        let info = try JSONDecoder().decode(BankCardInfo.self, from: json)

        XCTAssertEqual(info.auditStatus, 2)
        XCTAssertEqual(info.bankName, "中国银行")
    }

    func testRealNameInfoVerifiedWhenAuditStatusIsTwo() throws {
        let json = """
        {
          "real_name": "张三",
          "id_card_no": "320102199001011234",
          "audit_status": 2,
          "is_long_valid": 1
        }
        """.data(using: .utf8)!

        let info = try JSONDecoder().decode(RealNameInfo.self, from: json)

        XCTAssertTrue(info.verified)
    }

    func testRealNameInfoDecodesCompleteAuditDetails() throws {
        let json = """
        {
          "real_name": "张三",
          "id_card_no": "11010519491231002X",
          "audit_status": "3",
          "audit_status_text": "审核驳回",
          "reject_reason": "国徽面照片模糊",
          "start_date": "2020-01-01",
          "end_date": "2030-01-01",
          "is_long_valid": "0",
          "front_url": "https://cdn.example.com/front.jpg",
          "back_url": "https://cdn.example.com/back.jpg",
          "auditor_id": "8",
          "auditor": { "id": "8", "name": "审核员" },
          "audit_at": "2026-07-13 12:00:00"
        }
        """.data(using: .utf8)!

        let info = try JSONDecoder().decode(RealNameInfo.self, from: json)

        XCTAssertEqual(info.auditStatus, 3)
        XCTAssertEqual(info.rejectReason, "国徽面照片模糊")
        XCTAssertEqual(info.frontUrl, "https://cdn.example.com/front.jpg")
        XCTAssertEqual(info.backUrl, "https://cdn.example.com/back.jpg")
        XCTAssertEqual(info.auditorId, 8)
        XCTAssertEqual(info.auditor?.name, "审核员")
        XCTAssertEqual(info.auditAt, "2026-07-13 12:00:00")
    }

    func testRealNameInfoRemainsCompatibleWhenOptionalAuditFieldsAreMissing() throws {
        let json = """
        {
          "real_name": "李四",
          "id_card_no": "11010519491231002X",
          "audit_status": 1,
          "is_long_valid": 1
        }
        """.data(using: .utf8)!

        let info = try JSONDecoder().decode(RealNameInfo.self, from: json)

        XCTAssertEqual(info.auditStatus, 1)
        XCTAssertTrue(info.isLongValid)
        XCTAssertNil(info.rejectReason)
        XCTAssertNil(info.frontUrl)
        XCTAssertNil(info.auditor)
        XCTAssertNil(info.auditAt)
    }
}
