import time

from django.shortcuts import render
from BaseModels.openAI.openAI_funcs import send_request

def get_questions_for_category(category, quest_count, level):
    # if level == 1:
    name_level = 'простой'
    if level == 2:
        name_level = 'средний'
    if level == 3:
        name_level = 'сложный'


    # msg = f'создай список (в одну строку, без нумерации) из {quest_count} вопросов уровня сложности "{name_level}" по 3 ответа на каждый из них по теме ' \
    #       f'"{category.name}", ответы выведи в одну строку с разделителем "|", перед правильным ответом поставь символ "~", перед строкой каждого вопроса ставь символ "#"'
    msg = f'Cоздай список (в одну строку, без нумерации) из {quest_count} вопросов по теме "{category.name}", уровень сложности вопросов - "{name_level}", к каждому вопросу подготовь 2 неверных ответа и 1 верный (без нумерации, с разделителем "|"), перед верным ответом поставь символ "~", перед каждым вопросом поставь символ "#"'
    # print(msg)
    res = send_request(msg)
    return res

def get_questions():
    from QuestionsApp.models import Question, QuestionCategory, Answer

    answers_count = 3
    request_questions_count = 2

    from django.db.models import Count, F
    cats = QuestionCategory.objects.filter(enable=True).exclude(parent_category=None).annotate(
        quest_count=Count('rel_questions_for_category')
    ).order_by('quest_count')
    for cat in cats:
        for level in [1, 2, 3]:
            res = get_questions_for_category(cat, request_questions_count, level)

            data = res['choices'][0]['message']['content']
            print(data)
            res_quests = data.split('#')
            for res_quest in res_quests:
                if not res_quest:
                    continue

                check_right_answer_count = 0

                try:

                    res_quest_struct_list = res_quest.split('|')
                    if len(res_quest_struct_list) < 2:
                        continue

                    quest_for_add = prepare_phrase(res_quest_struct_list[0])
                    if not quest_for_add:
                        continue

                    print(quest_for_add)

                    quest_dublicate = Question.objects.filter(name=quest_for_add).count()
                    if quest_dublicate:
                        msg = f'! - дубликат вопроса "{quest_for_add}" > пропускаем'
                        print(msg)
                        continue

                    quest_obj = Question.objects.create(
                        name=quest_for_add,
                        category=cat,
                        game_level=level,
                    )

                    answer_objs_list = []

                    answers_list = res_quest_struct_list[1:]#.split('|')

                    for answer in answers_list:
                        if not answer:
                            continue

                        right_answer = False

                        answer = answer.strip()
                        if answer[0] == '~':
                            right_answer = True
                            check_right_answer_count += 1

                        answer_for_add = prepare_phrase(answer)
                        if not answer_for_add:
                            continue

                        answer_obj = Answer(
                            name=answer_for_add,
                            right_answer=right_answer,
                            question=quest_obj
                        )
                        answer_objs_list.append(answer_obj)
                        print(f' - {answer_for_add} > {right_answer}')

                    if len(answer_objs_list) != answers_count or check_right_answer_count != 1:
                        Question.objects.filter(id=quest_obj.id).delete()
                        msg = f'!!! - неверное количество ответов или правильных ответов'
                        print(msg)
                        continue

                    Answer.objects.bulk_create(answer_objs_list)


                except Exception as e:
                    msg = f'!!! --- get_questions question {res_quest} > processed ERROR = {str(e)}'
                    print(msg)

        # максимум 3 запроса в минуту
        time.sleep(30)

    return True


def get_sub_categories_for_category(category):
    msg = f'создай 500 подтематик для тематики {category.name} и выведи с разделителем ^ в одну строку без нумерации'
    print(msg)
    res = send_request(msg)
    return res

def prepare_phrase(phrase):

    try:
        name = phrase.replace('\n', '')
        while name and name[-1] in (',', '.', ':', ';', '-', ' '):
            name = name[:-1]

        while name and name[0] in (',', '.', ':', ';', '-', ' ', '~'):
            name = name[1:]

        if len(name) > 3 and name[1] == '.':
            name = name[2:]

        if len(name) > 3 and name[1] == ' ':
            try:
                i = int(name[0])
                name = name[2:]
            except:
                pass

            name = name[2:]

        # убираем маркировку подпунктов
        if name and name[1] == ')':
            name = name[2:]

        name = name.strip()
    except Exception as e:
        msg = f'prepare_phrase "{phrase}" error = {str(e)}'
        print(msg)
        name = ''

    return name


def get_subcategories():
    from QuestionsApp.models import QuestionCategory
    categories = QuestionCategory.objects.all()

    cats_names_list = list(categories.values_list('name', flat=True))


    for cat in categories:
        sub_cats_objs = []

        res = get_sub_categories_for_category(cat)

        data = res['choices'][0]['message']['content']
        # data = data.replace('-', '')

        sub_cats_list = data.split('^')
        for sub_cat in sub_cats_list:
            name = prepare_phrase(sub_cat)
            if not name:
                msg = f'! - {sub_cat} > проблема в строке, пропускаем'
                print(msg)
                continue

            if name in cats_names_list:
                msg = f'! - {name} > пропускаем, дубликат'
                print(msg)
                continue

            print(name)
            sub_cat_obj = QuestionCategory(
                name=name,
                parent_category=cat
            )
            sub_cats_objs.append(sub_cat_obj)
            cats_names_list.append(name)

        QuestionCategory.objects.bulk_create(sub_cats_objs)
        # cat.parsed = True

    return True
