# -*- coding: utf-8 -*-
"""
车辆数据相关API蓝图
处理车辆数据查询、添加、监控等功能
"""

from flask import Blueprint, request, jsonify

vehicle_bp = Blueprint('vehicle', __name__, url_prefix='/api/vehicle')


@vehicle_bp.route('/data')
def get_vehicle_data():
    """获取车辆数据"""
    try:
        from src.web.service_manager import service_manager
        vehicle_id = request.args.get('vehicle_id')
        vehicle_vin = request.args.get('vehicle_vin')
        data_type = request.args.get('data_type')
        limit = request.args.get('limit', 10, type=int)

        vehicle_mgr = service_manager.get_vehicle_manager()
        if vehicle_vin:
            data = vehicle_mgr.get_vehicle_data_by_vin(vehicle_vin, data_type, limit)
        elif vehicle_id:
            data = vehicle_mgr.get_vehicle_data(vehicle_id, data_type, limit)
        else:
            data = vehicle_mgr.search_vehicle_data(limit=limit)

        return jsonify(data)
    except Exception as e:
        return jsonify({"error": str(e)}), 500


@vehicle_bp.route('/data/vin/<vehicle_vin>/latest')
def get_latest_vehicle_data_by_vin(vehicle_vin):
    """按VIN获取车辆最新数据"""
    try:
        from src.web.service_manager import service_manager
        data = service_manager.get_vehicle_manager().get_latest_vehicle_data_by_vin(vehicle_vin)
        return jsonify(data)
    except Exception as e:
        return jsonify({"error": str(e)}), 500


@vehicle_bp.route('/data/<vehicle_id>/latest')
def get_latest_vehicle_data(vehicle_id):
    """获取车辆最新数据"""
    try:
        from src.web.service_manager import service_manager
        data = service_manager.get_vehicle_manager().get_latest_vehicle_data(vehicle_id)
        return jsonify(data)
    except Exception as e:
        return jsonify({"error": str(e)}), 500


@vehicle_bp.route('/data/<vehicle_id>/summary')
def get_vehicle_summary(vehicle_id):
    """获取车辆数据摘要"""
    try:
        from src.web.service_manager import service_manager
        summary = service_manager.get_vehicle_manager().get_vehicle_summary(vehicle_id)
        return jsonify(summary)
    except Exception as e:
        return jsonify({"error": str(e)}), 500


@vehicle_bp.route('/data', methods=['POST'])
def add_vehicle_data():
    """添加车辆数据"""
    try:
        from src.web.service_manager import service_manager
        data = request.get_json()
        success = service_manager.get_vehicle_manager().add_vehicle_data(
            vehicle_id=data['vehicle_id'],
            data_type=data['data_type'],
            data_value=data['data_value'],
            vehicle_vin=data.get('vehicle_vin')
        )
        return jsonify({"success": success, "message": "数据添加成功" if success else "添加失败"})
    except Exception as e:
        return jsonify({"error": str(e)}), 500


@vehicle_bp.route('/init-sample-data', methods=['POST'])
def init_sample_vehicle_data():
    """初始化示例车辆数据"""
    try:
        from src.web.service_manager import service_manager
        success = service_manager.get_vehicle_manager().add_sample_vehicle_data()
        return jsonify({"success": success, "message": "示例数据初始化成功" if success else "初始化失败"})
    except Exception as e:
        return jsonify({"error": str(e)}), 500
