package com.qaup.collision.datacollector.model.converter;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.springframework.stereotype.Component;

import com.qaup.collision.datacollector.model.dto.VehicleCommand;
import com.qaup.collision.datacollector.model.entity.VehicleCommandEntity;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * VehicleCommand和VehicleCommandEntity之间的转换工具类
 * 
 * 负责在API层DTO和数据持久化实体之间进行数据转换
 * 
 * @author AI Assistant
 * @version 1.0
 * @since 2024-12-19
 */
@Component
public class VehicleCommandConverter {
    
    private final GeometryFactory geometryFactory = new GeometryFactory();
    
    /**
     * 将VehicleCommand DTO转换为VehicleCommandEntity实体
     * 
     * @param dto API层的数据传输对象
     * @return 数据库实体对象
     */
    public VehicleCommandEntity toEntity(VehicleCommand dto) {
        if (dto == null) {
            return null;
        }
        
        // 创建PostGIS Point对象
        Point targetLocation = geometryFactory.createPoint(
            new Coordinate(dto.getLongitude(), dto.getLatitude())
        );
        targetLocation.setSRID(4326); // 设置WGS84坐标系
        
        // 转换时间戳（毫秒）为LocalDateTime
        LocalDateTime timestamp = LocalDateTime.ofInstant(
            Instant.ofEpochMilli(dto.getTimestamp()), 
            ZoneOffset.UTC
        );
        
        return VehicleCommandEntity.builder()
                .transId(dto.getTransId())
                .timestamp(timestamp)
                .vehicleId(dto.getVehicleId())
                .licensePlate(dto.getLicensePlate())
                .commandType(dto.getCommandType())
                .commandReason(dto.getCommandReason())
                .signalState(dto.getSignalState())
                .intersectionId(dto.getIntersectionId())
                .targetLocation(targetLocation)
                .relativeSpeed(dto.getRelativeSpeed() != 0.0 ? dto.getRelativeSpeed() : null)
                .relativeMotionX(dto.getRelativeMotionX() != 0.0 ? dto.getRelativeMotionX() : null)
                .relativeMotionY(dto.getRelativeMotionY() != 0.0 ? dto.getRelativeMotionY() : null)
                .minDistance(dto.getMinDistance() != 0.0 ? dto.getMinDistance() : null)
                .build();
    }
    
    /**
     * 将VehicleCommandEntity实体转换为VehicleCommand DTO
     * 
     * @param entity 数据库实体对象
     * @return API层的数据传输对象
     */
    public VehicleCommand toDto(VehicleCommandEntity entity) {
        if (entity == null) {
            return null;
        }
        
        VehicleCommand dto = new VehicleCommand();
        dto.setTransId(entity.getTransId());
        dto.setTimestamp(entity.getTimestamp().atZone(ZoneOffset.UTC).toInstant().toEpochMilli());
        dto.setVehicleId(entity.getVehicleId());
        dto.setLicensePlate(entity.getLicensePlate());
        dto.setCommandType(entity.getCommandType());
        dto.setCommandReason(entity.getCommandReason());
        dto.setSignalState(entity.getSignalState());
        dto.setIntersectionId(entity.getIntersectionId());
        
        // 转换PostGIS Point为经纬度
        Point location = entity.getTargetLocation();
        if (location != null) {
            dto.setLongitude(location.getX());
            dto.setLatitude(location.getY());
        }
        
        dto.setRelativeSpeed(entity.getRelativeSpeed() != null ? entity.getRelativeSpeed() : 0.0);
        dto.setRelativeMotionX(entity.getRelativeMotionX() != null ? entity.getRelativeMotionX() : 0.0);
        dto.setRelativeMotionY(entity.getRelativeMotionY() != null ? entity.getRelativeMotionY() : 0.0);
        dto.setMinDistance(entity.getMinDistance() != null ? entity.getMinDistance() : 0.0);
        
        return dto;
    }
} 