# SPDX-FileCopyrightText: WTF Kooperative eG <https://wtf-eg.de/>
#
# SPDX-License-Identifier: AGPL-3.0-or-later

import csv
import logging

from app import app, db
from ki.models import Address, Contact, ContactType, Language, Skill, Profile, ProfileLanguage, ProfileSearchtopic, \
    ProfileSkill, User


def seed_contacttypes():
    contacttypes_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/contacttypes.csv"

    logging.info("importing contacttypes")

    with open(contacttypes_seed_file_path) as file:
        csv_reader = csv.DictReader(file)

        for contacttype in csv_reader:
            id = int(contacttype["id"])
            db_contacttype = ContactType.query.get(id)

            if db_contacttype is None:
                db.session.add(ContactType(id=int(contacttype["id"]), name=contacttype["name"]))


def seed(dev: bool):
    seed_contacttypes()

    skill_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/skills.csv"

    app.logger.info("importing skills")

    with open(skill_seed_file_path) as skills_file:
        skills_csv_reader = csv.DictReader(skills_file)

        for skill in skills_csv_reader:
            id = int(skill["id"])
            db_skill = Skill.query.get(id)

            if db_skill is None:
                db.session.add(Skill(id=int(skill["id"]), name=skill["name"]))

    app.logger.info("importing languages")

    iso_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/iso_639_1.csv"

    with open(iso_seed_file_path) as iso_file:
        iso_csv_reader = csv.DictReader(iso_file)

        for iso in iso_csv_reader:
            id = iso["639-1"]
            db_language = Language.query.get(id)

            if db_language is None:
                db.session.add(Language(id=iso["639-1"], name=iso["Sprache"]))

    if dev:
        app.logger.info("seeding peter :)")

        peter = User(auth_id="peter")
        db.session.add(peter)

        peters_profile = Profile(nickname="peternichtlustig",
                                 pronouns="Herr Dr. Dr.",
                                 volunteerwork="Gartenverein",
                                 availability="Immer",
                                 freetext="Ich mag Kaffee",
                                 user=peter)
        db.session.add(peters_profile)

        matrix_contact = Contact(profile=peters_profile, contacttype_id=4, content="@peter:wtf-eg.de")
        db.session.add(matrix_contact)

        email_contact = Contact(profile=peters_profile, contacttype_id=1, content="peter@wtf-eg.de")
        db.session.add(email_contact)

        peters_address = Address(name="Peter Nichtlustig",
                                 street="Waldweg",
                                 house_number="23i",
                                 additional="Hinterhaus",
                                 postcode="13337",
                                 city="Bielefeld",
                                 country="Deutschland",
                                 profile=peters_profile)
        db.session.add(peters_address)

        peters_python_skill = ProfileSkill(profile=peters_profile, skill_id=3, level=3)
        db.session.add(peters_python_skill)

        peters_php_skill = ProfileSkill(profile=peters_profile, skill_id=1, level=5)
        db.session.add(peters_php_skill)

        peters_python_searchtopic = ProfileSearchtopic(profile=peters_profile, skill_id=3)
        db.session.add(peters_python_searchtopic)

        peters_php_searchtopic = ProfileSearchtopic(profile=peters_profile, skill_id=1)
        db.session.add(peters_php_searchtopic)

        peter_de = ProfileLanguage(profile=peters_profile, language_id="de", level=5)
        db.session.add(peter_de)

        peter_fr = ProfileLanguage(profile=peters_profile, language_id="fr", level=3)
        db.session.add(peter_fr)

        app.logger.info("seeding klaus :D")

        klaus = User(auth_id="klaus")
        db.session.add(klaus)

        app.logger.info("seeding dieter \\o/")

        dieter = User(auth_id="dieter")
        db.session.add(dieter)

        dieters_profile = Profile(nickname="dirtydieter",
                                  pronouns="",
                                  volunteerwork="Müll sammeln",
                                  availability="Nur nachts",
                                  freetext="1001010010111!!!",
                                  visible=True,
                                  user=dieter)
        db.session.add(dieters_profile)

    db.session.commit()