package com.example.ops_android.data.repository

import com.example.ops_android.data.model.Container
import com.example.ops_android.data.model.Item
import com.example.ops_android.data.remote.api.WarehouseApi
import com.example.ops_android.data.remote.api.ListContainerRequest
import com.example.ops_android.data.remote.api.GetContainerRequest
import com.example.ops_android.data.remote.api.ListItemRequest
import com.example.ops_android.data.remote.api.GetItemRequest
import com.example.ops_android.data.remote.api.ItemResponse
import com.example.ops_android.data.remote.api.MoveItemRequest
import com.example.ops_android.data.remote.api.WarehouseCountResponse

class WarehouseRepository(
    private val warehouseApi: WarehouseApi
) {
    suspend fun listContainer(parentId: Int? = null): Result<List<Container>> {
        return try {
            val response = warehouseApi.listContainer(ListContainerRequest(parentId))
            if (response.errCode == 0 && response.data != null) {
                Result.success(response.data.containers ?: emptyList())
            } else {
                Result.failure(Exception("获取容器列表失败"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun getContainer(id: Int): Result<Container> {
        return try {
            val response = warehouseApi.getContainer(GetContainerRequest(id))
            if (response.errCode == 0 && response.data?.container != null) {
                Result.success(response.data.container)
            } else {
                Result.failure(Exception("获取容器详情失败"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

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

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

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

    suspend fun listItem(containerId: Int? = null): Result<List<Item>> {
        return try {
            val response = warehouseApi.listItem(ListItemRequest(containerId))
            if (response.errCode == 0 && response.data != null) {
                Result.success(response.data.items ?: emptyList())
            } else {
                Result.failure(Exception("获取物品列表失败"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun searchItems(query: String): Result<List<Item>> {
        return try {
            val request = ListItemRequest(search = query, entries = 10)
            val response = warehouseApi.listItem(request)
            if (response.errCode == 0 && response.data != null) {
                Result.success(response.data.items ?: emptyList())
            } else {
                Result.failure(Exception("搜索物品失败"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun getItem(id: Int): Result<ItemResponse> {
        return try {
            val response = warehouseApi.getItem(GetItemRequest(id))
            if (response.errCode == 0 && response.data != null) {
                val resp = response.data
                val itm = resp.item?.copy(
                    linkedCustomers = resp.customers,
                    containerBreadcrumb = resp.container_breadcrumb
                )
                Result.success(resp.copy(item = itm))
            } else {
                Result.failure(Exception("获取物品详情失败"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

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

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

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

    suspend fun moveItem(itemId: Int, newContainer: Int?): Result<Unit> {
        return try {
            val response = warehouseApi.moveItem(MoveItemRequest(itemId, newContainer))
            if (response.errCode == 0) Result.success(Unit)
            else Result.failure(Exception("移动物品失败"))
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

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