//
//  AppRoleCode.swift
//  suixinkan
//

import Foundation

/// 业务角色编码，与 Android `AppRoleCode` 对齐。
enum AppRoleCode: String, CaseIterable, Equatable {
    case driver = "driver"
    case floristBuilder = "florist_builder"
    case clerk = "clerk"
    case liveStream = "live_stream"
    case frontDesk = "front_desk"
    case makeupArtist = "makeup_artist"
    case photographerAssistant = "photographer_assistant"
    case assistantManager = "assistant_manager"
    case scenicOperation = "scenic_operation"
    case scenicAdmin = "scenic_admin"
    case scenicCS = "scenic_cs"
    case editor = "editor"
    case storeAdmin = "store_admin"
    case photographer = "photographer"

    var displayName: String {
        switch self {
        case .driver: "司机"
        case .floristBuilder: "搭建花艺师"
        case .clerk: "店员"
        case .liveStream: "直播"
        case .frontDesk: "前台"
        case .makeupArtist: "化妆师"
        case .photographerAssistant: "摄影师助理"
        case .assistantManager: "副店长"
        case .scenicOperation: "景区运营"
        case .scenicAdmin: "景区管理员"
        case .scenicCS: "飞手"
        case .editor: "剪辑师"
        case .storeAdmin: "店铺管理员"
        case .photographer: "摄影师"
        }
    }

    var isPhotographer: Bool {
        self == .photographer
    }

    /// 首页隐藏顶部在线/位置/快捷操作的角色。
    static let homeMinimalTopRoles: Set<AppRoleCode> = [
        .storeAdmin,
        .editor,
        .scenicCS,
        .scenicAdmin,
        .scenicOperation,
    ]

    /// 当前角色是否使用首页精简顶部布局。
    var usesHomeMinimalTopLayout: Bool {
        AppRoleCode.homeMinimalTopRoles.contains(self)
    }

    /// 从后端 role_code 解析角色。
    static func fromCode(_ code: String?) -> AppRoleCode? {
        let normalized = code?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
        guard !normalized.isEmpty else { return nil }
        return AppRoleCode(rawValue: normalized)
    }

    /// 从旧版 role_id 推断角色。
    static func fromLegacyRoleId(_ roleId: Int) -> AppRoleCode? {
        switch roleId {
        case 41: .photographer
        case 46: .storeAdmin
        case 47: .editor
        case 52: .scenicCS
        case 53: .scenicAdmin
        case 54: .scenicOperation
        default: nil
        }
    }

    /// 从 role_name 文本推断角色，避免「摄影师助理」误判为摄影师。
    static func fromRoleName(_ roleName: String?) -> AppRoleCode? {
        let name = roleName?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
        guard !name.isEmpty else { return nil }
        if name.contains("摄影师助理") { return .photographerAssistant }
        if name.contains("副店长") { return .assistantManager }
        if name.contains("搭建花艺") || name.contains("花艺师") { return .floristBuilder }
        if name.contains("店铺管理") || name.contains("店长") { return .storeAdmin }
        if name.contains("景区管理") { return .scenicAdmin }
        if name.contains("景区运营") { return .scenicOperation }
        if name.contains("景区客服") || name.contains("飞手") { return .scenicCS }
        if name.contains("剪辑") { return .editor }
        if name.contains("化妆师") { return .makeupArtist }
        if name.contains("摄影师") { return .photographer }
        if name.contains("司机") { return .driver }
        if name.contains("店员") { return .clerk }
        if name.contains("直播") { return .liveStream }
        if name.contains("前台") { return .frontDesk }
        return AppRoleCode.allCases.first { name.contains($0.displayName) }
    }
}
