import Foundation

/// `qianapp://` 协议解析结果。
///
/// **协议形态**:`qianapp://<custom-path>?flowId=...&action=...&result=...&from=tencent_ess&<extras>`
///
/// **设计要点(参见 design.md D5)**:
/// - **不**包含 `errorCode` / `errorMsg`(如有则落入 `extras`)
/// - `action` / `result` **保持 String** 不转 enum,空字符串与未知值原样保留
/// - 重复 query key 取首个,与 H5 团队约定一致
///
/// **跨端一致性**:与 Android `SignResult` data class 字段 1:1 对齐。
public struct SignResult: Equatable {

    /// 原始 `qianapp://` URL 字面量
    public let rawUrl: String

    /// host + path,例 `callback/sign`
    public let customPath: String

    /// 流程 ID;协议未给则为空字符串
    public let flowId: String

    /// 动作类型(String 直透,未识别值原样透出)。已知值见 ``SignAction``。
    public let action: String

    /// 结果状态(String 直透,未识别值原样透出)。已知值见 ``SignResultStatus``。
    public let result: String

    /// 来源标识,通常为 `tencent_ess`
    public let from: String

    /// 客户自定义 query 透传(已剔除 4 个 recognized field)。
    /// 重复 key 保留首个,UTF-8 percent-decoded。
    public let extras: [String: String]

    public init(
        rawUrl: String,
        customPath: String,
        flowId: String,
        action: String,
        result: String,
        from: String,
        extras: [String: String]
    ) {
        self.rawUrl = rawUrl
        self.customPath = customPath
        self.flowId = flowId
        self.action = action
        self.result = result
        self.from = from
        self.extras = extras
    }

    /// 全空 fallback,用于非法 URL 解析失败场景。
    public static let empty: SignResult = SignResult(
        rawUrl: "",
        customPath: "",
        flowId: "",
        action: "",
        result: "",
        from: "",
        extras: [:]
    )
}
