import arrow

from service_app_test.bases import RequestToApi
from service_app_test.api_configs import LocalAPI, BothAPIS
from service_app_test.test_application.evyos.decision_book import (
    run_decision_book_depends, list_decision_books,
)
from service_app_test.test_application.evyos.decision_book_items import (
    send_invitation_to_building_residents,
    collect_invitation_to_building_residents,
    assign_people_to_pre_or_wrt, run_decision_book_items,
)
from service_app_test.test_application.migrate_old_data.get_type_codes import get_type_codes_key_and_class
from service_app_test.test_application.migrate_old_data.people import migrate_people
from service_app_test.test_application.migrate_old_data.building import (
    migrate_build,
    migrate_build_living_space,
)
from service_app_test.test_application.migrate_old_data.company import migrate_company
from service_app_test.test_application.migrate_old_data.accounts import (
    migrate_account_records,
)


password_token = "g17Anw5yBvrASMXtES8olfE8vN32R-U7wO_g8kbhjK8C67ww7fv39Mp3ygjouDw8UrwFJhemqFwbEIi2XzIN1u_rq4nmUSROZAnL3NyhczfizMZml0Rd84HWFA80Z1WpIsOadRlGVN77rr3OrrfYXfp1Fa6f_LsiTj0m3N-ie6Mr-4NIQ-3g25Z4oCD4Up6o"
login_data = {
    "domain": "evyos.com.tr",
    "access_key": "karatay.berkay.sup@evyos.com.tr",
    "password": "string",
    "remember_me": False,
    "password_token": password_token,
}
login_creds_employee = {
    "domain": "evyos.com.tr",
    "access_key": "karatay.berkay.sup@evyos.com.tr",
    "password": "string",
    "remember_me": False,
    "password_token": password_token,
}
access_key_president = "mehmet.karatay@hotmail.com"
login_creds_occupant = {
    "domain": "evyos.com.tr",
    "access_key": access_key_president,
    "password": "string",
    "remember_me": False,
    "password_token": "88ENxfw1a0tQRHTQ1RWPsv1iWEe2jzwPqra1tH5PuGmr69fneVDdCpQhgj5LnY0L9IA2Zq8eUX7LRpwKaez0rbYQZRdrKNCfx98M4erSNSRCFPI35tBvGdRuTEISIGoUXPI1xEcfQvGT-AOILc-yPOlyfYLyB7Chej1bjuGxe04hdtqQon2rYLmI8KMCv2XK"
}
wrt_creds_occupant = {
    "domain": "evyos.com.tr",
    "access_key": access_key_president,
    "password": "string",
    "remember_me": False,
    "password_token": "",
}

# wag_api = RequestToApi()
# wag_api.overwrite_base_url(base_url=WagAPI.base_url)
# wag_api.selected_object = wag_api.login_via_email_and_password(
#     login_data=login_data_wag, is_password_valid=True
# )

local_api = RequestToApi()
local_api.overwrite_base_url(base_url=LocalAPI.base_url)


both_apis = BothAPIS()
# both_apis.wag_api = wag_api
both_apis.local_api = local_api

assign_people_to_create_item = 3

# selection_list = None
selection_list = [
    "68b5e3d9-c546-4ebd-98f5-df468f20cc54",
    "da2a282c-7053-4cdf-bca5-c41f548883cc",
]
# selection_list = None
manager_token = "jk-8A2kXjk86QGP2WHMrOF7E9UTfzOe0Wcq0HDZFDh78V5YbuNVxmw"
writers_token = manager_token
people_uu_id_list = [
    "561a9cbc-587a-4582-8eff-f005f5b1e847",
    "561a9cbc-587a-4582-8eff-f005f5b1e847",
    "561a9cbc-587a-4582-8eff-f005f5b1e847",
]

list_of_attendees = [
"zFhY5z05ixUxvryeGYPdchUDYhIoatghz06bb74FwJuMyrH1w4JVQw",
"wlm0VNMnUDj91B8HLWQE9vKZsOIPcYQlHZqmgV_U_ywIOXFxR7RDxw",
"2ckn8Jp7r0xgHqdExk82SQVUhekwSMNI6Yvd1ESV6LD4769kyeso2g",
"6iY_p1JkptCj1JeptEUokw5BzxtcgQ3tHYkF2Vud9Cl1uzO3TJZmnQ",
"jaOQodhZaDcEJ8_1Y2hjOlKk3f5SiaYfYU-xqDlervLdeYgHumkniw",
"2iPk5lcTYdl_-S7odsLbN8Kij20pVNHmGSDC6gIJHIIRQ74JitP_LA",
"cl82VDi7xQNO77v3CC7X8wcQ4i_C-K9uuKZyqnvh4KHi0ciwqXyOKA",
"UTHZNg9gx0lZ4laDUz7S5xQ5e83vxrufqp7PbPCJx29AxhzX9DL62A",
"rwlasXNiQXXxfRBXv78DkQeSS1S3ubV0ulrXK8cZgmCDkHAIoWvU0w",
"JgAVc5U5lsfrbSx1gx0_SYQcO0q_K2HX8Vir5SIdiM-Z-PuHAG5cRg",
"V_rSldMaE9P_-uEfSsyh1ojEYep4Xma3NY7BIKzuicj_wEkCqFramw",
]
living_space_data = dict(
    build_parts_uu_id="68b5e3d9-c546-4ebd-98f5-df468f20cc54",
    person_uu_id="dc135536-086f-4e11-af3a-02c7cf65f265",
    occupant_type_uu_id="a47372a9-67da-4dfb-82a9-51f9ca15ef63",
    expiry_starts=arrow.now().date().__str__(),
)


if assign_people_to_create_item == -1:
    local_api.selected_object = local_api.login_via_email_and_password(
        login_data=login_data, is_password_valid=False
    )
    migrate_company(requester=both_apis)
    migrate_people(requester=both_apis)
    migrate_build(requester=both_apis)
    migrate_build_living_space(requester=both_apis)
    # migrate_account_records(requester=both_apis)


if assign_people_to_create_item == 0:
    if not bool(selection_list):
        local_api.selected_object = local_api.login_via_email_and_password(
            login_data=login_data, is_password_valid=True
        )
        response = local_api.post(endpoint="/building/living_space/create", data=living_space_data)
        print('response', response.text)

    local_api.selected_object = local_api.login_via_email_and_password(
        login_data=login_creds_occupant,
        is_password_valid=bool(selection_list),
        selection_list=selection_list,
    )
    selected_decision_book_uu_id = run_decision_book_depends(
        requester=both_apis.local_api
    )
    send_invitation = {
        "build_decision_book_uu_id": selected_decision_book_uu_id,
        "message": "Request for a Regular Building Meeting",
        "planned_date": arrow.now().shift(days=2).date().__str__(),
    }
    send_invitation_to_building_residents(
        send_invitation_dict=send_invitation, requester=both_apis.local_api
    )
elif assign_people_to_create_item == 1:
    local_api.selected_object = local_api.login_via_email_and_password(
        login_data=login_creds_occupant,
        is_password_valid=True,
        selection_list=selection_list,
    )
    count = 7
    list_of_attendees.insert(0, manager_token)
    collect_invitation_to_building_residents(
        attended_dict_list=list_of_attendees,
        attend_count=count + 1,
        requester=both_apis.local_api,
    )
elif assign_people_to_create_item == 2:
    local_api.selected_object = local_api.login_via_email_and_password(
        login_data=login_creds_occupant,
        is_password_valid=True,
        selection_list=selection_list,
    )
    assign_people_to_pre_or_wrt(
        person_uu_id=people_uu_id_list[0],
        manager_token=manager_token,
        occupant_code="MT-VPR",
        requester=both_apis.local_api,
    )
    assign_people_to_pre_or_wrt(
        person_uu_id=people_uu_id_list[1],
        manager_token=manager_token,
        occupant_code="MT-WRT",
        requester=both_apis.local_api,
    )
    assign_people_to_pre_or_wrt(
        person_uu_id=people_uu_id_list[2],
        manager_token=manager_token,
        occupant_code="MT-PRS",
        requester=both_apis.local_api,
    )
elif assign_people_to_create_item == 3:
    local_api.selected_object = local_api.login_via_email_and_password(
        login_data=wrt_creds_occupant,
        is_password_valid=True,
        selection_list=selection_list,
    )

    selected_decision_book_uu_id = list_decision_books(requester=both_apis.local_api)["data"][0]["uu_id"]
    print("select_company_uu_id", local_api.selected_object)
    is_fixed_price = False

    start_date, end_date = "2024-11-01", "2024-12-01"

    info_type_reno_uu_id = get_type_codes_key_and_class(
        class_name="BuildDuesTypes", key_name="BDT-R", requester=both_apis.local_api
    )["data"]["uu_id"]
    info_type_deb_uu_id = get_type_codes_key_and_class(
        class_name="BuildDuesTypes", key_name="BDT-D", requester=both_apis.local_api
    )["data"]["uu_id"]
    info_type_inf_uu_id = get_type_codes_key_and_class(
        class_name="BuildDuesTypes", key_name="BDT-I", requester=both_apis.local_api
    )["data"]["uu_id"]

    run_decision_book_items(
        writers_token=writers_token,
        info_type_uu_id=info_type_reno_uu_id,
        item_comment="En son toplantıda bahsedilen 2 nolu daireyi etkiliyen çözüm için kanalizasyon ve "
                     "tamirleri için fiyat alınarak arka alan yağmur suyu direnajının yaplımasının projelendirilmesi.",
        start_date=start_date,
        end_date=end_date,
        requester=both_apis.local_api
    )

    unit_price = 17.04
    run_decision_book_items(
        writers_token=writers_token,
        info_type_uu_id=info_type_deb_uu_id,
        item_comment="Aidat gündemi için 1,2,3 nolu daireler için 1400 TL, diğer daireler için 1500 TL "
                     "olmasına karar verildi.",
        unit_price=unit_price,
        requester=both_apis.local_api
    )

    run_decision_book_items(
        writers_token=writers_token,
        info_type_uu_id=info_type_reno_uu_id,
        item_comment="Hali hazırda yöneticilik görevini ifa eden 4 nolu daire sahibi Mehmet Karatayın bu "
                     "görevinin devam etmesine ve apartmanın bank hesaplarının kullanımı ile ilgili yetkisinin devam etmesine.",
        start_date=start_date,
        end_date=end_date,
        requester=both_apis.local_api
    )

    run_decision_book_items(
        writers_token=writers_token,
        info_type_uu_id=info_type_reno_uu_id,
        item_comment="Kiriş içinden geçen borunun çıkarılmadan kesilmesine, gerekli tadilatın yapılıp problemin "
                     "çözülmesine ayrıca bu aşamada yaplımasına onay verilirse her daire kendi radyatölerinin temizliği ve tadilatın yapılmasına.",
        start_date=start_date,
        end_date=end_date,
        requester=both_apis.local_api
    )

    run_decision_book_items(
        writers_token=writers_token,
        info_type_uu_id=info_type_reno_uu_id,
        item_comment="Dogalgazdan tadilatında çıkan eski boruların satışının yapılmasına.",
        start_date=start_date,
        end_date=end_date,
        requester=both_apis.local_api
    )

    run_decision_book_items(
        writers_token=writers_token,
        info_type_uu_id=info_type_reno_uu_id,
        item_comment="Apartman içinin tadilat yapılması, ışık ve elektrik sisteminin elden geçirilmesine ve elektrik "
                     "sayaç il panoların kutuya konulmasına, doğalgaz borularının astar boya ile "
                     "boyanmasına teklif alınarak projec açılması.",
        start_date=start_date,
        end_date=end_date,
        requester=both_apis.local_api
    )

    run_decision_book_items(
        writers_token=writers_token,
        info_type_uu_id=info_type_reno_uu_id,
        item_comment="İç kapı teklif alınarak gerektiğinde tadilatın geliştirilmesi, tek kapı yapılması "
                     "yada iki kapının alternatifi ile değerlendirilerek fiyat alınması.",
        start_date=start_date,
        end_date=end_date,
        requester=both_apis.local_api
    )

    run_decision_book_items(
        writers_token=writers_token,
        info_type_uu_id=info_type_reno_uu_id,
        item_comment="Bina su deposunun temizletilmesi.",
        start_date=start_date,
        end_date=end_date,
        requester=both_apis.local_api
    )

    run_decision_book_items(
        writers_token=writers_token,
        info_type_uu_id=info_type_reno_uu_id,
        item_comment="Çatının havalandırma camlarının değiştirilmesi ve onarılması.",
        start_date=start_date,
        end_date=end_date,
        requester=both_apis.local_api
    )

    run_decision_book_items(
        writers_token=writers_token,
        info_type_uu_id=info_type_inf_uu_id,
        item_comment="Arka zemin daire sahibinin yoğun yağmurlarda evin su basmaması için "
                     "arka bölüme saçak yapabilmesine onay verildi.",
        requester=both_apis.local_api
    )

elif assign_people_to_create_item == 4:
    pass
