import Foundation

struct OrderLineItem: Identifiable {
    let lineItemId: Int
    let parentLineItemId: Int
    let itemId: Int
    let itemName: String
    let itemPrice: Double
    let quantity: Int
    let remark: String
    let isModifier: Bool

    var id: Int { lineItemId }

    init(json: [String: Any]) {
        lineItemId = WorkTask.parseInt(json["LineItemID"]) ?? 0
        parentLineItemId = WorkTask.parseInt(json["ParentLineItemID"]) ?? 0
        itemId = WorkTask.parseInt(json["ItemID"]) ?? 0
        itemName = (json["ItemName"] as? String) ?? ""
        if let d = json["ItemPrice"] as? Double { itemPrice = d }
        else if let i = json["ItemPrice"] as? Int { itemPrice = Double(i) }
        else if let s = json["ItemPrice"] as? String, let d = Double(s) { itemPrice = d }
        else { itemPrice = 0 }
        quantity = WorkTask.parseInt(json["Quantity"]) ?? 1
        remark = (json["Remark"] as? String) ?? ""
        if let b = json["IsModifier"] as? Bool { isModifier = b }
        else if let i = json["IsModifier"] as? Int { isModifier = i == 1 }
        else { isModifier = false }
    }
}
