from flask import Blueprint, request
from app.di_container import Container
from dependency_injector.wiring import inject, Provide
from app.controllers import QuizController


quiz_bp = Blueprint("quiz", __name__)


@quiz_bp.route("", methods=["POST"])
@inject
def create_quiz(controller: QuizController = Provide[Container.quiz_controller]):
    reqBody = request.get_json()
    return controller.create_quiz(reqBody)


@quiz_bp.route("/ai", methods=["POST"])
@inject
def create_quiz_auto(controller: QuizController = Provide[Container.quiz_controller]):

    reqBody = request.get_json()
    return controller.create_quiz_auto(reqBody)


@quiz_bp.route("/<quiz_id>", methods=["GET"])
@inject
def get_quiz(
    quiz_id: str, controller: QuizController = Provide[Container.quiz_controller]
):
    return controller.get_quiz(quiz_id)


@quiz_bp.route("/answer", methods=["POST"])
@inject
def submit_answer(controller: QuizController = Provide[Container.quiz_controller]):
    req_body = request.get_json()
    return controller.submit_answer(req_body)


@quiz_bp.route("/answer", methods=["GET"])
@inject
def get_answer(controller: QuizController = Provide[Container.quiz_controller]):
    quiz_id = request.args.get("quiz_id")
    user_id = request.args.get("user_id")
    session_id = request.args.get("session_id")

    return controller.get_answer(
        quiz_id=quiz_id, user_id=user_id, session_id=session_id
    )


@quiz_bp.route("/recomendation", methods=["GET"])
@inject
def get_quiz_recommendation(
    controller: QuizController = Provide[Container.quiz_controller],
):
    page = request.args.get("page")
    limit = request.args.get("limit")
    return controller.get_quiz_recommendation(page=page, limit=limit)


@quiz_bp.route("/user/<user_id>", methods=["GET"])
@inject
def get_user_quiz(
    user_id: str, controller: QuizController = Provide[Container.quiz_controller]
):
    page = request.args.get("page", default=1, type=int)
    page_size = request.args.get("page_size", default=10, type=int)
    return controller.get_user_quiz(user_id=user_id, page=page, page_size=page_size)


@quiz_bp.route("/search", methods=["GET"])
@inject
def search_quiz(controller: QuizController = Provide[Container.quiz_controller]):
    keyword = request.args.get("keyword", "")
    subject_id = request.args.get("subject_id")
    page = int(request.args.get("page", 1))
    limit = int(request.args.get("limit", 10))

    return controller.search_quiz(
        keyword=keyword, subject_id=subject_id, page=page, limit=limit
    )
