//
//  HomeMenuCatalog.swift
//  suixinkan
//

import Foundation

/// 首页菜单静态 catalog，与服务端 permission URI 取交集后展示。
enum HomeMenuCatalog {

    private static let fixedHomeEntryURIs: Set<String> = ["message_center"]

    /// 全部已知菜单项，对齐 Android `Constants.menuList`。
    static let allItems: [HomeMenuItem] = [
        HomeMenuItem(uri: "space_settings", title: "空间设置", iconName: "home_menu_space"),
        HomeMenuItem(uri: "wallet", title: "我的钱包", iconName: "home_menu_wallet"),
        HomeMenuItem(uri: "cloud_management", title: "相册云盘", iconName: "icloud.fill"),
        HomeMenuItem(uri: "asset_management", title: "素材管理", iconName: "home_menu_material"),
        HomeMenuItem(uri: "task_management", title: "任务管理", iconName: "checklist"),
        HomeMenuItem(uri: "task_management_editor", title: "任务管理", iconName: "checklist"),
        HomeMenuItem(uri: "schedule_management", title: "日程管理", iconName: "calendar"),
        HomeMenuItem(uri: "system_settings", title: "设置中心", iconName: "gearshape.fill"),
        HomeMenuItem(uri: "message_center", title: "消息中心", iconName: "bell.fill"),
        HomeMenuItem(uri: "checkin_points", title: "打卡点管理", iconName: "home_menu_punch_point"),
        HomeMenuItem(uri: "sample_management", title: "样片管理", iconName: "sample_ic_home_menu_route"),
        HomeMenuItem(uri: "live_stream_management", title: "直播管理", iconName: "dot.radiowaves.left.and.right"),
        HomeMenuItem(uri: "verification_order", title: "核销订单", iconName: "checkmark.seal.fill"),
        HomeMenuItem(uri: "travel_album", title: "新增相册", iconName: "rectangle.stack.badge.plus"),
        HomeMenuItem(uri: "live_album", title: "直播相册", iconName: "play.rectangle.on.rectangle.fill"),
        HomeMenuItem(uri: "pm", title: "项目管理", iconName: "circle.grid.2x2.fill"),
        HomeMenuItem(uri: "pm_manager", title: "项目管理", iconName: "circle.grid.2x2.fill"),
        HomeMenuItem(uri: "location_report", title: "位置上报", iconName: "mappin.circle.fill"),
        HomeMenuItem(uri: "report_photographer", title: "举报摄影师", iconName: "shield.lefthalf.filled"),
        HomeMenuItem(uri: "registration_invitation", title: "注册邀请", iconName: "envelope.open.fill"),
        HomeMenuItem(uri: "photographer_invite", title: "邀请摄影师", iconName: "envelope.open.fill"),
        HomeMenuItem(uri: "invite_record", title: "邀请记录", iconName: "list.bullet.rectangle.fill"),
        HomeMenuItem(uri: "store", title: "店铺管理", iconName: "building.2.fill"),
        HomeMenuItem(uri: "fly", title: "飞行管理", iconName: "paperplane.fill"),
        HomeMenuItem(uri: "pilot_cert", title: "飞手认证", iconName: "paperplane.fill"),
        HomeMenuItem(uri: "pilot_controller", title: "飞控", iconName: "paperplane.fill"),
        HomeMenuItem(uri: "/scenic-queue", title: "排队管理", iconName: "person.3.fill"),
        HomeMenuItem(uri: "operating-area", title: "运营区域", iconName: "map.fill"),
        HomeMenuItem(uri: "/scenic-order-manage", title: "景区订单", iconName: "photo.on.rectangle.angled"),
        HomeMenuItem(uri: "cooperation_order", title: "合作订单", iconName: "person.2"),
    ]

    /// 将服务端权限映射为可展示菜单，保持服务端顺序。
    static func visibleMenus(from permissions: [HomePermissionItem]) -> [HomeMenuItem] {
        permissions.compactMap { permission in
            guard isCustomizable(uri: permission.uri) else { return nil }
            guard let catalog = allItems.first(where: { $0.uri == permission.uri }) else { return nil }
            return HomeMenuItem(
                uri: catalog.uri,
                title: catalog.title,
                iconName: catalog.iconName,
                serverName: permission.name
            )
        }
    }

    /// 查找 catalog 项。
    static func item(for uri: String) -> HomeMenuItem? {
        allItems.first { $0.uri == uri }
    }

    /// 判断入口是否允许出现在首页常用应用及其自定义页面中。
    static func isCustomizable(uri: String) -> Bool {
        !fixedHomeEntryURIs.contains(uri)
    }
}
