//
//  PermissionApplyStatusViewModelTests.swift
//  suixinkanTests
//

import XCTest
@testable import suixinkan

@MainActor
/// 权限申请审核状态 ViewModel 测试。
final class PermissionApplyStatusViewModelTests: XCTestCase {

    func testLoadPendingDataMatchesApplyCode() async throws {
        let json = """
        {"code":100000,"msg":"success","data":[{"code":"AP001","role_id":1,"role_name":"摄影师","scenic_list":[{"id":1,"name":"景区A"}],"status":1,"status_label":"审核中","created_at":"2026-01-01","audit_note":"","audited_at":null,"audited_by":null}]}
        """.data(using: .utf8)!
        let api = HomeAPI(client: APIClient(environment: .testing, session: MockURLSession(responses: [json])))
        let viewModel = PermissionApplyStatusViewModel(applyCode: "AP001")

        await viewModel.loadPendingData(api: api)

        XCTAssertEqual(viewModel.pendingData?.code, "AP001")
        XCTAssertEqual(viewModel.pendingData?.status, 1)
        XCTAssertFalse(viewModel.isLoading)
    }

    func testNavigateToEditWhenRejected() async throws {
        let json = """
        {"code":100000,"msg":"success","data":[{"code":"AP002","role_id":2,"role_name":"管理员","scenic_list":[],"status":3,"status_label":"已驳回","created_at":"","audit_note":"资料不全","audited_at":null,"audited_by":null}]}
        """.data(using: .utf8)!
        let api = HomeAPI(client: APIClient(environment: .testing, session: MockURLSession(responses: [json])))
        let viewModel = PermissionApplyStatusViewModel(applyCode: "AP002")
        await viewModel.loadPendingData(api: api)

        var edited: RoleApplyPendingResponse?
        viewModel.onNavigateToEdit = { edited = $0 }
        viewModel.navigateToEditIfRejected()

        XCTAssertEqual(edited?.status, 3)
    }

    func testNavigateToEditIgnoredWhenReviewing() async throws {
        let json = """
        {"code":100000,"msg":"success","data":[{"code":"AP003","role_id":1,"role_name":"摄影师","scenic_list":[],"status":1,"status_label":"审核中","created_at":"","audit_note":"","audited_at":null,"audited_by":null}]}
        """.data(using: .utf8)!
        let api = HomeAPI(client: APIClient(environment: .testing, session: MockURLSession(responses: [json])))
        let viewModel = PermissionApplyStatusViewModel(applyCode: "AP003")
        await viewModel.loadPendingData(api: api)

        var edited: RoleApplyPendingResponse?
        viewModel.onNavigateToEdit = { edited = $0 }
        viewModel.navigateToEditIfRejected()

        XCTAssertNil(edited)
    }

    func testCompletedApplicationIsNotDisplayedAsPending() async throws {
        let json = """
        {"code":100000,"msg":"success","data":[{"code":"AP004","role_id":1,"role_name":"摄影师","scenic_list":[],"status":2,"status_label":"已通过","created_at":"","audit_note":"","audited_at":null,"audited_by":null}]}
        """.data(using: .utf8)!
        let api = HomeAPI(client: APIClient(environment: .testing, session: MockURLSession(responses: [json])))
        let viewModel = PermissionApplyStatusViewModel(applyCode: "AP004")

        await viewModel.loadPendingData(api: api)

        XCTAssertNil(viewModel.pendingData)
        XCTAssertFalse(viewModel.isLoading)
    }

    func testScenicListDialogState() {
        let viewModel = PermissionApplyStatusViewModel(applyCode: "AP005")
        let scenic = ScenicInfo(id: 12, name: "西湖景区", status: 1)
        viewModel.showScenicList(roleName: "摄影师", scenics: [scenic])

        XCTAssertTrue(viewModel.showScenicListDialog)
        XCTAssertEqual(viewModel.dialogRoleName, "摄影师")
        XCTAssertEqual(viewModel.dialogScenicList, [scenic])

        viewModel.hideScenicList()
        XCTAssertFalse(viewModel.showScenicListDialog)
    }
}
