import random

from service_app_test.api_configs import BothAPIS
from api_validations.validations_request import InsertPerson, InsertUsers
from service_app_test.test_application.migrate_old_data.reader_and_alchemy_bulk_actions import (
    read_json_file,
)

# from service_app_test.test_application.evyos.people import generate_random_phone_number

requester_dict_build = lambda data: {"endpoint": "/people/create", "data": data}


def get_people_from_json():
    read_files_json, with_pydantic = read_json_file(json_file="people"), []
    read_files = read_files_json.get("people")
    for row in read_files:
        row["ref_id"] = row["uu_id"]
        pydantic_row = InsertPerson(**row)
        data_dict = pydantic_row.model_dump()
        data_dict["email"] = row.get("emails")
        with_pydantic.append(data_dict)
    if not with_pydantic:
        raise Exception("No data found")
    return with_pydantic


generate_random_national_identity_id = lambda n: str(
    random.randint(10 ** (n - 1), 10**n)
)


def migrate_people(requester: BothAPIS):
    for response_data in get_people_from_json():
        users_email = response_data.get("email")
        response_data["national_identity_id"] = generate_random_national_identity_id(11)
        response_data.pop("email", None)
        response = requester.local_api.post(**requester_dict_build(data=response_data))
        print("response", response.text)
        if response.ok:
            user_json = response.json().get("data", None)
            if not user_json:
                raise Exception("User can not be created user json is invalid")
            created_user = InsertUsers(
                people_uu_id=user_json.get("uu_id"),
                user_tag=user_json.get("firstname"),
                email=users_email,
                # phone_number=generate_random_phone_number(),
                avatar="https://s.tmimgcdn.com/scr/800x500/276800/building-home-nature-logo-vector-template-3_276851-original.jpg",
            )
            response = requester.local_api.post(
                endpoint="/user/create",
                data=created_user.model_dump(),
            )
            print("response", response.text)
    return
