package com.erp.warehouse.controller;

import com.erp.common.core.model.ApiResponse;
import com.erp.warehouse.dto.PurchaseOrderDTO;
import com.erp.warehouse.dto.SaleOrderDTO;
import com.erp.warehouse.dto.WarehouseDTO;
import com.erp.warehouse.entity.Warehouse;
import com.erp.warehouse.entity.WarehouseTemplateBinding;
import com.erp.warehouse.service.CloudWarehouseService;
import com.erp.warehouse.service.CloudWarehouseService.PageResult;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 仓库管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/warehouse")
public class WarehouseController {

    @Autowired
    private CloudWarehouseService cloudWarehouseService;

    // ==================== 仓库管理 ====================

    /**
     * 获取仓库列表
     */
    @GetMapping("/list")
    public ApiResponse<PageResult<Warehouse>> listWarehouses(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String type,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int limit) {
        
        PageResult<Warehouse> result = cloudWarehouseService.listWarehouses(name, type, page, limit);
        return ApiResponse.success(result);
    }

    /**
     * 获取仓库详情
     */
    @GetMapping("/{id}")
    public ApiResponse<Warehouse> getWarehouse(@PathVariable Long id) {
        Warehouse warehouse = cloudWarehouseService.getWarehouse(id);
        return ApiResponse.success(warehouse);
    }

    /**
     * 创建仓库
     */
    @PostMapping
    public ApiResponse<Warehouse> createWarehouse(@Valid @RequestBody WarehouseDTO dto) {
        Warehouse warehouse = cloudWarehouseService.createWarehouse(dto);
        return ApiResponse.success("创建成功", warehouse);
    }

    /**
     * 更新仓库
     */
    @PutMapping("/{id}")
    public ApiResponse<Warehouse> updateWarehouse(
            @PathVariable Long id,
            @Valid @RequestBody WarehouseDTO dto) {
        Warehouse warehouse = cloudWarehouseService.updateWarehouse(id, dto);
        return ApiResponse.success("更新成功", warehouse);
    }

    /**
     * 删除仓库
     */
    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteWarehouse(@PathVariable Long id) {
        cloudWarehouseService.deleteWarehouse(id);
        return ApiResponse.success("删除成功", null);
    }

    // ==================== 云仓订单管理 ====================

    /**
     * 推送采购单到云仓
     */
    @PostMapping("/{id}/purchase-order")
    public ApiResponse<Object> pushPurchaseOrder(
            @PathVariable Long id,
            @RequestBody PurchaseOrderDTO purchaseOrder) {
        var response = cloudWarehouseService.pushPurchaseOrder(id, purchaseOrder);
        if (response.isSuccess()) {
            return ApiResponse.success("推送成功", response.getData());
        }
        return ApiResponse.error(response.getCode(), response.getMessage());
    }

    /**
     * 确认采购入库
     */
    @PostMapping("/{id}/purchase-order/confirm")
    public ApiResponse<Object> confirmPurchaseOrder(
            @PathVariable Long id,
            @RequestParam String poOrderNo) {
        var response = cloudWarehouseService.confirmPurchaseOrder(id, poOrderNo);
        if (response.isSuccess()) {
            return ApiResponse.success("确认成功", response.getData());
        }
        return ApiResponse.error(response.getCode(), response.getMessage());
    }

    /**
     * 推送销售订单到云仓
     */
    @PostMapping("/{id}/sale-order")
    public ApiResponse<Object> pushSaleOrder(
            @PathVariable Long id,
            @RequestBody SaleOrderDTO saleOrder) {
        var response = cloudWarehouseService.pushSaleOrder(id, saleOrder);
        if (response.isSuccess()) {
            return ApiResponse.success("推送成功", response.getData());
        }
        return ApiResponse.error(response.getCode(), response.getMessage());
    }

    // ==================== 云仓库存同步 ====================

    /**
     * 查询单SKU库存
     */
    @GetMapping("/{id}/stock")
    public ApiResponse<Object> queryStock(
            @PathVariable Long id,
            @RequestParam String skuCode) {
        Object result = cloudWarehouseService.queryStock(id, skuCode);
        return ApiResponse.success(result);
    }

    /**
     * 获取库存列表
     */
    @GetMapping("/{id}/stock/list")
    public ApiResponse<Object> getStockList(
            @PathVariable Long id,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "50") int pageSize) {
        Object result = cloudWarehouseService.getStockList(id, page, pageSize);
        return ApiResponse.success(result);
    }

    /**
     * 库存调整
     */
    @PostMapping("/{id}/stock/adjust")
    public ApiResponse<Object> adjustStock(
            @PathVariable Long id,
            @RequestParam String skuCode,
            @RequestParam int type,
            @RequestParam int quantity,
            @RequestParam(required = false) String reason) {
        var response = cloudWarehouseService.adjustStock(id, skuCode, type, quantity, reason);
        if (response.isSuccess()) {
            return ApiResponse.success("调整成功", response.getData());
        }
        return ApiResponse.error(response.getCode(), response.getMessage());
    }

    /**
     * 获取云仓商品列表
     */
    @GetMapping("/{id}/goods")
    public ApiResponse<Object> getGoodsList(
            @PathVariable Long id,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "50") int pageSize) {
        Object result = cloudWarehouseService.getGoodsList(id, page, pageSize);
        return ApiResponse.success(result);
    }

    /**
     * 获取商品详情
     */
    @GetMapping("/{id}/goods/{itemId}")
    public ApiResponse<Object> getGoodsDetail(
            @PathVariable Long id,
            @PathVariable String itemId) {
        Object result = cloudWarehouseService.getGoodsDetail(id, itemId);
        return ApiResponse.success(result);
    }

    /**
     * 查询物流轨迹
     */
    @GetMapping("/{id}/logistics/{logisticsNo}")
    public ApiResponse<Object> getLogisticsTrace(
            @PathVariable Long id,
            @PathVariable String logisticsNo) {
        Object result = cloudWarehouseService.getLogisticsTrace(id, logisticsNo);
        return ApiResponse.success(result);
    }

    /**
     * 获取云仓仓库列表
     */
    @GetMapping("/{id}/cloud-warehouses")
    public ApiResponse<Object> getCloudWarehouseList(@PathVariable Long id) {
        Object result = cloudWarehouseService.getCloudWarehouseList(id);
        return ApiResponse.success(result);
    }

    // ==================== 仓库模板绑定 ====================

    /**
     * 获取仓库的模板绑定列表
     */
    @GetMapping("/{id}/bindings")
    public ApiResponse<List<WarehouseTemplateBinding>> getBindings(@PathVariable Long id) {
        List<WarehouseTemplateBinding> bindings = cloudWarehouseService.getBindings(id);
        return ApiResponse.success(bindings);
    }

    /**
     * 创建模板绑定
     */
    @PostMapping("/{id}/bindings")
    public ApiResponse<WarehouseTemplateBinding> createBinding(
            @PathVariable Long id,
            @RequestParam String platform,
            @RequestParam Long templateId) {
        WarehouseTemplateBinding binding = cloudWarehouseService.createBinding(id, platform, templateId);
        return ApiResponse.success("绑定成功", binding);
    }

    /**
     * 更新模板绑定
     */
    @PutMapping("/bindings/{bindingId}")
    public ApiResponse<WarehouseTemplateBinding> updateBinding(
            @PathVariable Long bindingId,
            @RequestParam Long templateId) {
        WarehouseTemplateBinding binding = cloudWarehouseService.updateBinding(bindingId, templateId);
        return ApiResponse.success("更新成功", binding);
    }

    /**
     * 删除模板绑定
     */
    @DeleteMapping("/bindings/{bindingId}")
    public ApiResponse<Void> deleteBinding(@PathVariable Long bindingId) {
        cloudWarehouseService.deleteBinding(bindingId);
        return ApiResponse.success("解绑成功", null);
    }

    // ==================== 云仓连接测试 ====================

    /**
     * 测试云仓连接
     */
    @GetMapping("/{id}/test-connection")
    public ApiResponse<Object> testConnection(@PathVariable Long id) {
        var response = cloudWarehouseService.testConnection(id);
        if (response.isSuccess()) {
            return ApiResponse.success("连接成功", response.getData());
        }
        return ApiResponse.error(response.getCode(), response.getMessage());
    }
}
