import csv
from tqdm import tqdm
import sqlite3

conn = sqlite3.connect('./database.db')
cur = conn.cursor()

cur.execute('CREATE TABLE IF NOT EXISTS tasks (id INTEGER, correct_answer TEXT, media_url TEXT, weight INTEGER);')
cur.execute('CREATE TABLE IF NOT EXISTS questions (task_id INTEGER, lang TEXT, text TEXT);')
cur.execute('CREATE TABLE IF NOT EXISTS tasks_advanced (id INTEGER, correct_answer TEXT, media_url TEXT, weight INTEGER);')
cur.execute('CREATE TABLE IF NOT EXISTS questions_advanced (task_id INTEGER, lang TEXT, text TEXT, answer_a TEXT, answer_b TEXT, answer_c TEXT);')
cur.execute('CREATE TABLE IF NOT EXISTS categories (name TEXT, task_id INTEGER);')

cur.execute('DELETE FROM tasks;')
cur.execute('DELETE FROM questions;')
cur.execute('DELETE FROM categories;')
cur.execute('DELETE FROM tasks_advanced;')
cur.execute('DELETE FROM questions_advanced;')

with open('./pytania_z_punktacja_11_2025.csv') as file:
    reader = csv.reader(file, delimiter=';')
    next(reader)

    for row in tqdm(reader):
        numer = row[1]
        pytanie_pl = row[2]
        odp_a_pl = row[3]
        odp_b_pl = row[4]
        odp_c_pl = row[5]
        poprawna_odp = row[6]
        media = row[7]
        pkt = row[9]
        kategorie = row[10]
        # pjm_pytanie = row[11]
        # pjm_odp_a = row[12]
        # pjm_odp_b = row[13]
        # pjm_odp_c = row[14]
        pytanie_en = row[15]
        odp_a_en = row[16]
        odp_b_en = row[17]
        odp_c_en = row[18]
        pytanie_de = row[19]
        odp_a_de = row[20]
        odp_b_de = row[21]
        odp_c_de = row[22]
        pytanie_ua = row[23]
        odp_a_ua = row[24]
        odp_b_ua = row[25]
        odp_c_ua = row[26]

        categories = kategorie.split(',')
        task_id = int(numer)

        if numer and pytanie_pl and poprawna_odp and kategorie and not odp_a_pl and not odp_b_pl and not odp_c_pl:
            if poprawna_odp == 'T':
                correct_answer = "true"
            elif poprawna_odp == 'N':
                correct_answer = "false"
            else:
                raise Exception(f'Wystąpił problem, odpowiedź dla pytania {numer} jest niejasna. Poprawna odpowiedź to {poprawna_odp}...')

            cur.execute('INSERT INTO tasks (id, correct_answer, media_url) VALUES (?, ?, ?)',
                        (task_id, correct_answer, media))

            cur.execute('INSERT INTO questions(task_id, lang, text) VALUES (?, ?, ?)',
                        (task_id, 'PL', pytanie_pl))

            if pytanie_en:
                cur.execute('INSERT INTO questions(task_id, lang, text) VALUES (?, ?, ?)',
                            (task_id, 'EN', pytanie_en))
            if pytanie_de:
                cur.execute('INSERT INTO questions(task_id, lang, text) VALUES (?, ?, ?)',
                            (task_id, 'DE', pytanie_de))
            if pytanie_ua:
                cur.execute('INSERT INTO questions(task_id, lang, text) VALUES (?, ?, ?)',
                            (task_id, 'UA', pytanie_ua))

        if numer and pytanie_pl and poprawna_odp and kategorie and odp_a_pl and odp_b_pl and odp_c_pl:
            cur.execute('INSERT INTO tasks_advanced(id, correct_answer, media_url) VALUES (?, ?, ?)',
                        (task_id, poprawna_odp, media))

            cur.execute('INSERT INTO questions_advanced(task_id, lang, text, answer_a, answer_b, answer_c) VALUES (?, ?, ?, ?, ?, ?)',
                        (task_id, 'PL', pytanie_pl, odp_a_pl, odp_b_pl, odp_c_pl))

            if odp_a_en and odp_b_en and odp_c_en:
                cur.execute('INSERT INTO questions_advanced(task_id, lang, text, answer_a, answer_b, answer_c) VALUES (?, ?, ?, ?, ?, ?)',
                    (task_id, 'EN', pytanie_en, odp_a_en, odp_b_en, odp_c_en))

            if odp_a_de and odp_b_de and odp_c_de:
                cur.execute('INSERT INTO questions_advanced(task_id, lang, text, answer_a, answer_b, answer_c) VALUES (?, ?, ?, ?, ?, ?)',
                    (task_id, 'DE', pytanie_de, odp_a_de, odp_b_de, odp_c_de))

            if odp_a_ua and odp_b_ua and odp_c_ua:
                cur.execute('INSERT INTO questions_advanced(task_id, lang, text, answer_a, answer_b, answer_c) VALUES (?, ?, ?, ?, ?, ?)',
                    (task_id, 'UA', pytanie_ua, odp_a_ua, odp_b_ua, odp_c_ua))

        for category in categories:
            cur.execute('INSERT INTO categories(name, task_id) VALUES (?, ?)',
                        (category, task_id))

        ## COMMENT OUT BELOW IF POINT VALUES ARE NOT IN YOUR CSV
        if numer and pytanie_pl and poprawna_odp and kategorie:
            cur.execute('UPDATE tasks SET weight=? WHERE id=?', (pkt, numer))
            cur.execute('UPDATE tasks_advanced SET weight=? WHERE id=?', (pkt, numer))
        ## COMMENT OUT ABOVE IF POINT VALUES ARE NOT IN YOUR CSV


## UNCOMMENT BELOW IF POINT VALUES ARE IN ANOTHER CSV
# with open('punkty.csv') as file:
#     reader = csv.reader(file, delimiter=';')
#     next(reader)
#     for row in tqdm(reader):
#         numer = int(row[0])
#         punkty = int(row[1])
#         cur.execute('UPDATE tasks SET weight=? WHERE id=?', (punkty, numer))
#         cur.execute('UPDATE tasks_advanced SET weight=? WHERE id=?', (punkty, numer))
## UNCOMMENT BELOW IF POINT VALUES ARE IN ANOTHER CSV


cur.execute('SELECT COUNT(id) FROM tasks')
print(f'Imported {cur.fetchone()[0]} tasks')
cur.execute('SELECT COUNT(*) FROM questions')
print(f'Imported {cur.fetchone()[0]} questions')
cur.execute('SELECT COUNT(id) FROM tasks_advanced')
print(f'Imported {cur.fetchone()[0]} tasks_advanced')
cur.execute('SELECT COUNT(*) FROM questions_advanced')
print(f'Imported {cur.fetchone()[0]} questions_advanced')
cur.execute('SELECT COUNT(*) FROM categories')
print(f'Imported {cur.fetchone()[0]} categories')

conn.commit()
cur.close()
conn.close()
