package com.carmaintenance.controller;

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

import java.util.List;

/**
 * 车辆管理控制器
 */
@RestController
@RequestMapping("/vehicles")
@CrossOrigin
public class VehicleController {

    @Autowired
    private VehicleRepository vehicleRepository;

    /**
     * 获取所有车辆
     */
    @GetMapping
    public Result<List<Vehicle>> getAllVehicles() {
        List<Vehicle> vehicles = vehicleRepository.findAll();
        return Result.success(vehicles);
    }

    /**
     * 根据ID获取车辆
     */
    @GetMapping("/{id}")
    public Result<Vehicle> getVehicleById(@PathVariable Integer id) {
        Vehicle vehicle = vehicleRepository.findById(id).orElse(null);
        if (vehicle == null) {
            return Result.notFound("车辆不存在");
        }
        return Result.success(vehicle);
    }

    /**
     * 根据客户ID获取车辆列表
     */
    @GetMapping("/customer/{customerId}")
    public Result<List<Vehicle>> getVehiclesByCustomerId(@PathVariable Integer customerId) {
        List<Vehicle> vehicles = vehicleRepository.findByCustomerId(customerId);
        return Result.success(vehicles);
    }

    /**
     * 根据车牌号查询车辆
     */
    @GetMapping("/plate/{licensePlate}")
    public Result<Vehicle> getVehicleByPlate(@PathVariable String licensePlate) {
        Vehicle vehicle = vehicleRepository.findByLicensePlate(licensePlate).orElse(null);
        if (vehicle == null) {
            return Result.notFound("车辆不存在");
        }
        return Result.success(vehicle);
    }

    /**
     * 创建车辆档案
     */
    @PostMapping
    public Result<Vehicle> createVehicle(@RequestBody Vehicle vehicle) {
        if (vehicleRepository.findByLicensePlate(vehicle.getLicensePlate()).isPresent()) {
            return Result.error("车牌号已存在");
        }
        if (vehicle.getVin() != null && vehicleRepository.findByVin(vehicle.getVin()).isPresent()) {
            return Result.error("车架号已存在");
        }
        Vehicle savedVehicle = vehicleRepository.save(vehicle);
        return Result.success("创建成功", savedVehicle);
    }

    /**
     * 更新车辆信息
     */
    @PutMapping("/{id}")
    public Result<Vehicle> updateVehicle(@PathVariable Integer id, @RequestBody Vehicle vehicle) {
        Vehicle existingVehicle = vehicleRepository.findById(id).orElse(null);
        if (existingVehicle == null) {
            return Result.notFound("车辆不存在");
        }

        if (vehicle.getColor() != null) existingVehicle.setColor(vehicle.getColor());
        if (vehicle.getMileage() != null) existingVehicle.setMileage(vehicle.getMileage());
        if (vehicle.getLastMaintenanceDate() != null)
            existingVehicle.setLastMaintenanceDate(vehicle.getLastMaintenanceDate());
        if (vehicle.getNextMaintenanceDate() != null)
            existingVehicle.setNextMaintenanceDate(vehicle.getNextMaintenanceDate());
        if (vehicle.getStatus() != null) existingVehicle.setStatus(vehicle.getStatus());

        Vehicle updatedVehicle = vehicleRepository.save(existingVehicle);
        return Result.success("更新成功", updatedVehicle);
    }

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