package com.erp.purchase.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.erp.common.core.model.ApiResponse;
import com.erp.purchase.dto.PurchaseInboundDTO;
import com.erp.purchase.entity.PurchaseInbound;
import com.erp.purchase.service.PurchaseInboundService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/purchase/inbounds")
@RequiredArgsConstructor
public class PurchaseInboundController {

    private final PurchaseInboundService purchaseInboundService;

    @GetMapping("/page")
    public ApiResponse<IPage<PurchaseInbound>> listPage(PurchaseInboundDTO.QueryRequest query) {
        try {
            IPage<PurchaseInbound> page = purchaseInboundService.listPage(query);
            return ApiResponse.success(page);
        } catch (Exception e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public ApiResponse<PurchaseInbound> getById(@PathVariable Long id) {
        try {
            PurchaseInbound inbound = purchaseInboundService.getById(id);
            return ApiResponse.success(inbound);
        } catch (IllegalArgumentException e) {
            return ApiResponse.error(404, e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }

    @GetMapping("/no/{inboundNo}")
    public ApiResponse<PurchaseInbound> getByInboundNo(@PathVariable String inboundNo) {
        try {
            PurchaseInbound inbound = purchaseInboundService.getByInboundNo(inboundNo);
            return ApiResponse.success(inbound);
        } catch (IllegalArgumentException e) {
            return ApiResponse.error(404, e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }

    @PostMapping
    public ApiResponse<PurchaseInbound> create(@Valid @RequestBody PurchaseInboundDTO.CreateRequest request,
                                                @RequestHeader(value = "X-Operator-Id", required = false) Long operatorId) {
        try {
            PurchaseInbound inbound = purchaseInboundService.create(request, operatorId);
            return ApiResponse.success("创建成功", inbound);
        } catch (IllegalArgumentException e) {
            return ApiResponse.error(422, e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }

    @PostMapping("/{id}/confirm")
    public ApiResponse<PurchaseInbound> confirmInbound(@PathVariable Long id,
                                                        @RequestBody PurchaseInboundDTO.ConfirmRequest request,
                                                        @RequestHeader(value = "X-Operator-Id", required = false) Long operatorId) {
        try {
            PurchaseInbound inbound = purchaseInboundService.confirmInbound(id, request, operatorId);
            return ApiResponse.success("确认入库成功", inbound);
        } catch (IllegalArgumentException | IllegalStateException e) {
            return ApiResponse.error(400, e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> cancel(@PathVariable Long id,
                                     @RequestHeader(value = "X-Operator-Id", required = false) Long operatorId) {
        try {
            purchaseInboundService.cancel(id, operatorId);
            return ApiResponse.<Void>success("取消成功", null);
        } catch (IllegalArgumentException | IllegalStateException e) {
            return ApiResponse.error(400, e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }
}
