package com.erp.purchase.controller;

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

@Slf4j
@RestController
@RequestMapping("/api/purchase/returns")
@RequiredArgsConstructor
public class PurchaseReturnController {

    private final PurchaseReturnService purchaseReturnService;

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

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

    @GetMapping("/no/{returnNo}")
    public ApiResponse<PurchaseReturn> getByReturnNo(@PathVariable String returnNo) {
        try {
            PurchaseReturn ret = purchaseReturnService.getByReturnNo(returnNo);
            return ApiResponse.success(ret);
        } catch (IllegalArgumentException e) {
            return ApiResponse.error(404, e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }

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

    @PostMapping("/{id}/confirm")
    public ApiResponse<PurchaseReturn> confirmReturn(@PathVariable Long id,
                                                       @RequestBody PurchaseReturnDTO.ConfirmRequest request,
                                                       @RequestHeader(value = "X-Operator-Id", required = false) Long operatorId) {
        try {
            PurchaseReturn ret = purchaseReturnService.confirmReturn(id, request, operatorId);
            return ApiResponse.success("确认退货成功", ret);
        } 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 {
            purchaseReturnService.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());
        }
    }
}
