//
//  OrdersAPI.swift
//  suixinkan
//
//  Created by Codex on 2026/6/22.
//

import Foundation
import Observation

@MainActor
/// 订单服务协议，抽象订单列表、核销列表和核销操作以便测试替换。
protocol OrderServing {
    /// 获取订单管理列表。
    func orderList(
        scenicId: Int,
        page: Int,
        pageSize: Int,
        orderStatus: Int?,
        userPhone: String?,
        startTime: String?,
        endTime: String?,
        isRefined: Int?,
        isScenicAdmin: Bool
    ) async throws -> ListPayload<OrderEntity>

    /// 获取核销订单列表。
    func writeOffList(scenicId: Int, storeId: Int?, page: Int, pageSize: Int) async throws -> ListPayload<WriteOffOrderItem>

    /// 核销指定订单号。
    func writeOff(orderNumber: String) async throws

    /// 获取门店订单详情。
    func storeOrderDetail(storeId: Int, orderNumber: String) async throws -> StoreOrderDetailResponse
}

@MainActor
@Observable
/// 订单 API，封装订单管理、核销订单和订单核销接口。
final class OrdersAPI: OrderServing {
    @ObservationIgnored private let client: APIClient

    /// 初始化订单 API，并注入共享网络客户端。
    init(client: APIClient) {
        self.client = client
    }

    /// 获取订单管理列表。
    func orderList(
        scenicId: Int,
        page: Int = 1,
        pageSize: Int = 20,
        orderStatus: Int? = nil,
        userPhone: String? = nil,
        startTime: String? = nil,
        endTime: String? = nil,
        isRefined: Int? = nil,
        isScenicAdmin: Bool = false
    ) async throws -> ListPayload<OrderEntity> {
        var query = [
            URLQueryItem(name: "scenic_id", value: "\(scenicId)"),
            URLQueryItem(name: "page", value: "\(max(page, 1))"),
            URLQueryItem(name: "page_size", value: "\(max(pageSize, 1))")
        ]
        if let orderStatus, orderStatus > 0 {
            query.append(URLQueryItem(name: "order_status", value: "\(orderStatus)"))
        }
        if let userPhone, !userPhone.isEmpty {
            query.append(URLQueryItem(name: "user_phone", value: userPhone))
        }
        if let startTime, !startTime.isEmpty {
            query.append(URLQueryItem(name: "start_time", value: startTime))
        }
        if let endTime, !endTime.isEmpty {
            query.append(URLQueryItem(name: "end_time", value: endTime))
        }
        if let isRefined, isRefined > 0 {
            query.append(URLQueryItem(name: "is_refined", value: "\(isRefined)"))
        }

        return try await client.send(
            APIRequest(
                method: .get,
                path: isScenicAdmin ? "/api/app/scenic-admin/order/list" : "/api/yf-handset-app/photog/order/listv2",
                queryItems: query
            )
        )
    }

    /// 获取核销订单列表。
    func writeOffList(scenicId: Int, storeId: Int? = nil, page: Int = 1, pageSize: Int = 20) async throws -> ListPayload<WriteOffOrderItem> {
        var queryItems = [
            URLQueryItem(name: "scenic_id", value: "\(scenicId)"),
            URLQueryItem(name: "page", value: "\(max(page, 1))"),
            URLQueryItem(name: "page_size", value: "\(max(pageSize, 1))")
        ]
        if let storeId, storeId > 0 {
            queryItems.append(URLQueryItem(name: "store_id", value: "\(storeId)"))
        }

        return try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/order/order-verification-list",
                queryItems: queryItems
            )
        )
    }

    /// 核销指定订单号。
    func writeOff(orderNumber: String) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/order/order-verify",
                body: WriteOffRequest(orderNumber: orderNumber)
            )
        )
    }

    /// 获取门店订单详情。
    func storeOrderDetail(storeId: Int, orderNumber: String) async throws -> StoreOrderDetailResponse {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/app/store/order/detail",
                queryItems: [
                    URLQueryItem(name: "store_id", value: "\(storeId)"),
                    URLQueryItem(name: "order_number", value: orderNumber)
                ]
            )
        )
    }
}
