from schemas import (
    Addresses,
    AddressCity,
    AddressStreet,
    AddressLocality,
    AddressDistrict,
    AddressNeighborhood,
    AddressState,
    AddressCountry,
)


def create_one_address(db_session):

    address_list = []
    AddressCountry.set_session(db_session)
    country = AddressCountry.query.filter_by(country_name="TÜRKİYE", country_code="TR").first()
    if not country:
        country = AddressCountry.create(
            country_name="TÜRKİYE", country_code="TR", is_confirmed=True
        )
        country.save()
        address_list.append(country)
    else:
        print(f"Country already exists {country.to_dict()}")
    
    AddressState.set_session(db_session)
    state = AddressState.query.filter_by(state_name="TÜRKİYE", state_code="TR").first()
    if not state:
        state = AddressState.create(
            state_name="TÜRKİYE",
            state_code="TR",
            phone_code="90",
            country_id=country.id,
            country_uu_id=str(country.uu_id),
            is_confirmed=True,
        )
        state.save()
        address_list.append(state)
    else:
        print(f"State already exists {state.to_dict()}")

    AddressCity.set_session(db_session)
    city = AddressCity.query.filter_by(city_name="ANKARA", city_code="6").first()
    if not city:
        city = AddressCity.create(
            city_name="ANKARA",
            city_code="6",
            licence_plate="06",
            state_id=state.id,
            state_uu_id=str(state.uu_id),
            is_confirmed=True,
        )
        city.save()
        address_list.append(city)
    else:
        print(f"City already exists {city.to_dict()}")
    
    AddressDistrict.set_session(db_session)
    district = AddressDistrict.query.filter_by(district_name="ÇANKAYA", district_code="1231").first()
    if not district:
        district = AddressDistrict.create(
            district_name="ÇANKAYA",
            district_code="1231",
            city_id=city.id,
            city_uu_id=str(city.uu_id),
            is_confirmed=True,
        )
        district.save()
        address_list.append(district)
    else:
        print(f"District already exists {district.to_dict()}")
    
    AddressLocality.set_session(db_session)
    locality = AddressLocality.query.filter_by(locality_name="MERKEZ", locality_code="2431").first()
    if not locality:
        locality = AddressLocality.create(
            locality_name="MERKEZ",
            locality_code="2431",
            type_code="3",
            type_description=None,
            district_id=district.id,
            district_uu_id=str(district.uu_id),
            is_confirmed=True,
        )
        locality.save()
        address_list.append(locality)
    else:
        print(f"Locality already exists {locality.to_dict()}")
    
    AddressNeighborhood.set_session(db_session)
    neighborhood = AddressNeighborhood.query.filter_by(neighborhood_name="AYRANCI MAHALLESİ", neighborhood_code="1522").first()
    if not neighborhood:
        neighborhood = AddressNeighborhood.create(
            neighborhood_name="AYRANCI MAHALLESİ",
            neighborhood_code="1522",
            type_code="1",
            type_description="MAHALLESİ",
            locality_id=locality.id,
            locality_uu_id=str(locality.uu_id),
            is_confirmed=True,
        )
        neighborhood.save()
        address_list.append(neighborhood)
    else:
        print(f"Neighborhood already exists {neighborhood.to_dict()}")
    
    AddressStreet.set_session(db_session)
    street = AddressStreet.query.filter_by(street_name="REŞAT NURİ CADDESİ", street_code="52270").first()
    if not street:
        street = AddressStreet.create(
            street_name="REŞAT NURİ CADDESİ",
            type_description="CADDESİ",
            type_code="3",
            street_code="52270",
            neighborhood_id=neighborhood.id,
            neighborhood_uu_id=str(neighborhood.uu_id),
            is_confirmed=True,
        )
        street.save()
        address_list.append(street)
    else:
        print(f"Street already exists {street.to_dict()}")
    
    Addresses.set_session(db_session)
    address = Addresses.query.filter_by(street_id=street.id, street_uu_id=str(street.uu_id)).first()
    if not address:
        address = Addresses.create(
            street_id=street.id,
            street_uu_id=str(street.uu_id),
            build_number="Ex1",
            door_number="1",
            floor_number="1",
            comment_address="Example Address",
            letter_address="Example Address",
            short_letter_address="Example Address",
            latitude=0,
            longitude=0,
            is_confirmed=True,
        )
        address.save()
        address_list.append(address)
    else:
        print(f"Address already exists {address.to_dict()}")
