package com.erp.purchase.controller;

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

@Slf4j
@RestController
@RequestMapping("/api/purchase/orders")
@RequiredArgsConstructor
public class PurchaseOrderController {

    private final PurchaseOrderService purchaseOrderService;

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

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

    @GetMapping("/no/{orderNo}")
    public ApiResponse<PurchaseOrder> getByOrderNo(@PathVariable String orderNo) {
        try {
            PurchaseOrder order = purchaseOrderService.getByOrderNo(orderNo);
            return ApiResponse.success(order);
        } catch (IllegalArgumentException e) {
            return ApiResponse.error(404, e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }

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

    @PutMapping
    public ApiResponse<PurchaseOrder> update(@Valid @RequestBody PurchaseOrderDTO.UpdateRequest request,
                                              @RequestHeader(value = "X-Operator-Id", required = false) Long operatorId) {
        try {
            PurchaseOrder order = purchaseOrderService.update(request, operatorId);
            return ApiResponse.success("更新成功", order);
        } catch (IllegalArgumentException e) {
            return ApiResponse.error(422, e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable Long id,
                                     @RequestHeader(value = "X-Operator-Id", required = false) Long operatorId) {
        try {
            purchaseOrderService.delete(id, operatorId);
            return ApiResponse.<Void>success("删除成功", null);
        } catch (IllegalArgumentException e) {
            return ApiResponse.error(422, e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }

    @PostMapping("/{id}/submit")
    public ApiResponse<Void> submit(@PathVariable Long id,
                                     @RequestHeader(value = "X-Operator-Id", required = false) Long operatorId) {
        try {
            purchaseOrderService.submit(id, operatorId);
            return ApiResponse.<Void>success("提交成功", null);
        } catch (IllegalArgumentException e) {
            return ApiResponse.error(422, e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }

    @PostMapping("/{id}/approve")
    public ApiResponse<PurchaseOrder> approve(@PathVariable Long id,
                                               @RequestBody PurchaseOrderDTO.ApproveRequest request,
                                               @RequestHeader(value = "X-Operator-Id", required = false) Long operatorId) {
        try {
            PurchaseOrder order = purchaseOrderService.approve(id, request, operatorId);
            return ApiResponse.success("审批成功", order);
        } catch (IllegalArgumentException e) {
            return ApiResponse.error(422, e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }

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