import arrow

from Modules.Token.password_module import PasswordModule
from Controllers.Mongo.database import mongo_handler


def create_application_defaults(db_session):

    from Schemas 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",
            "is_commercial": True,
            "is_confirmed": True,
        },
        db=db_session,
    )
    created_list.append(company_management)
    active_row = dict(
        is_confirmed=True, active=True, deleted=False, is_notification_send=True
    )
    company_id, company_uu_id = company_management.id, str(company_management.uu_id)
    created_list = []
    execution = Departments.find_or_create(
        department_name="Execution Office",
        department_code="EO001",
        company_id=company_id,
        company_uu_id=str(company_uu_id),
        **active_row,
        db=db_session,
    )
    created_list.append(execution)
    it_dept = Departments.find_or_create(
        department_name="IT Department",
        department_code="ITD001",
        company_id=company_id,
        company_uu_id=str(company_uu_id),
        **active_row,
        db=db_session,
    )
    created_list.append(it_dept)
    bm_duty = Duty.find_or_create(
        duty_name="Business Manager",
        duty_code="BM0001",
        duty_description="Business Manager",
        **active_row,
        db=db_session,
    )
    created_list.append(bm_duty)
    it_duty = Duty.find_or_create(
        duty_name="IT Manager",
        duty_code="IT0001",
        duty_description="IT Manager",
        **active_row,
        db=db_session,
    )
    created_list.append(it_duty)
    bulk_duty = Duty.find_or_create(
        duty_name="BULK",
        duty_code="BULK",
        duty_description="BULK RECORDS OF THE COMPANY",
        **active_row,
        db=db_session,
    )
    created_list.append(bulk_duty)
    occu_duty = Duty.find_or_create(
        duty_name="OCCUPANT",
        duty_code="OCCUPANT",
        duty_description="OCCUPANT RECORDS OF THE COMPANY",
        **active_row,
        db=db_session,
    )
    created_list.append(occu_duty)
    duties_created_bm = Duties.find_or_create(
        company_id=company_id,
        company_uu_id=str(company_uu_id),
        duties_id=bm_duty.id,
        duties_uu_id=str(bm_duty.uu_id),
        department_id=execution.id,
        department_uu_id=str(execution.uu_id),
        **active_row,
        db=db_session,
    )
    created_list.append(duties_created_bm)
    duties_created_it = Duties.find_or_create(
        company_id=company_id,
        company_uu_id=str(company_uu_id),
        duties_id=it_duty.id,
        duties_uu_id=str(it_duty.uu_id),
        department_id=it_dept.id,
        department_uu_id=str(it_dept.uu_id),
        **active_row,
        db=db_session,
    )
    created_list.append(duties_created_it)
    duties_created__ex = Duties.find_or_create(
        company_id=company_id,
        company_uu_id=str(company_uu_id),
        duties_id=bulk_duty.id,
        duties_uu_id=str(bulk_duty.uu_id),
        department_id=execution.id,
        department_uu_id=str(execution.uu_id),
        **active_row,
        db=db_session,
    )
    created_list.append(duties_created__ex)
    duties_created_at = Duties.find_or_create(
        company_id=company_id,
        company_uu_id=str(company_uu_id),
        duties_id=occu_duty.id,
        duties_uu_id=str(occu_duty.uu_id),
        department_id=execution.id,
        department_uu_id=str(execution.uu_id),
        **active_row,
        db=db_session,
    )
    created_list.append(duties_created_at)
    bulk_duty = Duty.filter_by_one(system=True, duty_code="BULK", db=db_session).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),
        db=db_session,
    ).data

    created_duty = Duty.find_or_create(
        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,
        db=db_session,
    )
    created_list.append(created_duty)

    created_duty = Duty.find_or_create(
        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,
        db=db_session,
    )
    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,
        db=db_session,
    )
    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,
        db=db_session,
    )
    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,
        db=db_session,
    )
    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,
        db=db_session,
    )
    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,
        db=db_session,
    )
    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,
        db=db_session,
    )
    created_list.append(super_user_duties)

    app_manager = People.find_or_create(
        **{
            "person_tag": "BAM-System",
            "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",
            "is_confirmed": True,
        },
        db=db_session,
    )
    created_list.append(app_manager)
    sup_manager = People.find_or_create(
        **{
            "person_tag": "BSU-System",
            "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",
        },
        db=db_session,
    )
    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,
        db=db_session,
    )
    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,
        db=db_session,
    )
    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,
        db=db_session,
    )
    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,
        db=db_session,
    )
    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",
        related_company=str(company_management.uu_id),
        is_confirmed=True,
        active=True,
        deleted=False,
        is_notification_send=True,
        db=db_session,
    )
    created_list.append(app_manager_user)
    app_manager_user.password_expiry_begins = str(arrow.now())
    app_manager_user.password_token = PasswordModule.generate_refresher_token()

    main_domain, collection_name = (
        "evyos.com.tr",
        f"{str(company_management.uu_id)}*Domain",
    )
    with mongo_handler.collection(collection_name) as mongo_engine:
        mongo_engine.insert_one(
            document={
                "user_uu_id": str(app_manager_user.uu_id),
                "other_domains_list": [main_domain],
                "main_domain": main_domain,
                "modified_at": arrow.now().timestamp(),
            }
        )

    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,
        db=db_session,
    )
    created_list.append(sup_manager_employee)

    sup_manager_employee.password_expiry_begins = str(arrow.now())
    sup_manager_employee.password_token = PasswordModule.generate_refresher_token()
    with mongo_handler.collection(collection_name) as mongo_engine:
        mongo_engine.insert_one(
            document={
                "user_uu_id": str(sup_manager_employee.uu_id),
                "other_domains_list": [main_domain],
                "main_domain": main_domain,
                "modified_at": arrow.now().timestamp(),
            }
        )
    db_session.commit()

    print("All Defaults Create is now completed")
