//
//  WeChatShareModels.swift
//  suixinkan
//

import Foundation

/// 微信分享目标场景。
enum WeChatShareScene: Int, CaseIterable, Sendable {
    /// 微信好友会话。
    case session = 0
    /// 朋友圈。
    case timeline = 1
    /// 微信收藏。
    case favorite = 2

    /// 展示文案。
    var title: String {
        switch self {
        case .session:
            return "微信好友"
        case .timeline:
            return "朋友圈"
        case .favorite:
            return "收藏"
        }
    }

    /// 映射到微信 SDK 的 `scene` 整数值。
    var wxSceneValue: Int32 {
        Int32(rawValue)
    }
}

/// 微信分享请求结果。
enum WeChatShareResult: Equatable, Error, Sendable {
    case success
    case cancelled
    case notInstalled
    case unsupported
    case invalidPayload(String)
    case sendFailed(Int)
    case unknown(Int)

    /// 根据微信 SDK 错误码转换为业务可识别的分享结果。
    static func fromWXErrorCode(_ code: Int32) -> WeChatShareResult {
        switch code {
        case 0:
            return .success
        case -2:
            return .cancelled
        default:
            return .unknown(Int(code))
        }
    }
}

/// 微信小程序分享版本。
enum WeChatMiniProgramType: Int, Equatable, Sendable {
    /// 正式版。
    case release = 0
    /// 开发版。
    case test = 1
    /// 体验版。
    case preview = 2

    /// 当前编译配置下实际传给微信 SDK 的小程序版本。
    var resolvedForCurrentBuild: WeChatMiniProgramType {
        #if DEBUG
        return .preview
        #else
        return self
        #endif
    }
}

/// 网页分享载荷，供邀请等业务模块调用微信分享。
struct WeChatWebPageSharePayload: Equatable, Sendable {
    let title: String
    let description: String
    let url: String
}

/// 文本分享载荷。
struct WeChatTextSharePayload: Equatable, Sendable {
    let text: String
}

/// 图片分享载荷。
struct WeChatImageSharePayload: Equatable, Sendable {
    let imageData: Data
}

/// 微信小程序分享载荷。
struct WeChatMiniProgramSharePayload: Equatable, Sendable {
    let title: String
    let description: String
    let webpageURL: String
    let userName: String
    let path: String?
    let miniProgramType: WeChatMiniProgramType
    let withShareTicket: Bool
    let disableForward: Bool

    /// 初始化小程序分享载荷。
    init(
        title: String,
        description: String,
        webpageURL: String,
        userName: String,
        path: String? = nil,
        miniProgramType: WeChatMiniProgramType = .release,
        withShareTicket: Bool = false,
        disableForward: Bool = false
    ) {
        self.title = title
        self.description = description
        self.webpageURL = webpageURL
        self.userName = userName
        self.path = path
        self.miniProgramType = miniProgramType
        self.withShareTicket = withShareTicket
        self.disableForward = disableForward
    }
}
