package com.example.ops_android.data.repository

import com.example.ops_android.data.model.PurchaseOrder
import com.example.ops_android.data.model.WorkOrder
import com.example.ops_android.data.model.WorkOrderCounts
import com.example.ops_android.data.remote.api.WorkOrderApi
import com.example.ops_android.data.remote.api.WorkOrderListRequest
import com.example.ops_android.data.remote.api.WorkOrderGetRequest
import com.example.ops_android.data.remote.api.WorkOrderCommitRequest
import com.example.ops_android.data.remote.api.SearchPurchaseOrdersRequest

class WorkOrderRepository(
    private val workOrderApi: WorkOrderApi
) {
    suspend fun list(status: String? = null, search: String? = null, page: Int = 1, entries: Int = 20): Result<Pair<List<WorkOrder>, Int>> {
        return try {
            val response = workOrderApi.list(WorkOrderListRequest(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 get(id: Int): Result<WorkOrder> {
        return try {
            val response = workOrderApi.get(WorkOrderGetRequest(id))
            if (response.errCode == 0 && response.data?.order != null) {
                val detail = response.data
                val wo = detail.order.copy(
                    linkedCustomers = detail.linkedCustomers
                )
                Result.success(wo)
            } else {
                Result.failure(Exception("获取工单详情失败"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

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

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

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

    suspend fun delete(id: Int): Result<Unit> {
        return try {
            val response = workOrderApi.delete(mapOf("id" to id))
            if (response.errCode == 0) Result.success(Unit)
            else Result.failure(Exception("删除工单失败"))
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun commit(workOrderId: Int, status: String?, comment: String?, photos: List<String>?): Result<Unit> {
        return try {
            val response = workOrderApi.commit(WorkOrderCommitRequest(workOrderId, status, comment, photos))
            if (response.errCode == 0) Result.success(Unit)
            else Result.failure(Exception("提交流程失败"))
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun searchPurchaseOrders(query: String, limit: Int = 10): Result<List<PurchaseOrder>> {
        return try {
            val response = workOrderApi.searchPurchaseOrders(SearchPurchaseOrdersRequest(query, limit))
            if (response.errCode == 0 && response.data != null) {
                val orders = response.data.orders?.map { info ->
                    PurchaseOrder(id = info.id, title = info.title, status = info.status)
                } ?: emptyList()
                Result.success(orders)
            } else {
                Result.failure(Exception("搜索采购订单失败"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }
}
