//
//  RolePermissionModels.swift
//  suixinkan
//

import Foundation

/// 角色权限接口根节点，对齐 Android `RolePermissionResponse`。
struct RolePermissionResponse: Codable, Equatable, Sendable {
    let role: RoleInfo
    let scenic: [ScenicInfo]
    let store: [RolePermissionStoreInfo]

    enum CodingKeys: String, CodingKey {
        case role
        case scenic
        case store
    }

    init(
        role: RoleInfo,
        scenic: [ScenicInfo] = [],
        store: [RolePermissionStoreInfo] = []
    ) {
        self.role = role
        self.scenic = scenic
        self.store = store
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        role = try container.decode(RoleInfo.self, forKey: .role)
        scenic = try container.decodeIfPresent([ScenicInfo].self, forKey: .scenic) ?? []
        store = try container.decodeIfPresent([RolePermissionStoreInfo].self, forKey: .store) ?? []
    }
}

/// 当前角色账号可使用的店铺摘要，来源于 `role-permission` 的 `store` 字段。
struct RolePermissionStoreInfo: Codable, Equatable, Sendable {
    let id: Int
    let name: String

    init(id: Int = 0, name: String = "") {
        self.id = id
        self.name = name
    }
}

/// 角色信息与顶层权限列表。
struct RoleInfo: Codable, Equatable, Sendable {
    let id: Int
    let name: String
    let roleCode: String
    let notes: String?
    let permission: [PermissionItem]

    enum CodingKeys: String, CodingKey {
        case id
        case name
        case roleCode = "role_code"
        case notes
        case permission
    }

    init(
        id: Int = 0,
        name: String = "",
        roleCode: String = "",
        notes: String? = nil,
        permission: [PermissionItem] = []
    ) {
        self.id = id
        self.name = name
        self.roleCode = roleCode
        self.notes = notes
        self.permission = permission
    }
}

/// 权限树节点。
struct PermissionItem: Codable, Equatable, Sendable {
    let id: Int
    let pid: Int
    let name: String
    let uri: String
    let iconSrc: String?
    let children: [PermissionItem]?

    enum CodingKeys: String, CodingKey {
        case id
        case pid
        case name
        case uri
        case iconSrc = "icon_src"
        case children
    }

    init(
        id: Int = 0,
        pid: Int = 0,
        name: String = "",
        uri: String = "",
        iconSrc: String? = nil,
        children: [PermissionItem]? = nil
    ) {
        self.id = id
        self.pid = pid
        self.name = name
        self.uri = uri
        self.iconSrc = iconSrc
        self.children = children
    }
}

/// 角色绑定的景区信息。
struct ScenicInfo: Codable, Equatable, Sendable, Hashable {
    let id: Int
    let name: String
    let status: Int
    let location: ScenicLocationInfo?
    let coverImg: String?

    enum CodingKeys: String, CodingKey {
        case id
        case name
        case status
        case location
        case coverImg = "cover_img"
    }

    init(
        id: Int = 0,
        name: String = "",
        status: Int = 0,
        location: ScenicLocationInfo? = nil,
        coverImg: String? = nil
    ) {
        self.id = id
        self.name = name
        self.status = status
        self.location = location
        self.coverImg = coverImg
    }
}

/// 景区位置信息。
struct ScenicLocationInfo: Codable, Equatable, Sendable, Hashable {
    let lng: Double
    let lat: Double
    let address: String
}

/// 扁平化后的权限项，供首页菜单与本地缓存使用。
struct HomePermissionItem: Codable, Equatable, Sendable, Hashable {
    let id: String
    let name: String
    let uri: String
    let pid: Int

    init(id: String, name: String, uri: String, pid: Int = 0) {
        self.id = id
        self.name = name
        self.uri = uri
        self.pid = pid
    }

    init(from item: PermissionItem) {
        self.id = String(item.id)
        self.name = item.name
        self.uri = item.uri
        self.pid = item.pid
    }
}
