from flask import Blueprint, jsonify, request
from flask_login import login_required, current_user
from models import db

user_bp = Blueprint('user', __name__, url_prefix='/api/user')

@user_bp.route('/preferred_view', methods=['GET'])
@login_required
def get_preferred_view():
    return jsonify({'preferred_view': current_user.preferred_view})

@user_bp.route('/preferred_view', methods=['POST'])
@login_required
def update_preferred_view():
    data = request.get_json()
    if not data or 'preferred_view' not in data:
        return jsonify({'error': 'Missing preferred_view'}), 400
    if data['preferred_view'] not in ['grid', 'list']:
        return jsonify({'error': 'Invalid preferred_view'}), 400
    current_user.preferred_view = data['preferred_view']
    db.session.commit()
    return jsonify({'preferred_view': current_user.preferred_view}) 