from databases.no_sql_models.validations import DomainViaUser
from databases import MongoQueryIdentity, Event2Employee, OccupantTypes


def create_occupant_types_defaults():
    """
    occupant_category = mapped_column(String, server_default="")
    occupant_category_type = mapped_column(String, server_default="")
    occupant_is_unique = mapped_column(Boolean, server_default="0")
    """
    list_occupant_types = [
        {
            "occupant_type": "Toplantı Başkanı",
            "occupant_description": "Toplantı Başkanı",
            "occupant_code": "MT-PRS",
            "occupant_category": "Toplantı",
            "occupant_category_type": "MT",
            "occupant_is_unique": True,
        },
        {
            "occupant_type": "Toplantı Katip",
            "occupant_description": "Toplantıda tutanak tutan kişi",
            "occupant_code": "MT-WRT",
            "occupant_category": "Toplantı",
            "occupant_category_type": "MT",
            "occupant_is_unique": True,
        },
        {
            "occupant_type": "Toplantı Katılımcısı",
            "occupant_description": "Toplantıda sadece katılan kişi",
            "occupant_code": "MT-ATT",
            "occupant_category": "Toplantı",
            "occupant_category_type": "MT",
            "occupant_is_unique": False,
        },
        {
            "occupant_type": "Toplantı Danışman",
            "occupant_description": "Toplantıda danışmanlık yapan kişi",
            "occupant_code": "MT-ADV",
            "occupant_category": "Toplantı",
            "occupant_category_type": "MT",
            "occupant_is_unique": False,
        },
        {
            "occupant_type": "Toplantı Seçilmiş Başkanı",
            "occupant_description": "Toplantı Seçilmiş Başkanı",
            "occupant_code": "MT-VPR",
            "occupant_category": "Toplantı",
            "occupant_category_type": "MT",
            "occupant_is_unique": True,
        },
        {
            "occupant_type": "Daire Sahibi",
            "occupant_description": "Daire Sahibi",
            "occupant_code": "FL-OWN",
            "occupant_category": "Daire",
            "occupant_category_type": "FL",
            "occupant_is_unique": True,
        },
        {
            "occupant_type": "Daire Kiracısı",
            "occupant_description": "Daire Kiracısı",
            "occupant_code": "FL-TEN",
            "occupant_category": "Daire",
            "occupant_category_type": "FL",
            "occupant_is_unique": True,
        },
        {
            "occupant_type": "Daire Sakini",
            "occupant_description": "Daire Sakini",
            "occupant_code": "FL-RES",
            "occupant_category": "Daire",
            "occupant_category_type": "FL",
            "occupant_is_unique": False,
        },
        {
            "occupant_type": "Daire Sakini Vekili",
            "occupant_description": "Daire Sakini Vekili",
            "occupant_code": "FL-REP",
            "occupant_category": "Daire",
            "occupant_category_type": "FL",
            "occupant_is_unique": False,
        },
        {
            "occupant_type": "Bina Avukatı",
            "occupant_description": "Bina Avukatı",
            "occupant_code": "BU-ATT",
            "occupant_category": "Bina",
            "occupant_category_type": "BU",
            "occupant_is_unique": False,
        },
        {
            "occupant_type": "Bina Avukatı Yardımcısı",
            "occupant_description": "Bina Avukatı Yardımcısı",
            "occupant_code": "BU-ATA",
            "occupant_category": "Bina",
            "occupant_category_type": "BU",
            "occupant_is_unique": False,
        },
        {
            "occupant_type": "Bina Denetmen Yardımcısı",
            "occupant_description": "Bina Denetmen Yardımcısı",
            "occupant_code": "BU-SPA",
            "occupant_category": "Bina",
            "occupant_category_type": "BU",
            "occupant_is_unique": False,
        },
        {
            "occupant_type": "Bina Denetmeni",
            "occupant_description": "Bina Denetmeni",
            "occupant_code": "BU-SPV",
            "occupant_category": "Bina",
            "occupant_category_type": "BU",
            "occupant_is_unique": False,
        },
        {
            "occupant_type": "Bina Yönetici Yardımcısı",
            "occupant_description": "Bina Yönetici Yardımcısı",
            "occupant_code": "BU-MNA",
            "occupant_category": "Bina",
            "occupant_category_type": "BU",
            "occupant_is_unique": False,
        },
        {
            "occupant_type": "Bina Yöneticisi",
            "occupant_description": "Bina Yöneticisi",
            "occupant_code": "BU-MNG",
            "occupant_category": "Bina",
            "occupant_category_type": "BU",
            "occupant_is_unique": True,
        },
        {
            "occupant_type": "Bina Muhasabecisi",
            "occupant_description": "Bina Muhasabecisi",
            "occupant_code": "BU-ACC",
            "occupant_category": "Bina",
            "occupant_category_type": "BU",
            "occupant_is_unique": False,
        },
        {
            "occupant_type": "Proje Lideri",
            "occupant_description": "Proje Lideri",
            "occupant_code": "PRJ-LDR",
            "occupant_category": "Proje",
            "occupant_category_type": "PRJ",
            "occupant_is_unique": False,
        },
        {
            "occupant_type": "Proje Sorumlusu",
            "occupant_description": "Proje Sorumlusu",
            "occupant_code": "PRJ-RES",
            "occupant_category": "Proje",
            "occupant_category_type": "PRJ",
            "occupant_is_unique": False,
        },
        {
            "occupant_type": "Proje Ekibi",
            "occupant_description": "Proje Ekibi",
            "occupant_code": "PRJ-EMP",
            "occupant_category": "Proje",
            "occupant_category_type": "PRJ",
            "occupant_is_unique": False,
        },
        {
            "occupant_type": "Proje Finans Sorumlusu",
            "occupant_description": "Proje Finans Sorumlusu",
            "occupant_code": "PRJ-FIN",
            "occupant_category": "Proje",
            "occupant_category_type": "PRJ",
            "occupant_is_unique": False,
        },
        {
            "occupant_type": "Proje Teknik Sorumlusu",
            "occupant_description": "Proje Teknik Sorumlusu",
            "occupant_code": "PRJ-TEC",
            "occupant_category": "Proje",
            "occupant_category_type": "PRJ",
            "occupant_is_unique": False,
        },
        {
            "occupant_type": "Daire Mülkiyet Vekili",
            "occupant_description": "Daire Mülkiyet Vekili",
            "occupant_code": "FL-DEP",  # deputy
            "occupant_category": "Daire",
            "occupant_category_type": "FL",
            "occupant_is_unique": False,
        },
        {
            "occupant_type": "Bina Teknik Sorumlusu",
            "occupant_description": "Bina Teknik Sorumlusu",
            "occupant_code": "BU-TEC",
            "occupant_category": "Bina",
            "occupant_category_type": "BU",
            "occupant_is_unique": False,
        },
        {
            "occupant_type": "Bina Teknik Elemanı",
            "occupant_description": "Bina Teknik Elemanı",
            "occupant_code": "BU-EMP",
            "occupant_category": "Bina",
            "occupant_category_type": "BU",
            "occupant_is_unique": False,
        },
        {
            "occupant_type": "Bina Teknik Freelancer",
            "occupant_description": "Bina Teknik Freelancer",
            "occupant_code": "BU-FLC",
            "occupant_category": "Bina",
            "occupant_category_type": "BU",
            "occupant_is_unique": False,
        },
    ]
    for list_occupant_type in list_occupant_types:
        try:
            created_type = OccupantTypes.find_or_create(**list_occupant_type)
            created_type.save_and_confirm()
        except Exception as e:
            print(f"Error: {e}")


def create_application_defaults():

    from databases import (
        Companies,
        Departments,
        Duty,
        Duties,
        Employees,
        People,
        Users,
        Staff,
        RelationshipDutyCompany,
    )

    created_list = []
    created_by, confirmed_by = "System", "System"
    company_management = Companies.find_or_create(
        **{
            "formal_name": "Evyos LTD",
            "public_name": "Evyos Verimlilik Sistemleri",
            "company_type": "LTD",
            "commercial_type": "Commercial",
            "tax_no": "123132123132",
            "company_tag": "Evyos",
            "default_lang_type": "TR",
            "default_money_type": "TL",
            "created_by": created_by,
            "confirmed_by": confirmed_by,
            "is_commercial": True,
        }
    )
    created_list.append(company_management)

    another_list = Duties.init_a_company_default_duties(
        company_id=company_management.id, company_uu_id=str(company_management.uu_id)
    )
    created_list.extend(another_list)
    bulk_duty = Duty.filter_by_one(
        system=True,
        duty_code="BULK",
    ).data

    it_dept = Departments.filter_by_one(
        system=True,
        department_name="IT Department",
        department_code="ITD001",
        company_id=company_management.id,
        company_uu_id=str(company_management.uu_id),
    ).data

    created_duty = Duty.find_or_create(
        **dict(
            duty_name="Database Manager",
            duty_code="DM",
            duty_description="Database Manager",
            created_by=created_by,
            confirmed_by=confirmed_by,
            is_confirmed=True,
            active=True,
            deleted=False,
            is_notification_send=True,
        )
    )
    created_list.append(created_duty)

    created_duty = Duty.find_or_create(
        **dict(
            duty_name="Network Manager",
            duty_code="NM",
            duty_description="Network Manager",
            created_by=created_by,
            confirmed_by=confirmed_by,
            is_confirmed=True,
            active=True,
            deleted=False,
            is_notification_send=True,
        )
    )
    created_list.append(created_duty)

    application_manager_duty = Duty.find_or_create(
        duty_name="Application Manager",
        duty_code="AM",
        duty_description="Application Manager",
        created_by=created_by,
        confirmed_by=confirmed_by,
        is_confirmed=True,
        active=True,
        deleted=False,
        is_notification_send=True,
    )
    created_list.append(application_manager_duty)
    application_super_user_duty = Duty.find_or_create(
        duty_name="Super User",
        duty_code="SUE",
        duty_description="Super User",
        created_by=created_by,
        confirmed_by=confirmed_by,
        is_confirmed=True,
        active=True,
        deleted=False,
        is_notification_send=True,
    )
    created_list.append(application_super_user_duty)
    application_manager_duties = Duties.find_or_create(
        department_id=it_dept.id,
        department_uu_id=str(it_dept.uu_id),
        duties_id=application_manager_duty.id,
        duties_uu_id=str(application_manager_duty.uu_id),
        company_id=company_management.id,
        company_uu_id=str(company_management.uu_id),
        is_confirmed=True,
        active=True,
        deleted=False,
        is_notification_send=True,
    )
    created_list.append(application_manager_duties)
    super_user_duties = Duties.find_or_create(
        department_id=it_dept.id,
        department_uu_id=str(it_dept.uu_id),
        duties_id=application_super_user_duty.id,
        duties_uu_id=str(application_manager_duty.uu_id),
        company_id=company_management.id,
        company_uu_id=str(company_management.uu_id),
        is_confirmed=True,
        active=True,
        deleted=False,
        is_notification_send=True,
    )
    created_list.append(super_user_duties)
    RelationshipDutyCompany.find_or_create(
        duties_id=application_manager_duties.id,
        owner_id=company_management.id,
        member_id=company_management.id,
        parent_id=None,
        child_count=0,
        is_confirmed=True,
        active=True,
        deleted=False,
        is_notification_send=True,
    )
    created_list.append(application_manager_duties)
    RelationshipDutyCompany.find_or_create(
        duties_id=super_user_duties.id,
        owner_id=company_management.id,
        member_id=company_management.id,
        parent_id=None,
        child_count=0,
        is_confirmed=True,
        active=True,
        deleted=False,
        is_notification_send=True,
    )
    created_list.append(super_user_duties)

    app_manager = People.find_or_create(
        **{
            "firstname": "Berkay Application Manager",
            "surname": "Karatay",
            "sex_code": "M",
            "middle_name": "",
            "father_name": "Father",
            "mother_name": "Mother",
            "country_code": "TR",
            "national_identity_id": "12312312312",
            "birth_place": "Ankara",
            "birth_date": "01.07.1990",
            "tax_no": "1231231231",
            "created_by": created_by,
            "confirmed_by": confirmed_by,
        }
    )
    created_list.append(app_manager)
    sup_manager = People.find_or_create(
        **{
            "firstname": "Berkay Super User",
            "surname": "Karatay",
            "sex_code": "M",
            "middle_name": "",
            "father_name": "Father",
            "mother_name": "Mother",
            "country_code": "TR",
            "national_identity_id": "12312312313",
            "birth_place": "Ankara",
            "birth_date": "01.07.1990",
            "tax_no": "1231231232",
            "created_by": created_by,
            "confirmed_by": confirmed_by,
        }
    )
    created_list.append(sup_manager)
    application_manager_staff = Staff.find_or_create(
        staff_description="Application Manager",
        staff_name="Application Manager Employee",
        staff_code="AME",
        duties_id=application_manager_duties.id,
        duties_uu_id=str(application_manager_duty.uu_id),
        is_confirmed=True,
        active=True,
        deleted=False,
        is_notification_send=True,
    )
    created_list.append(application_manager_staff)
    super_user_staff = Staff.find_or_create(
        staff_description="Super User",
        staff_name="Super User Employee",
        staff_code="SUE",
        duties_id=super_user_duties.id,
        duties_uu_id=str(application_manager_duty.uu_id),
        is_confirmed=True,
        active=True,
        deleted=False,
        is_notification_send=True,
    )
    created_list.append(super_user_staff)
    app_manager_employee = Employees.find_or_create(
        staff_id=application_manager_staff.id,
        staff_uu_id=str(application_manager_staff.uu_id),
        people_id=app_manager.id,
        people_uu_id=str(app_manager.uu_id),
        is_confirmed=True,
        active=True,
        deleted=False,
        is_notification_send=True,
    )
    created_list.append(app_manager_employee)

    super_user_employee = Employees.find_or_create(
        staff_id=super_user_staff.id,
        staff_uu_id=str(super_user_staff.uu_id),
        people_id=sup_manager.id,
        people_uu_id=str(sup_manager.uu_id),
        is_confirmed=True,
        active=True,
        deleted=False,
        is_notification_send=True,
    )
    created_list.append(super_user_employee)

    app_manager_user = Users.find_or_create(
        person_id=app_manager.id,
        person_uu_id=str(app_manager.uu_id),
        user_tag=app_manager.person_tag,
        email="karatay.berkay.man@evyos.com.tr",
        phone_number="+901111111111",
        avatar="https://s.tmimgcdn.com/scr/800x500/276800/building-home-nature-logo-vector-template-3_276851-original.jpg",
        created_by=created_by,
        confirmed_by=confirmed_by,
        related_company=str(company_management.uu_id),
        is_confirmed=True,
        active=True,
        deleted=False,
        is_notification_send=True,
    )
    created_list.append(app_manager_user)
    app_manager_user.reset_password_token(found_user=app_manager_user)
    mongo_engine = MongoQueryIdentity(company_uuid=company_management.uu_id)
    mongo_engine.create_domain_via_user(
        payload=DomainViaUser(
            user_uu_id=str(app_manager_user.uu_id),
            main_domain="evyos.com.tr",
            other_domains_list=["evyos.com.tr"],
        )
    )

    sup_manager_employee = Users.find_or_create(
        person_id=sup_manager.id,
        person_uu_id=str(sup_manager.uu_id),
        user_tag=sup_manager.person_tag,
        email="karatay.berkay.sup@evyos.com.tr",
        phone_number="+901111111112",
        avatar="https://s.tmimgcdn.com/scr/800x500/276800/building-home-nature-logo-vector-template-3_276851-original.jpg",
        created_by=created_by,
        confirmed_by=confirmed_by,
        related_company=str(company_management.uu_id),
        is_confirmed=True,
        active=True,
        deleted=False,
        is_notification_send=True,
    )
    created_list.append(sup_manager_employee)

    sup_manager_employee.reset_password_token(found_user=sup_manager_employee)
    mongo_engine.create_domain_via_user(
        payload=DomainViaUser(
            user_uu_id=str(sup_manager_employee.uu_id),
            main_domain="evyos.com.tr",
            other_domains_list=["evyos.com.tr"],
        )
    )
    People.save()
    for created_list_item in created_list:
        created_list_item.save_and_confirm()
    print("All Defaults Create is now completed")
