package com.qaup.collision.controller;

import com.qaup.collision.common.model.dto.Response;
import com.qaup.collision.common.model.spatial.VehicleLocation;
import com.qaup.collision.datacollector.model.dto.VehicleCommand;
import com.qaup.collision.datacollector.model.dto.VehicleStateInfo;
import com.qaup.collision.datacollector.model.dto.VehicleStateRequest;
import com.qaup.collision.datacollector.service.UnmannedVehicleControlService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 无人车控制接口控制器
 * 实现官方API文档第2章要求的无人车控制接口
 * 
 * @author AI Assistant
 * @version 1.0
 * @since 2024-12-19
 */
@RestController
@RequestMapping("/api")
@Validated
public class UnmannedVehicleController {

    private static final Logger logger = LoggerFactory.getLogger(UnmannedVehicleController.class);

    @Autowired
    private UnmannedVehicleControlService unmannedVehicleControlService;

    /**
     * 无人车控制指令接口
     * 接收外部系统发送的无人车控制指令
     * 
     * @param vehicleCommand 车辆控制指令对象
     * @return 控制指令执行结果
     */
    @PostMapping("/VehicleCommandInfo")
    public ResponseEntity<Response<String>> sendVehicleCommand(
            @Valid @RequestBody VehicleCommand vehicleCommand) {
        
        logger.info("接收到无人车控制指令: transId={}, vehicleId={}, commandType={}", 
                   vehicleCommand.getTransId(), vehicleCommand.getVehicleId(), vehicleCommand.getCommandType());
        
        try {
            String result = unmannedVehicleControlService.processVehicleCommand(vehicleCommand);
            
            logger.info("无人车控制指令处理成功: transId={}, result={}", 
                       vehicleCommand.getTransId(), result);
            
            return ResponseEntity.ok(Response.success("控制指令执行成功", result));
            
        } catch (Exception e) {
            logger.error("无人车控制指令处理失败: transId={}, error={}", 
                        vehicleCommand.getTransId(), e.getMessage(), e);
            
            return ResponseEntity.badRequest()
                    .body(Response.error("控制指令执行失败: " + e.getMessage()));
        }
    }

    /**
     * 无人车位置上报接口
     * 查询指定车辆或所有无人车的当前位置信息
     * 
     * @param vehicleId 车辆ID（可选，为空时返回所有无人车位置）
     * @return 车辆位置信息列表
     */
    @GetMapping("/VehicleLocationInfo")
    public ResponseEntity<Response<List<VehicleLocation>>> getVehicleLocationInfo(
            @RequestParam(required = false) String vehicleId) {
        
        logger.info("查询无人车位置信息: vehicleId={}", vehicleId);
        
        try {
            List<VehicleLocation> locations = unmannedVehicleControlService.getVehicleLocations(vehicleId);
            
            logger.info("查询无人车位置信息成功: vehicleId={}, count={}", vehicleId, locations.size());
            
            return ResponseEntity.ok(Response.success("位置信息查询成功", locations));
            
        } catch (Exception e) {
            logger.error("查询无人车位置信息失败: vehicleId={}, error={}", vehicleId, e.getMessage(), e);
            
            return ResponseEntity.badRequest()
                    .body(Response.error("位置信息查询失败: " + e.getMessage()));
        }
    }

    /**
     * 无人车状态查询接口
     * 查询指定车辆的状态信息
     * 
     * @param request 状态查询请求对象
     * @return 车辆状态信息
     */
    @PostMapping("/VehicleStateInfo")
    public ResponseEntity<Response<VehicleStateInfo>> getVehicleStateInfo(
            @Valid @RequestBody VehicleStateRequest request) {
        
        logger.info("查询无人车状态信息: vehicleId={}", request.getVehicleId());
        
        try {
            // 将Long类型的vehicleId转换为String类型
            VehicleStateInfo vehicleState = unmannedVehicleControlService.getVehicleState(String.valueOf(request.getVehicleId()));
            
            if (vehicleState != null) {
                logger.info("查询无人车状态信息成功: vehicleId={}", request.getVehicleId());
                
                return ResponseEntity.ok(Response.success("状态信息查询成功", vehicleState));
            } else {
                logger.warn("未找到指定车辆的状态信息: vehicleId={}", request.getVehicleId());
                
                return ResponseEntity.badRequest()
                        .body(Response.error("未找到指定车辆的状态信息"));
            }
            
        } catch (Exception e) {
            logger.error("查询无人车状态信息失败: vehicleId={}, error={}", request.getVehicleId(), e.getMessage(), e);
            
            return ResponseEntity.badRequest()
                    .body(Response.error("状态信息查询失败: " + e.getMessage()));
        }
    }
} 