package com.erp.purchase.controller;

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

@Slf4j
@RestController
@RequestMapping("/api/suppliers")
@RequiredArgsConstructor
public class SupplierController {

    private final SupplierService supplierService;

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

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

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

    @PutMapping
    public ApiResponse<Supplier> update(@Valid @RequestBody SupplierDTO.UpdateRequest request,
                                        @RequestHeader(value = "X-Operator-Id", required = false) Long operatorId) {
        try {
            Supplier supplier = supplierService.update(request, operatorId);
            return ApiResponse.success("更新成功", supplier);
        } 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 {
            supplierService.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());
        }
    }

    @PutMapping("/{id}/status")
    public ApiResponse<Void> updateStatus(@PathVariable Long id,
                                           @RequestParam String status,
                                           @RequestHeader(value = "X-Operator-Id", required = false) Long operatorId) {
        try {
            supplierService.updateStatus(id, status, operatorId);
            return ApiResponse.<Void>success("状态更新成功", null);
        } catch (IllegalArgumentException e) {
            return ApiResponse.error(422, e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }
}
