from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import Group
from environs import Env

env = Env()
env.read_env()

from vbv_lernwelt.core.constants import (
    ADMIN_USER_ID,
    TEST_STUDENT1_USER_ID,
    TEST_STUDENT2_USER_ID,
    TEST_TRAINER1_USER_ID,
)
from vbv_lernwelt.core.models import User

default_users = [
    {
        "email": "student",
        "first_name": "Student",
        "last_name": "Meier",
        "avatar_url": "/static/avatars/avatar_iterativ.png",
    },
    {
        "email": "daniel.egger@iterativ.ch",
        "first_name": "Daniel",
        "last_name": "Egger",
        "avatar_url": "/static/avatars/avatar_iterativ.png",
    },
    {
        "email": "axel.manderbach@lernetz.ch",
        "first_name": "Axel",
        "last_name": "Manderbach",
        "avatar_url": "/static/avatars/avatar_axel.jpg",
    },
    {
        "email": "christoph.bosshard@vbv-afa.ch",
        "first_name": "Christoph",
        "last_name": "Bosshard",
        "avatar_url": "/static/avatars/avatar_christoph.png",
        "password": "myvbv1234",
    },
    {
        "email": "alexandra.vangelista@lernetz.ch",
        "first_name": "Alexandra",
        "last_name": "Vangelista",
        "avatar_url": "/static/avatars/avatar_alexandra.png",
        "password": "myvbv1234",
    },
    {
        "email": "chantal.rosenberg@vbv-afa.ch",
        "first_name": "Chantal",
        "last_name": "Rosenberg",
        "avatar_url": "/static/avatars/avatar_chantal.png",
        "password": "myvbv1234",
    },
    {
        "email": "bianca.muster@eiger-versicherungen.ch",
        "first_name": "Bianca",
        "last_name": "Muster",
        "avatar_url": "/static/avatars/avatar_bianca.png",
        "password": "myvbv1234",
    },
]


def create_default_users(user_model=User, group_model=Group, default_password=None):
    if default_password is None:
        default_password = "test"

    admin_group, created = group_model.objects.get_or_create(name="admin_group")
    _content_creator_grop, _created = group_model.objects.get_or_create(
        name="content_creator_grop"
    )
    student_group, created = group_model.objects.get_or_create(name="student_group")

    def _create_student_user(
        email,
        first_name,
        last_name,
        avatar_url="",
        password=default_password,
        language="de",
        id=None,
    ):
        student_user, created = _get_or_create_user(
            user_model=user_model,
            username=email,
            password=password,
            language=language,
            id=id,
        )
        student_user.first_name = first_name
        student_user.last_name = last_name
        student_user.avatar_url = avatar_url
        student_user.groups.add(student_group)
        student_user.save()

    def _create_admin_user(
        email, first_name, last_name, avatar_url="", id=None, password=default_password
    ):
        admin_user, created = _get_or_create_user(
            user_model=user_model, username=email, password=password, id=id
        )
        admin_user.is_superuser = True
        admin_user.is_staff = True
        admin_user.first_name = first_name
        admin_user.last_name = last_name
        admin_user.avatar_url = avatar_url
        admin_user.groups.add(admin_group)
        admin_user.save()

    _create_admin_user(
        email="info@iterativ.ch",
        first_name="Info",
        last_name="Iterativ",
        avatar_url="/static/avatars/avatar_iterativ.png",
        password=env("IT_DEFAULT_ADMIN_PASSWORD", default_password),
    )

    _create_admin_user(
        email="admin",
        first_name="Peter",
        last_name="Adminson",
        avatar_url="/static/avatars/avatar_iterativ.png",
        id=ADMIN_USER_ID,
        password=env("IT_DEFAULT_ADMIN_PASSWORD", default_password),
    )

    for user_data in default_users:
        _create_student_user(**user_data)

    _create_student_user(
        email="expert-vv.expert1@eiger-versicherungen.ch",
        first_name="Heidi",
        last_name="Zurbuchen",
    )
    _create_student_user(
        email="expert-vv.expert2@eiger-versicherungen.ch",
        first_name="Christa",
        last_name="von Allmen",
        avatar_url="/static/avatars/uk1.patrizia.huggel.jpg",
    )
    _create_student_user(
        email="expert-vv.expert3@eiger-versicherungen.ch",
        first_name="Expert",
        last_name="Expert3",
    )
    _create_student_user(
        email="student-vv@eiger-versicherungen.ch",
        first_name="Student",
        last_name="VV",
    )
    _create_student_user(
        email="patrizia.huggel@eiger-versicherungen.ch",
        first_name="Patrizia",
        last_name="Huggel",
        avatar_url="/static/avatars/uk1.patrizia.huggel.jpg",
        password="myvbv1234",
    )
    _create_student_user(
        email="andreas.feuz@eiger-versicherungen.ch",
        first_name="Andreas",
        last_name="Feuz",
        avatar_url="/static/avatars/uk1.daniel.tanaka.jpg",
        password="myvbv1234",
    )
    _create_student_user(
        email="daniel.tanaka@eiger-versicherung.ch",
        first_name="Daniel",
        last_name="Tanaka",
        avatar_url="/static/avatars/uk1.daniel.tanaka.jpg",
    )
    _create_student_user(
        email="maria.spini@eiger-versicherung.ch",
        first_name="Maria",
        last_name="Spini",
        avatar_url="/static/avatars/uk1.maria.spini.jpg",
    )
    _create_student_user(
        email="christian.koller@eiger-versicherung.ch",
        first_name="Christian",
        last_name="Koller",
        avatar_url="/static/avatars/uk1.christian.koller.jpg",
    )
    _create_student_user(
        email="michael.meier@example.com",
        first_name="Michael",
        last_name="Meier",
        avatar_url="/static/avatars/uk1.michael.meier.jpg",
        password="myvbv1234",
    )
    _create_student_user(
        email="lina.egger@example.com",
        first_name="Lina",
        last_name="Egger",
        avatar_url="/static/avatars/uk1.lina.egger.jpg",
        password="myvbv1234",
    )
    _create_student_user(
        email="evelyn.schmid@example.com",
        first_name="Evelyn",
        last_name="Schmid",
        avatar_url="/static/avatars/uk1.evelyn.schmid.jpg",
    )

    _create_student_user(
        email="trainer-uk1.analyse@eiger-versicherungen.ch",
        first_name="Trainer",
        last_name="Analyse",
    )
    _create_student_user(
        email="trainer-uk1.einstieg@eiger-versicherungen.ch",
        first_name="Trainer",
        last_name="Einstieg",
    )
    _create_student_user(
        email="student-uk1-bern@eiger-versicherungen.ch",
        first_name="Student",
        last_name="UK1-Bern",
    )
    _create_student_user(
        email="student-uk1-zurich@eiger-versicherungen.ch",
        first_name="Student",
        last_name="UK1-Zürich",
    )
    _create_student_user(
        email="luca.dupont@assurance.ch",
        first_name="Luca",
        last_name="Dupont",
        avatar_url="/static/avatars/uk1.michael.meier.jpg",
        password="myafa1234",
        language="fr",
    )
    _create_student_user(
        email="patrick.muster@eiger-versicherungen.ch",
        first_name="Patrick",
        last_name="Muster",
        avatar_url="/static/avatars/uk1.michael.meier.jpg",
        password="myvbv1234",
        language="de",
    )
    _create_student_user(
        email="geraldine.kolly@assurance.ch",
        first_name="Géraldine",
        last_name="Kolly",
        avatar_url="/static/avatars/uk1.patrizia.huggel.jpg",
        password="myafa1234",
        language="fr",
    )

    # users for cypress tests
    _create_student_user(
        id=TEST_TRAINER1_USER_ID,
        email="test-trainer1@example.com",
        first_name="Test",
        last_name="Trainer1",
        avatar_url="/static/avatars/uk1.patrizia.huggel.jpg",
    )
    _create_student_user(
        id=TEST_STUDENT1_USER_ID,
        email="test-student1@example.com",
        first_name="Test",
        last_name="Student1",
        avatar_url="/static/avatars/uk1.michael.meier.jpg",
    )
    _create_student_user(
        id=TEST_STUDENT2_USER_ID,
        email="test-student2@example.com",
        first_name="Test",
        last_name="Student2",
        avatar_url="/static/avatars/uk1.lina.egger.jpg",
    )


def _get_or_create_user(user_model, *args, **kwargs):
    username = kwargs.get("username", None)
    password = kwargs.get("password", None)
    language = kwargs.get("language", "de")
    id = kwargs.get("id", None)
    created = False

    user = user_model.objects.filter(username=username).first()

    if not user:
        user = user_model.objects.create(
            username=username,
            password=make_password(password),
            email=username,
            language=language,
            id=id,
        )
        created = True
    return user, created
