from flask import Blueprint, jsonify, request

from services.predict_service import predict_service

predict_bp = Blueprint('predict', __name__, url_prefix='/api/predict')


@predict_bp.route('/single', methods=['POST'])
def predict_single():
    try:
        data = request.get_json()
        
        if not data:
            return jsonify({
                'code': 400,
                'message': 'Request body is required',
                'data': None
            }), 400
        
        model_type = data.get('model_type')
        
        result = predict_service.predict_single(data, model_type)
        
        return jsonify({
            'code': 200,
            'message': 'success',
            'data': result
        })
    except Exception as e:
        return jsonify({
            'code': 500,
            'message': str(e),
            'data': None
        }), 500


@predict_bp.route('/compare', methods=['POST'])
def predict_compare():
    try:
        data = request.get_json()
        
        if not data:
            return jsonify({
                'code': 400,
                'message': 'Request body is required',
                'data': None
            }), 400
        
        results = predict_service.predict_compare(data)
        
        return jsonify({
            'code': 200,
            'message': 'success',
            'data': {
                'results': results,
                'total_models': len(results)
            }
        })
    except Exception as e:
        return jsonify({
            'code': 500,
            'message': str(e),
            'data': None
        }), 500


@predict_bp.route('/models', methods=['GET'])
def get_models():
    try:
        models = predict_service.get_available_models()
        
        return jsonify({
            'code': 200,
            'message': 'success',
            'data': {
                'models': models,
                'total': len(models)
            }
        })
    except Exception as e:
        return jsonify({
            'code': 500,
            'message': str(e),
            'data': None
        }), 500


@predict_bp.route('/model-info', methods=['GET'])
def get_model_info():
    try:
        result = predict_service.get_model_info()
        return jsonify({
            'code': 200,
            'message': 'success',
            'data': result
        })
    except Exception as e:
        return jsonify({
            'code': 500,
            'message': str(e),
            'data': None
        }), 500


@predict_bp.route('/risk-classify', methods=['POST'])
def risk_classify():
    try:
        data = request.get_json()
        if not data:
            return jsonify({'code': 400, 'message': 'Request body is required', 'data': None}), 400
        model_type = data.get('model_type')
        result = predict_service.predict_risk_classification(data, model_type)
        if result is None:
            return jsonify({'code': 404, 'message': 'No classifier available', 'data': None}), 404
        return jsonify({'code': 200, 'message': 'success', 'data': result})
    except Exception as e:
        return jsonify({'code': 500, 'message': str(e), 'data': None}), 500
