package com.carmaintenance.controller;

import com.carmaintenance.dto.Result;
import com.carmaintenance.entity.PartsInventory;
import com.carmaintenance.repository.PartsInventoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 配件库存管理控制器
 */
@RestController
@RequestMapping("/parts")
@CrossOrigin
public class PartsInventoryController {

    @Autowired
    private PartsInventoryRepository partsInventoryRepository;

    /**
     * 获取所有配件
     */
    @GetMapping
    public Result<List<PartsInventory>> getAllParts() {
        List<PartsInventory> parts = partsInventoryRepository.findAll();
        return Result.success(parts);
    }

    /**
     * 根据ID获取配件
     */
    @GetMapping("/{id}")
    public Result<PartsInventory> getPartById(@PathVariable Integer id) {
        PartsInventory part = partsInventoryRepository.findById(id).orElse(null);
        if (part == null) {
            return Result.notFound("配件不存在");
        }
        return Result.success(part);
    }

    /**
     * 根据类别获取配件列表
     */
    @GetMapping("/category/{category}")
    public Result<List<PartsInventory>> getPartsByCategory(@PathVariable String category) {
        List<PartsInventory> parts = partsInventoryRepository.findByCategory(category);
        return Result.success(parts);
    }

    /**
     * 获取库存预警配件
     */
    @GetMapping("/low-stock")
    public Result<List<PartsInventory>> getLowStockParts() {
        List<PartsInventory> allParts = partsInventoryRepository.findAll();
        List<PartsInventory> lowStockParts = allParts.stream()
                .filter(part -> part.getStockQuantity() <= part.getMinStock())
                .collect(java.util.stream.Collectors.toList());
        return Result.success(lowStockParts);
    }

    /**
     * 创建配件
     */
    @PostMapping
    public Result<PartsInventory> createPart(@RequestBody PartsInventory part) {
        if (partsInventoryRepository.findByPartNo(part.getPartNo()).isPresent()) {
            return Result.error("配件编号已存在");
        }
        PartsInventory savedPart = partsInventoryRepository.save(part);
        return Result.success("创建成功", savedPart);
    }

    /**
     * 更新配件
     */
    @PutMapping("/{id}")
    public Result<PartsInventory> updatePart(@PathVariable Integer id, @RequestBody PartsInventory part) {
        PartsInventory existingPart = partsInventoryRepository.findById(id).orElse(null);
        if (existingPart == null) {
            return Result.notFound("配件不存在");
        }

        if (part.getPartName() != null) existingPart.setPartName(part.getPartName());
        if (part.getCategory() != null) existingPart.setCategory(part.getCategory());
        if (part.getBrand() != null) existingPart.setBrand(part.getBrand());
        if (part.getModel() != null) existingPart.setModel(part.getModel());
        if (part.getUnit() != null) existingPart.setUnit(part.getUnit());
        if (part.getUnitPrice() != null) existingPart.setUnitPrice(part.getUnitPrice());
        if (part.getStockQuantity() != null) existingPart.setStockQuantity(part.getStockQuantity());
        if (part.getMinStock() != null) existingPart.setMinStock(part.getMinStock());
        if (part.getSupplier() != null) existingPart.setSupplier(part.getSupplier());
        if (part.getWarehouseLocation() != null) existingPart.setWarehouseLocation(part.getWarehouseLocation());
        if (part.getStatus() != null) existingPart.setStatus(part.getStatus());
        if (part.getRemark() != null) existingPart.setRemark(part.getRemark());

        PartsInventory updatedPart = partsInventoryRepository.save(existingPart);
        return Result.success("更新成功", updatedPart);
    }

    /**
     * 删除配件
     */
    @DeleteMapping("/{id}")
    public Result<Void> deletePart(@PathVariable Integer id) {
        if (!partsInventoryRepository.existsById(id)) {
            return Result.notFound("配件不存在");
        }
        partsInventoryRepository.deleteById(id);
        return Result.<Void>success("删除成功", null);
    }
}
