import random

from datetime import datetime, timedelta
from api_validations.validations_request import InsertPerson, InsertUsers
from faker import Faker

faker_engine = Faker(["tr-TR"])


def generate_random_country_code():
    return random.choice(["TR", "EN"])


def generate_random_digit(n):
    return random.randint(10 ** (n - 1), 10**n)


def generate_random_gender():
    return random.choice(["M", "F"])


def generate_random_city_code():
    return random.choice(["ANK", "IST", "IZM"])


def generate_random_phone_number():
    return f"+90{''.join([str(random.randint(0, 9)) for _ in range(10)])}"


def generate_random_coordinates():
    latitude = random.uniform(-90, 90)
    longitude = random.uniform(-180, 180)
    return latitude, longitude


def generate_random_profile():
    """
    asd = {
    'job': 'Belediye meclisi üyesi', 'company': 'Zorlu Enerji', 'ssn': '33384169120',
    'residence': 'Unit 2753 Box 0533\nDPO AE 94969', 'current_location': (Decimal('57.4049185'), Decimal('56.715540')),
    'blood_group': 'B+', 'website': ['https://borusan.tr/'], 'username': 'mugberdemirel', 'name': 'Kelâmi Yorulmaz',
    'sex': 'M', 'address': '39314 Zorlu Ramp\nEast Ömürberg, MO 85532', 'mail': 'denizalp28@yandex.com',
    'birthdate': datetime.date(2017, 8, 20)
    }
    """
    return faker_engine.profile()


def create_random_email(people_id: int = None, company_id: int = None):
    return {
        "email": faker_engine.email(domain="evyos.com.tr"),
        "expiry_starts": datetime.now().__str__(),
        "expiry_ends": str(datetime.now() + timedelta(days=365)),
        "email_name": faker_engine.email(domain="evyos.com.tr").split("@")[0],
        "people_id": people_id,
        "company_id": company_id,
    }


def create_random_phone(people_id: int = None, company_id: int = None):
    return {
        "country_code": generate_random_country_code(),
        "phone_no": generate_random_phone_number(),
        "city_code": generate_random_city_code(),
        "expiry_starts": datetime.now().__str__(),
        "expiry_ends": str(datetime.now() + timedelta(days=365)),
        "description": faker_engine.text(max_nb_chars=50),
        "people_id": people_id,
        "company_id": company_id,
    }


def create_random_person():
    profile = generate_random_profile()
    return InsertPerson(
        **{
            "firstname": faker_engine.first_name(),
            "surname": faker_engine.last_name(),
            "middle_name": faker_engine.name_male().split(" ")[1],
            "father_name": faker_engine.name_male().replace(".", ""),
            "mother_name": faker_engine.name_female().replace(".", ""),
            "sex_code": profile.get("sex"),
            "country_code": generate_random_country_code(),
            "national_identity_id": str(generate_random_digit(n=11)),
            "birth_place": faker_engine.city(),
            "birth_date": faker_engine.date_of_birth(
                minimum_age=25, maximum_age=50
            ).__str__(),
            "tax_no": str(generate_random_digit(n=10)),
            "is_employee_related": True,
            "is_living_space_related": False,
        }
    )


def create_random_user(people_id: int):
    profile = generate_random_profile()
    insert_user = InsertUsers(
        **dict(
            user_tag=profile.get("username"),
            domain_name="wag.evyos.com",
            email=faker_engine.email(domain="evyos.com.tr"),
            phone_number=generate_random_phone_number(),
            avatar="https://s.tmimgcdn.com/scr/800x500/276800/building-home-nature-logo-vector-template-3_276851-original.jpg",
            people_uu_id=str(people_id),
        )
    )
    return insert_user.excluded_dump()


def create_random_occupant_user(people_id: int):
    profile = generate_random_profile()
    insert_user = InsertUsers(
        **dict(
            user_tag=profile.get("username"),
            domain_name="wag.evyos.com",
            email=faker_engine.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",
            people_uu_id=str(people_id),
        )
    )
    return insert_user.excluded_dump()
