from flask import Blueprint, request, jsonify from backend.models import db, Game, GameQuestion, Team, Question bp = Blueprint('games', __name__, url_prefix='/api/games') @bp.route('', methods=['GET']) def list_games(): """Get all games""" games = Game.query.order_by(Game.created_at.desc()).all() return jsonify([g.to_dict(include_teams=True) for g in games]), 200 @bp.route('/', methods=['GET']) def get_game(game_id): """Get a single game by ID with full details""" game = Game.query.get_or_404(game_id) return jsonify(game.to_dict(include_questions=True, include_teams=True)), 200 @bp.route('', methods=['POST']) def create_game(): """Create a new game""" data = request.get_json() if not data: return jsonify({'error': 'No data provided'}), 400 name = data.get('name') if not name: return jsonify({'error': 'Game name is required'}), 400 try: game = Game(name=name) db.session.add(game) db.session.commit() return jsonify(game.to_dict()), 201 except Exception as e: db.session.rollback() return jsonify({'error': str(e)}), 500 @bp.route('/', methods=['DELETE']) def delete_game(game_id): """Delete a game""" game = Game.query.get_or_404(game_id) try: db.session.delete(game) db.session.commit() return jsonify({'message': 'Game deleted successfully'}), 200 except Exception as e: db.session.rollback() return jsonify({'error': str(e)}), 500 @bp.route('//questions', methods=['POST']) def add_questions_to_game(game_id): """Add questions to a game Expects JSON: { "question_ids": [1, 2, 3, ...] } """ game = Game.query.get_or_404(game_id) data = request.get_json() if not data or 'question_ids' not in data: return jsonify({'error': 'question_ids array is required'}), 400 question_ids = data['question_ids'] if not isinstance(question_ids, list): return jsonify({'error': 'question_ids must be an array'}), 400 try: # Remove existing questions for this game GameQuestion.query.filter_by(game_id=game_id).delete() # Add new questions with order for order, question_id in enumerate(question_ids): question = Question.query.get(question_id) if not question: db.session.rollback() return jsonify({'error': f'Question with ID {question_id} not found'}), 404 game_question = GameQuestion( game_id=game_id, question_id=question_id, order=order ) db.session.add(game_question) db.session.commit() return jsonify(game.to_dict(include_questions=True, include_teams=True)), 200 except Exception as e: db.session.rollback() return jsonify({'error': str(e)}), 500 @bp.route('//teams', methods=['POST']) def add_team_to_game(game_id): """Add a team to a game Expects JSON: { "name": "Team Name" } """ game = Game.query.get_or_404(game_id) data = request.get_json() if not data or 'name' not in data: return jsonify({'error': 'Team name is required'}), 400 try: team = Team(name=data['name'], game_id=game_id) db.session.add(team) db.session.commit() return jsonify(team.to_dict()), 201 except Exception as e: db.session.rollback() return jsonify({'error': str(e)}), 500 @bp.route('//save-template', methods=['POST']) def save_as_template(game_id): """Mark a game as a template for reuse""" game = Game.query.get_or_404(game_id) try: game.is_template = True db.session.commit() return jsonify(game.to_dict(include_questions=True)), 200 except Exception as e: db.session.rollback() return jsonify({'error': str(e)}), 500 @bp.route('/templates', methods=['GET']) def list_templates(): """Get all game templates""" templates = Game.query.filter_by(is_template=True).order_by(Game.created_at.desc()).all() return jsonify([g.to_dict(include_questions=True) for g in templates]), 200 @bp.route('//clone', methods=['POST']) def clone_template(template_id): """Clone a template to create a new game Expects JSON: { "name": "New Game Name" } """ template = Game.query.get_or_404(template_id) data = request.get_json() if not data or 'name' not in data: return jsonify({'error': 'Game name is required'}), 400 try: # Create new game new_game = Game(name=data['name'], is_template=False) db.session.add(new_game) db.session.flush() # Get new game ID # Clone questions for gq in template.game_questions: new_gq = GameQuestion( game_id=new_game.id, question_id=gq.question_id, order=gq.order ) db.session.add(new_gq) db.session.commit() return jsonify(new_game.to_dict(include_questions=True)), 201 except Exception as e: db.session.rollback() return jsonify({'error': str(e)}), 500