from json import loads
from os import path
from databases import (
    AddressCountry,
    AddressCity,
    AddressDistrict,
    AddressLocality,
    AddressNeighborhood,
    AddressState,
)

path_to_folder = "initialize_app/default_inits"
list_of_snippets = [
    "countries.json",
    "cities.json",
    "district.json",
    "locality.json",
    "neighborhood.json",
]


def create_country_defaults(path_to_joined_folder, confirmed_by_system):

    with open(path_to_joined_folder("countries.json"), "r") as file:
        countries = loads(file.read())

        print("Countries are read from file ------------------")
        if not countries:
            print("Countries json file is empty")

        for country in countries.get("tr_co"):
            country_obj = AddressCountry.find_or_create(
                country_name=country.get("country_name"),
                country_code=country.get("country_code"),
                ref_id=str(country.get("ref_id")),
                **confirmed_by_system,
            )


def create_cities_defaults(path_to_joined_folder, confirmed_by_system, state_id):

    with open(path_to_joined_folder("cities.json"), "r") as file:
        cities = loads(file.read())
        print("Cities are read from file ------------------")
        if not cities:
            print("Cities json file is empty")

        for city in cities.get("cities"):
            city_obj = AddressCity.find_or_create(
                state_id=state_id,
                city_name=city.get("city_name"),
                city_code=city.get("licence_plate"),
                licence_plate=city.get("licence_plate"),
                ref_id=str(city.get("ref_id")),
                **confirmed_by_system,
            )
            print(f"City {city_obj.city_name} is created")


def create_district_defaults(path_to_joined_folder, confirmed_by_system):

    with open(path_to_joined_folder("district.json"), "r") as file:
        districts = loads(file.read())
        print("Districts are read from file ------------------")
        if not districts:
            print("Districts json file is empty")

        for district in districts.get("tr_ilce"):
            city = AddressCity.find_one(ref_id=str(district.get("city_id")))
            if not city:
                print(f"City with ref_id {district.get('city_id')} is not found")

            district_obj = AddressDistrict.find_or_create(
                city_id=city.id,
                district_name=district.get("district_name"),
                district_code=str(district.get("ref_id")),
                ref_id=str(district.get("ref_id")),
                **confirmed_by_system,
            )
            print(f"District {district_obj} is created")


def create_locality_defaults(path_to_joined_folder, confirmed_by_system):

    with open(path_to_joined_folder("locality.json"), "r") as file:
        localities = loads(file.read())
        print("Localities are read from file ------------------")
        if not localities:
            print("Localities json file is empty")

        for locality in localities.get("tr_semt"):
            district = AddressDistrict.find_one(ref_id=str(locality.get("district_id")))
            if not district:
                print(
                    f"District with ref_id {locality.get('district_id')} is not found"
                )

            locality_obj = AddressLocality.find_or_create(
                district_id=district.id,
                locality_name=locality.get("locality_name"),
                locality_code=str(locality.get("post_code")),
                ref_id=str(locality.get("ref_id")),
                **confirmed_by_system,
            )
            print(f"Locality {locality_obj} is created")


def create_neighborhood_defaults(path_to_joined_folder, confirmed_by_system):

    with open(path_to_joined_folder("neighborhood.json"), "r") as file:
        neighborhoods = loads(file.read())
        print("Neighborhoods are read from file ------------------")
        if not neighborhoods:
            print("Neighborhoods json file is empty")

        for neighborhood in neighborhoods.get("tr_mahalle_koy"):
            locality = AddressLocality.find_one(
                ref_id=str(neighborhood.get("locality_id"))
            )
            district = AddressDistrict.find_one(
                ref_id=str(neighborhood.get("district_id"))
            )

            if not district:
                print(
                    f"District with ref_id {neighborhood.get('district_id')} is not found"
                )

            neighborhood_obj = AddressNeighborhood.find_or_create(
                locality_id=locality.id if locality else None,
                district_id=district.id,
                neighborhood_name=neighborhood.get("neighborhood_name"),
                neighborhood_code=str(neighborhood.get("ref_id")),
                ref_id=str(neighborhood.get("ref_id")),
                **confirmed_by_system,
            )
            print(f"Neighborhood {neighborhood_obj} is created")


def create_identity_address_defaults():

    print("Creating address defaults ------------------")

    path_to_joined_folder = lambda json_name: path.join(path_to_folder, json_name)
    confirmed_by_system = dict(
        is_confirmed=True,
        active=True,
        deleted=False,
        is_notification_send=True,
        created_by="System",
        confirmed_by="System",
    )

    create_country_defaults(path_to_joined_folder, confirmed_by_system)

    turkey = AddressCountry.find_one(ref_id="90")
    turkey_state = AddressState.find_or_create(
        state_name="Türkiye",
        state_code="TR",
        country_id=turkey.id,
        **confirmed_by_system,
    )

    create_cities_defaults(path_to_joined_folder, confirmed_by_system, turkey_state.id)
    create_district_defaults(path_to_joined_folder, confirmed_by_system)
    create_locality_defaults(path_to_joined_folder, confirmed_by_system)
    create_neighborhood_defaults(path_to_joined_folder, confirmed_by_system)
    print("All address defaults are created ------------------")
    return True
