package com.example.ops_android.data.repository

import com.example.ops_android.data.remote.api.PurchaseApi
import com.example.ops_android.data.remote.api.GetOrdersRequest
import com.example.ops_android.data.remote.api.GetOrderRequest
import com.example.ops_android.data.remote.api.UpdateOrderStatusRequest
import com.example.ops_android.data.model.OrderCounts
import com.example.ops_android.data.model.PurchaseOrder

class OrderRepository(
    private val purchaseApi: PurchaseApi
) {
    suspend fun getOrders(status: String? = null, search: String? = null, page: Int = 1, entries: Int = 20): Result<Pair<List<PurchaseOrder>, Int>> {
        return try {
            val response = purchaseApi.getOrders(GetOrdersRequest(status, search, entries, page))
            if (response.errCode == 0 && response.data != null) {
                Result.success(Pair(response.data.all_orders ?: emptyList(), response.data.all_count))
            } else {
                Result.failure(Exception("获取订单列表失败"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun getOrder(id: Int): Result<PurchaseOrder> {
        return try {
            val response = purchaseApi.getOrder(GetOrderRequest(id))
            if (response.errCode == 0 && response.data?.order != null) {
                // Copy costs/commits into the order for UI convenience
                val order = response.data.order
                Result.success(order)
            } else {
                Result.failure(Exception("获取订单详情失败"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun getOrderCount(): Result<OrderCounts> {
        return try {
            val response = purchaseApi.getOrderCount()
            if (response.errCode == 0 && response.data != null) {
                Result.success(response.data)
            } else {
                Result.failure(Exception("获取订单统计失败"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun updateOrderStatus(id: Int, status: String, comment: String? = null, photos: List<String>? = null): Result<Unit> {
        return try {
            val response = purchaseApi.updateOrderStatus(UpdateOrderStatusRequest(id, status, comment, photos))
            if (response.errCode == 0) Result.success(Unit)
            else Result.failure(Exception("更新订单状态失败"))
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun addOrder(data: Map<String, Any>): Result<Unit> {
        return try {
            val response = purchaseApi.addOrder(data)
            if (response.errCode == 0) Result.success(Unit)
            else Result.failure(Exception("创建订单失败"))
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun updateOrder(data: Map<String, Any>): Result<Unit> {
        return try {
            val response = purchaseApi.updateOrder(data)
            if (response.errCode == 0) Result.success(Unit)
            else Result.failure(Exception("更新订单失败"))
        } catch (e: Exception) {
            Result.failure(e)
        }
    }
}
