from flask import Blueprint, request, jsonify
from config import db
from models.ai import AIModel
from utils.decorators import token_required

model_bp = Blueprint('model_routes', __name__, url_prefix='/api/models')

@model_bp.route('', methods=['GET', 'OPTIONS'])
def get_models():
    """Get all AI models"""
    if request.method == 'OPTIONS':
        return '', 200
    try:
        models = AIModel.query.order_by(AIModel.name.asc()).all()
        return jsonify({
            'models': [model.to_dict() for model in models]
        }), 200
    except Exception as e:
        return jsonify({'error': str(e)}), 500

@model_bp.route('/<int:model_id>', methods=['GET'])
def get_model(model_id):
    """Get specific AI model details"""
    try:
        model = db.session.get(AIModel, model_id)
        if not model:
            return jsonify({'error': 'Model not found'}), 404
        return jsonify({'model': model.to_dict()}), 200
    except Exception as e:
        return jsonify({'error': str(e)}), 500
