import Foundation

struct OrderSourcePerson: Identifiable, Hashable {
    let key: String
    let name: String
    let phone: String

    var id: String { key }
}

struct OrderSourceLead: Identifiable, Hashable {
    let key: String
    let userPhone: String
    let remark: String
    let images: [String]

    var id: String { key }
}

struct OrderSourceSelection: Hashable {
    let person: OrderSourcePerson
    let lead: OrderSourceLead
}

extension CooperativeSaler {
    func toOrderSourcePerson() -> OrderSourcePerson {
        OrderSourcePerson(
            key: "\(displayId)",
            name: displayName,
            phone: displayPhone
        )
    }
}

extension ReferralOrder {
    func toOrderSourceLead() -> OrderSourceLead {
        let remarkText: String
        if !remark.isEmpty {
            remarkText = remark
        } else if !statusLabel.isEmpty {
            remarkText = statusLabel
        } else {
            remarkText = "带客单 \(displayReferralOrderNo)"
        }
        return OrderSourceLead(
            key: displayReferralOrderNo,
            userPhone: userMobile,
            remark: remarkText,
            images: images
        )
    }
}

extension BoundReferralOrder {
    func toOrderSourceSelection(fallbackSalerName: String = "", fallbackPhotogRemarkName: String = "") -> OrderSourceSelection? {
        let referralNo = orderNo.trimmingCharacters(in: .whitespacesAndNewlines)
        guard !referralNo.isEmpty else { return nil }
        let fallbackDisplayName = fallbackPhotogRemarkName.saleUserDisplayName(primaryName: fallbackSalerName)
        let personName = displaySalerName.isEmpty
            ? (fallbackDisplayName.isEmpty ? "—" : fallbackDisplayName)
            : displaySalerName
        let remarkText: String
        if !remark.isEmpty {
            remarkText = remark
        } else if !displayText.isEmpty {
            remarkText = displayText
        } else {
            remarkText = "带客单号 \(referralNo)"
        }
        return OrderSourceSelection(
            person: OrderSourcePerson(
                key: saleUserId > 0 ? "\(saleUserId)" : "",
                name: personName,
                phone: salerPhone
            ),
            lead: OrderSourceLead(
                key: referralNo,
                userPhone: userMobile,
                remark: remarkText,
                images: []
            )
        )
    }
}

extension OrderEntity {
    func toBoundOrderSourceSelection() -> OrderSourceSelection? {
        if let referralOrder,
           let selection = referralOrder.toOrderSourceSelection(
            fallbackSalerName: referralSalerName,
            fallbackPhotogRemarkName: referralSalerPhotogRemarkName
           ) {
            return selection
        }
        guard !referralOrderNo.isEmpty else { return nil }
        let personName = displayReferralSalerName.isEmpty ? "—" : displayReferralSalerName
        return OrderSourceSelection(
            person: OrderSourcePerson(
                key: "",
                name: personName,
                phone: ""
            ),
            lead: OrderSourceLead(
                key: referralOrderNo,
                userPhone: phone,
                remark: "带客单号 \(referralOrderNo)",
                images: []
            )
        )
    }
}
