//
//  LNHttpManager+Order.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/18.
//

import Foundation


private let kNetPath_Order_Create = "/skill/order/payment"

private let kNetPath_Order_List = "/skill/order/list"
private let kNetPath_Order_Detail = "/skill/order/detail"
private let kNetPath_Order_Unfinished = "/skill/order/unfinishedWith"
private let kNetPath_Order_Last = "/playmate/mylast/orderinfo"

private let kNetPath_Order_Finish = "/skill/order/finish"
private let kNetPath_Order_Refund = "/skill/order/refund/apply"
private let kNetPath_Order_Star = "/skill/order/star"
private let kNetPath_Order_Cancel = "/skill/order/canecl"
private let kNetPath_Order_Handler = "/playmate/order/handler"

private let kNetPath_Order_QR_Create = "/skill/create/order/qrcode"
private let kNetPath_Order_QR_Detail = "/skill/view/order/qrcode"
private let kNetPath_Order_QR_Order = "/skill/order/qr/payment"

private let kNetPath_Order_Records = "/playmate/order/list"
private let kNetPath_Order_Protest = "/playmate/submit/refundVoucher"

private let kNetPath_Order_Discount = "/skill/firstOrder/discount/chance"

private enum LNOrderHandlerType: Int {
    case accept = 1
    case reject = 2
    case start = 3
    case done = 4
}

enum LNOrderScene: Int {
    case normal = 0
    case room
}

extension LNHttpManager {
    func createOrderBySkill(
        skillId: String, count: Int, remark: String, scene: LNOrderScene,
        completion: @escaping (LNCreateOrderResponse?, LNHttpError?) -> Void)
    {
        let params: [String: Any] = [
            "skillId": skillId,
            "purchaseQty": count,
            "remark": remark,
            "source": scene.rawValue
        ]
        post(path: kNetPath_Order_Create, params: params, completion: completion)
    }
}

extension LNHttpManager {
    func getOrderList(
        size: Int, next: String,
        completion: @escaping (LNOrderListResponse?, LNHttpError?) -> Void)
    {
        post(path: kNetPath_Order_List, params: [
            "status": -1,
            "page": [
                "size": size, "next": next
            ]
        ], completion: completion)
    }
    
    func getOrderDetail(orderId: String, completion: @escaping (LNOrderDetailResponse?, LNHttpError?) -> Void) {
        post(path: kNetPath_Order_Detail, params: ["id": orderId], completion: completion)
    }
    
    func getOrderRecordList(
        size: Int, next: String,
        completion: @escaping (LNOrderRecordListResponse?, LNHttpError?) -> Void)
    {
        post(path: kNetPath_Order_Records, params: [
            "status": -1,
            "page": [
                "size": size, "next": next
            ]
        ], completion: completion)
    }
    
    func getUnfinishedOrderWith(uid: String, size: Int, next: String,
                                completion: @escaping (LNUnfinishedOrderListResponse?, LNHttpError?) -> Void) {
        post(path: kNetPath_Order_Unfinished, params: [
            "userNo": uid,
            "page": [
                "size": size,
                "next": next
            ]
        ], completion: completion)
    }
    
    func getLastOrder(completion: @escaping (LNLastOrderResponse?, LNHttpError?) -> Void) {
        post(path: kNetPath_Order_Last, completion: completion)
    }
}

extension LNHttpManager {
    func createOrderQR(skillId: String, count: Int, type: LNOrderSource,
                       completion: @escaping (LNCreateOrderQRCodeResponse?, LNHttpError?) -> Void) {
        post(path: kNetPath_Order_QR_Create, params: [
            "skillId": skillId,
            "purchaseQty": count,
            "type": type.rawValue
        ], completion: completion)
    }
    
    func getQRDetail(data: String, completion: @escaping (LNQRCodeDetailResponse?, LNHttpError?) -> Void) {
        post(path: kNetPath_Order_QR_Detail, params: [
            "id": data
        ], completion: completion)
    }
    
    func createQROrder(data: String, count: Int, extra: String, scene: LNOrderScene,
                       completion: @escaping (LNCreateOrderResponse?, LNHttpError?) -> Void) {
        let params: [String: Any] = [
            "qrCode": data,
            "purchaseQty": count,
            "remark": extra,
            "source": scene.rawValue
        ]
        post(path: kNetPath_Order_QR_Order, params: params, completion: completion)
    }
}

extension LNHttpManager {
    func cancelOrder(orderId: String, completion: @escaping (LNHttpError?) -> Void) {
        post(path: kNetPath_Order_Cancel, params: [
            "id": orderId
        ], completion: completion)
    }
    
    func finishOrder(orderId: String, completion: @escaping (LNHttpError?) -> Void) {
        post(path: kNetPath_Order_Finish, params: [
            "id": orderId
        ], completion: completion)
    }
    
    func refundOrder(orderId: String, reason: String, attachments: [String], completion: @escaping (LNHttpError?) -> Void) {
        var atts: [[String: String]] = []
        attachments.forEach {
            atts.append(["attachmentUrl": $0])
        }
        post(path: kNetPath_Order_Refund, params: [
            "orderId": orderId,
            "reason": reason,
            "attachments": atts
        ], completion: completion)
    }
    
    func commentOrder(orderId: String, star: Double, comment: String, completion: @escaping (LNHttpError?) -> Void) {
        post(path: kNetPath_Order_Star, params: [
            "orderId": orderId,
            "star": star,
            "comment": comment
        ], completion: completion)
    }
    
    func rejectOrder(orderId: String, completion: @escaping (LNHttpError?) -> Void) {
        post(path: kNetPath_Order_Handler, params: [
            "orderNo": orderId,
            "type": LNOrderHandlerType.reject.rawValue
        ], completion: completion)
    }
    
    func acceptOrder(orderId: String, completion: @escaping (LNHttpError?) -> Void) {
        post(path: kNetPath_Order_Handler, params: [
            "orderNo": orderId,
            "type": LNOrderHandlerType.accept.rawValue
        ], completion: completion)
    }
    
    func startOrderService(orderId: String, completion: @escaping (LNHttpError?) -> Void) {
        post(path: kNetPath_Order_Handler, params: [
            "orderNo": orderId,
            "type": LNOrderHandlerType.start.rawValue
        ], completion: completion)
    }
    
    func finishOrderService(orderId: String, completion: @escaping (LNHttpError?) -> Void) {
        post(path: kNetPath_Order_Handler, params: [
            "orderNo": orderId,
            "type": LNOrderHandlerType.done.rawValue
        ], completion: completion)
    }
    
    func protestOrder(orderId: String, reason: String, attachments: [String], completion: @escaping (LNHttpError?) -> Void) {
        var atts: [[String: String]] = []
        attachments.forEach {
            atts.append(["attachmentUrl": $0])
        }
        post(path: kNetPath_Order_Protest, params: [
            "orderId": orderId,
            "reason": reason,
            "attachments": atts
        ], completion: completion)
    }
}

extension LNHttpManager {
    func fetchOrderDiscount(completion: @escaping (LNOrderDiscountVO?, LNHttpError?) -> Void) {
        post(path: kNetPath_Order_Discount, completion: completion)
    }
}
