from database import search_database
from geocoder import validate_address
from new_parser_test import parse
from route import build_route
from ouput_chat import RouteDescriber
import json

if __name__=='__main__':
    user_input,conversation_history = parse()
    user_input = json.loads(user_input)
    print(user_input)
    query =user_input['tags']
    user_address =user_input['user_location']
    user_time =user_input['time']
    val_output = validate_address('addresses.sqlite',user_address)
    print(val_output)
    found_points,mapping = search_database('output.json', query)
    print(len(found_points))
    print(mapping)
    user_position =[]
    if val_output['valid']:
        user_position.append(val_output['coordinates']['lat'])
        user_position.append(val_output['coordinates']['lon'])
    else:
        print('Адрес не найден')
    n_nodes = len(mapping)
    allow_extend = any(v[1] for v in mapping.values())
    print(len(mapping),allow_extend)
    route_otp =0
    if allow_extend:
        while n_nodes <= 5 and (route_otp := build_route(found_points, mapping, user_position, user_time, n_nodes,strategy='random') or route_otp): n_nodes += 1
    else:
        route_otp= build_route(found_points, mapping, user_position, user_time, n_nodes,strategy='random')
    route, places = route_otp
    dscb = RouteDescriber()
    print(conversation_history)
    description = dscb.generate_route_description(places,conversation_history)
    print(route)
    print(description)
    for pl in places: print(pl)
    q = input('ЗАГЛУШКА')
    answer = dscb.answer_question(q)
    print(answer)
