//
//  AppRoleCodeTests.swift
//  suixinkanTests
//
//  Created by Codex on 2026/6/29.
//

import XCTest
@testable import suixinkan

final class AppRoleCodeTests: XCTestCase {
    func testFromCodeParsesKnownRoles() {
        XCTAssertEqual(AppRoleCode.fromCode("store_admin"), .storeAdmin)
        XCTAssertEqual(AppRoleCode.fromCode("scenic_admin"), .scenicAdmin)
        XCTAssertEqual(AppRoleCode.fromCode("photographer"), .photographer)
    }

    func testFromLegacyRoleIdMapsKnownIds() {
        XCTAssertEqual(AppRoleCode.fromLegacyRoleId(46), .storeAdmin)
        XCTAssertEqual(AppRoleCode.fromLegacyRoleId(53), .scenicAdmin)
        XCTAssertEqual(AppRoleCode.fromLegacyRoleId(41), .photographer)
    }

    func testFromRoleNameAvoidsAssistantMisclassification() {
        XCTAssertEqual(AppRoleCode.fromRoleName("摄影师助理"), .photographerAssistant)
        XCTAssertEqual(AppRoleCode.fromRoleName("资深摄影师"), .photographer)
    }

    func testHomeMinimalTopRolesContainsManagementRoles() {
        XCTAssertTrue(AppRoleCode.homeMinimalTopRoles.contains(.storeAdmin))
        XCTAssertTrue(AppRoleCode.homeMinimalTopRoles.contains(.scenicAdmin))
        XCTAssertFalse(AppRoleCode.homeMinimalTopRoles.contains(.photographer))
    }
}

final class RolePermissionMatchingTests: XCTestCase {
    func testMatchPrefersRoleCodeOverLegacyId() {
        let permissions = [
            RolePermissionResponse(
                role: RoleInfo(id: 10, name: "摄影师", roleCode: "photographer", notes: nil, permission: []),
                scenic: []
            ),
            RolePermissionResponse(
                role: RoleInfo(id: 53, name: "景区管理员", roleCode: "scenic_admin", notes: nil, permission: []),
                scenic: []
            )
        ]

        let matched = RolePermissionMatching.match(
            in: permissions,
            roleCode: "scenic_admin",
            roleId: 10,
            roleName: "摄影师"
        )

        XCTAssertEqual(matched?.role.id, 53)
    }

    func testResolveRoleCodeMigratesLegacySession() {
        let resolved = RolePermissionMatching.resolveRoleCode(
            roleCode: nil,
            roleId: 46,
            roleName: nil,
            permissions: []
        )

        XCTAssertEqual(resolved, .storeAdmin)
    }
}

final class AccountSnapshotMigrationTests: XCTestCase {
    func testMigrateLegacyRoleIdToRoleCode() {
        var snapshot = AccountSnapshot(currentRoleId: 53)
        snapshot.migrateLegacyRoleIfNeeded()
        XCTAssertEqual(snapshot.currentRoleCode, "scenic_admin")
        XCTAssertNil(snapshot.currentRoleId)
    }

    func testLoadMigratesAndPersistsLegacySnapshot() {
        let defaults = UserDefaults(suiteName: "AccountSnapshotMigrationTests")!
        defaults.removePersistentDomain(forName: "AccountSnapshotMigrationTests")
        let store = AccountSnapshotStore(defaults: defaults, key: "test.snapshot")
        store.save(AccountSnapshot(currentRoleId: 46))

        let loaded = store.load()
        XCTAssertEqual(loaded?.currentRoleCode, "store_admin")
        XCTAssertNil(loaded?.currentRoleId)

        let persisted = store.load()
        XCTAssertEqual(persisted?.currentRoleCode, "store_admin")
        XCTAssertNil(persisted?.currentRoleId)
    }
}
