import arrow
from api_modules.token.password_module import PasswordModule
from api_controllers.mongo.database import mongo_handler
from schemas import (
    Addresses,
    BuildLivingSpace,
    Users,
    People,
    Build,
    BuildParts,
    BuildTypes,
    ApiEnumDropdown,
    Companies,
    OccupantTypes,
)

def create_occupant_defaults(db_session):

    created_list = []
    Addresses.set_session(db_session)
    BuildLivingSpace.set_session(db_session)
    Users.set_session(db_session)
    People.set_session(db_session)
    Build.set_session(db_session)
    BuildParts.set_session(db_session)
    BuildTypes.set_session(db_session)
    ApiEnumDropdown.set_session(db_session)
    Companies.set_session(db_session)
    OccupantTypes.set_session(db_session)

    company_management = Companies.query.filter_by(formal_name = "Evyos LTD",).first()
    if not company_management:
        raise Exception("Company not found")

    company_id, company_uu_id = company_management.id, str(company_management.uu_id)
    active_row = dict(is_confirmed=True, active=True, deleted=False, is_notification_send=True)
    build_type = BuildTypes.query.filter_by(type_code = "APT").first()
    address = Addresses.query.filter_by(letter_address = "Example Address").first()

    created_build = Build.query.filter_by(build_name = "Build Example").first()
    if not created_build:
        created_build = Build.create(
            build_name="Build Example",
            build_code="B001",
            build_no="B001",
            build_date="01.07.1980",
            address_id=address.id,
            address_uu_id=str(address.uu_id),
            build_types_id=build_type.id,
            build_types_uu_id=str(build_type.uu_id),
            **active_row
        )
        created_list.append(created_build)

    build_type_created = BuildTypes.query.filter_by(type_code = "APT").first()
    build_type_flat = BuildTypes.query.filter_by(type_code = "DAIRE").first()
    enum_dropdown = ApiEnumDropdown.query.filter_by(key = "NE", enum_class = "Directions").first()

    occupant_type_prs = OccupantTypes.query.filter_by(occupant_code = "MT-PRS").first()
    occupant_type_owner = OccupantTypes.query.filter_by(occupant_code = "FL-OWN").first()
    occupant_type_tenant = OccupantTypes.query.filter_by(occupant_code = "FL-TEN").first()
    created_managment_room = BuildParts.query.filter_by(part_code = "MR001").first()

    if not created_managment_room:
        created_managment_room = BuildParts.create(
            address_gov_code="123123123123",
            build_id=created_build.id,
            build_uu_id=str(created_build.uu_id),
            part_code="MR001",
            part_net_size=100,
            part_no=0,
            part_level=0,
            part_type_id=build_type_created.id,
            part_type_uu_id=str(build_type_created.uu_id),
            part_direction_id=enum_dropdown.id,
            part_direction_uu_id=str(enum_dropdown.uu_id),
            human_livable=True,
            due_part_key="Example",
            **active_row,
        )
        created_list.append(created_managment_room)

    created_flat = BuildParts.query.filter_by(part_code = "MF001").first()
    if not created_flat:
        created_flat = BuildParts.create(
            address_gov_code="123123123124",
            build_id=created_build.id,
            build_uu_id=str(created_build.uu_id),
            part_code="MF001",
            part_net_size=100,
            part_no=1,
            part_level=1,
            part_type_id=build_type_flat.id,
            part_type_uu_id=str(build_type_flat.uu_id),
            part_direction_id=enum_dropdown.id,
            part_direction_uu_id=str(enum_dropdown.uu_id),
            human_livable=True,
            due_part_key="Example",
            **active_row,
        )
        created_list.append(created_flat)

    build_manager_people = People.query.filter_by(person_tag = "Build Manager Example").first()
    if not build_manager_people:
        build_manager_people = People.create(
        **{
            "person_tag": "Build Manager Example",
            "firstname": "Example Build Manager",
            "surname": "Example",
            "sex_code": "M",
            "middle_name": "",
            "father_name": "Father",
            "mother_name": "Mother",
            "country_code": "TR",
            "national_identity_id": "12312312315",
            "birth_place": "Ankara",
            "birth_date": "01.07.1990",
            "tax_no": "1231231234",
            }
        )
        created_list.append(build_manager_people)

    owner_people = People.query.filter_by(person_tag = "Owner Example").first()
    if not owner_people:
        owner_people = People.create(
        **{
            "person_tag": "Owner Example",
            "firstname": "Example Owner",
            "surname": "Example",
            "sex_code": "M",
            "middle_name": "",
            "father_name": "Father",
            "mother_name": "Mother",
            "country_code": "TR",
            "national_identity_id": "12312312316",
            "birth_place": "Ankara",
            "birth_date": "01.07.1990",
            "tax_no": "1231231234",
            }
        )
        created_list.append(owner_people)

    tenant_people = People.query.filter_by(person_tag = "Tenant Example").first()
    if not tenant_people:
        tenant_people = People.create(
        **{
            "person_tag": "Tenant Example",
            "firstname": "Example Tenant",
            "surname": "Example",
            "sex_code": "M",
            "middle_name": "",
            "father_name": "Father",
            "mother_name": "Mother",
            "country_code": "TR",
            "national_identity_id": "12312312317",
            "birth_place": "Ankara",
            "birth_date": "01.07.1990",
            "tax_no": "1231231234",
            }
        )
        created_list.append(tenant_people)

    main_domain, collection_name = "evyos.com.tr", f"{str(company_management.uu_id)}*Domain"
    user_build_manager = Users.query.filter_by(user_tag = "Build Manager Example").first()
    if not user_build_manager:
        user_build_manager = Users.create(
            person_id=build_manager_people.id,
            person_uu_id=str(build_manager_people.uu_id),
            user_tag=build_manager_people.person_tag,
            email="example.build.manager@gmail.com",
            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),
            **active_row,
        )
        created_list.append(user_build_manager)
        user_build_manager.password_expiry_begins = str(arrow.now())
        user_build_manager.password_token = PasswordModule.generate_refresher_token()

    user_owner = Users.query.filter_by(user_tag = "Owner Example").first()
    if not user_owner:
        user_owner = Users.create(
            person_id=owner_people.id,
            person_uu_id=str(owner_people.uu_id),
            user_tag=owner_people.person_tag,
            email="example.owner@gmail.com",
            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),
            **active_row,
        )
        created_list.append(user_owner)
        user_owner.password_expiry_begins = str(arrow.now())
        user_owner.password_token = PasswordModule.generate_refresher_token()

    user_tenant = Users.query.filter_by(user_tag = "Tenant Example").first()
    if not user_tenant:
        user_tenant = Users.create(
            person_id=tenant_people.id,
            person_uu_id=str(tenant_people.uu_id),
            user_tag=tenant_people.person_tag,
            email="example.tenant@gmail.com",
            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),
            **active_row,
        )
        created_list.append(user_tenant)
        user_tenant.password_expiry_begins = str(arrow.now())
        user_tenant.password_token = PasswordModule.generate_refresher_token()

    with mongo_handler.collection(collection_name) as mongo_engine:
        existing_record = mongo_engine.find_one({"user_uu_id": str(user_build_manager.uu_id)})
        if not existing_record:
            mongo_engine.insert_one(
                document={"user_uu_id": str(user_build_manager.uu_id), "other_domains_list": [main_domain], "main_domain": main_domain, "modified_at": arrow.now().timestamp()}
            )
        else:
            mongo_engine.update_one(
                {"user_uu_id": str(user_build_manager.uu_id)},
                {"$set": {"other_domains_list": [main_domain], "main_domain": main_domain, "modified_at": arrow.now().timestamp()}}
            )

    with mongo_handler.collection(collection_name) as mongo_engine:
        existing_record = mongo_engine.find_one({"user_uu_id": str(user_owner.uu_id)})
        if not existing_record:
            mongo_engine.insert_one(
                document={"user_uu_id": str(user_owner.uu_id), "other_domains_list": [main_domain], "main_domain": main_domain, "modified_at": arrow.now().timestamp()}
            )
        else:
            mongo_engine.update_one(
                {"user_uu_id": str(user_owner.uu_id)},
                {"$set": {"other_domains_list": [main_domain], "main_domain": main_domain, "modified_at": arrow.now().timestamp()}}
            )

    with mongo_handler.collection(collection_name) as mongo_engine:
        existing_record = mongo_engine.find_one({"user_uu_id": str(user_tenant.uu_id)})
        if not existing_record:
            mongo_engine.insert_one(
                document={"user_uu_id": str(user_tenant.uu_id), "other_domains_list": [main_domain], "main_domain": main_domain, "modified_at": arrow.now().timestamp()}
            )
        else:
            mongo_engine.update_one(
                {"user_uu_id": str(user_tenant.uu_id)},
                {"$set": {"other_domains_list": [main_domain], "main_domain": main_domain, "modified_at": arrow.now().timestamp()}})
    
    created_build_living_space_prs = BuildLivingSpace.query.filter_by(
        build_id=created_build.id, build_parts_id=created_managment_room.id, person_id=build_manager_people.id
    ).first()
    if not created_build_living_space_prs:
        created_build_living_space_prs = BuildLivingSpace.create(
            build_id=created_build.id,
            build_uu_id=str(created_build.uu_id),
            build_parts_id=created_managment_room.id,
            build_parts_uu_id=str(created_managment_room.uu_id),
            person_id=build_manager_people.id,
            person_uu_id=str(build_manager_people.uu_id),
            occupant_type_id=occupant_type_prs.id,
            occupant_type_uu_id=str(occupant_type_prs.uu_id),
            **active_row,
        )
        created_list.append(created_build_living_space_prs)

    created_build_living_space_owner = BuildLivingSpace.query.filter_by(
        build_id=created_build.id, build_parts_id=created_flat.id, person_id=owner_people.id
    ).first()
    if not created_build_living_space_owner:
        created_build_living_space_owner = BuildLivingSpace.create(
            build_id=created_build.id,
            build_uu_id=str(created_build.uu_id),
            build_parts_id=created_flat.id,
            build_parts_uu_id=str(created_flat.uu_id),
            person_id=owner_people.id,
            person_uu_id=str(owner_people.uu_id),
            occupant_type_id=occupant_type_owner.id,
            occupant_type_uu_id=str(occupant_type_owner.uu_id),
            **active_row,
        )
        created_list.append(created_build_living_space_owner)

    created_build_living_space_tenant = BuildLivingSpace.query.filter_by(
        build_id=created_build.id, build_parts_id=created_flat.id, person_id=tenant_people.id
    ).first()
    if not created_build_living_space_tenant:
        created_build_living_space_tenant = BuildLivingSpace.create(
            build_id=created_build.id,
            build_uu_id=str(created_build.uu_id),
            build_parts_id=created_flat.id,
            build_parts_uu_id=str(created_flat.uu_id),
            person_id=tenant_people.id,
            person_uu_id=str(tenant_people.uu_id),
            occupant_type_id=occupant_type_tenant.id,
            occupant_type_uu_id=str(occupant_type_tenant.uu_id),
            **active_row,
        )
        created_list.append(created_build_living_space_tenant)

    db_session.commit()
    print("Occupant Defaults Create is now completed")
