package com.carmaintenance.data.repository

import com.carmaintenance.data.model.ServiceOrder
import com.carmaintenance.data.remote.ApiResponse
import com.carmaintenance.data.remote.ApiService
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class OrderRepository @Inject constructor(
    private val apiService: ApiService
) {
    suspend fun getOrders(): Result<List<ServiceOrder>> {
        return try {
            val response = apiService.getOrders()
            if (response.isSuccess) {
                Result.success(response.data ?: emptyList())
            } else {
                Result.failure(Exception(response.message ?: "获取工单列表失败"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun getCustomerOrders(customerId: Int): Result<List<ServiceOrder>> {
        return try {
            val response = apiService.getOrdersByCustomer(customerId)
            if (response.isSuccess) {
                Result.success(response.data ?: emptyList())
            } else {
                Result.failure(Exception(response.message ?: "获取工单列表失败"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun updateOrder(orderId: Int, order: ServiceOrder): Result<ServiceOrder> {
        return try {
            val response = apiService.updateOrder(orderId, order)
            if (response.isSuccess && response.data != null) {
                Result.success(response.data)
            } else {
                Result.failure(Exception(response.message ?: "更新工单失败"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }
}
