//
//  AuthModelsTests.swift
//  suixinkanTests
//

import XCTest
@testable import suixinkan

/// Auth 模块模型测试，覆盖 v9 登录响应解码和账号转换规则。
final class AuthModelsTests: XCTestCase {
    func testV9AuthResponseDecodesMultiAccountJSON() throws {
        let json = """
        {
          "code": 100000,
          "msg": "success",
          "data": {
            "token": "person-temp-token",
            "scenic_users": [
              {
                "account_type": "scenic_user",
                "id": 101,
                "user_id": 101,
                "scenic_user_id": 101,
                "ss_user_id": 101,
                "username": "scenic_admin",
                "user_name": "scenic_operator",
                "real_name": "张三",
                "nickname": "张三",
                "phone": "13800138000",
                "scenic_id": 10,
                "scenic_name": "示例景区",
                "role_name": "摄影师",
                "is_current": false
              }
            ],
            "store_users": [
              {
                "account_type": "store_user",
                "id": 201,
                "user_id": 201,
                "store_user_id": 201,
                "username": "store_admin",
                "user_name": "store_admin",
                "real_name": "李四",
                "phone": "13900139000",
                "avatar": "",
                "scenic_id": 10,
                "scenic_name": "示例景区",
                "store_id": 20,
                "store_name": "示例门店",
                "role_name": "店长",
                "is_current": false
              }
            ]
          }
        }
        """.data(using: .utf8)!

        let envelope = try JSONDecoder().decode(APIEnvelope<V9AuthResponse>.self, from: json)
        let response = try XCTUnwrap(envelope.data)

        XCTAssertEqual(response.token, "person-temp-token")
        XCTAssertEqual(response.accounts.map(\.businessUserId), [101, 201])
        XCTAssertEqual(response.accounts.map(\.subtitle), ["示例景区 · 摄影师", "示例景区 · 店长"])
    }

    func testV9AuthResponseDecodesMissingAccountListsAsEmpty() throws {
        let json = """
        {
          "code": 100000,
          "msg": "success",
          "data": {
            "token": "only-token"
          }
        }
        """.data(using: .utf8)!

        let envelope = try JSONDecoder().decode(APIEnvelope<V9AuthResponse>.self, from: json)
        let response = try XCTUnwrap(envelope.data)

        XCTAssertEqual(response.token, "only-token")
        XCTAssertTrue(response.scenicUsers.isEmpty)
        XCTAssertTrue(response.storeUsers.isEmpty)
    }

    func testV9ScenicUserBusinessUserIdPrefersSsUserId() throws {
        let json = """
        {
          "account_type": "scenic_user",
          "id": "1",
          "user_id": 2,
          "scenic_user_id": 3,
          "ss_user_id": 101,
          "username": "scenic_admin",
          "user_name": "scenic_operator",
          "real_name": "张三",
          "nickname": "张三",
          "phone": "13800138000",
          "scenic_id": 10,
          "scenic_name": "示例景区",
          "role_name": "摄影师",
          "is_current": false
        }
        """.data(using: .utf8)!
        let user = try JSONDecoder().decode(V9ScenicUser.self, from: json)

        XCTAssertEqual(user.businessUserId, 101)
        XCTAssertEqual(user.toAccountSwitchAccount().subtitle, "示例景区 · 摄影师")
        XCTAssertEqual(user.toAccountSwitchAccount().realName, "张三")
        XCTAssertEqual(user.toAccountSwitchAccount().toSetUserRequest(), SetUserRequest(ssUserId: 101))
    }

    func testV9StoreUserBusinessUserIdPrefersStoreUserId() throws {
        let json = """
        {
          "account_type": "store_user",
          "id": 1,
          "user_id": 2,
          "store_user_id": 201,
          "username": "store_admin",
          "user_name": "store_admin",
          "real_name": "张三",
          "phone": "13800138000",
          "avatar": "",
          "scenic_id": 10,
          "scenic_name": "示例景区",
          "store_id": 20,
          "store_name": "示例门店",
          "role_name": "店长",
          "is_current": false
        }
        """.data(using: .utf8)!
        let user = try JSONDecoder().decode(V9StoreUser.self, from: json)

        XCTAssertEqual(user.businessUserId, 201)
        XCTAssertEqual(user.toAccountSwitchAccount().subtitle, "示例景区 · 店长")
        XCTAssertEqual(user.toAccountSwitchAccount().realName, "张三")
        XCTAssertEqual(user.toAccountSwitchAccount().toSetUserRequest(), SetUserRequest(storeUserId: 201))
    }
}
