from datetime import datetime
import dateutil.parser
import json


import requests
from requests.models import to_key_val_list

from sqlmodel import Field, Session, SQLModel, create_engine, select
from fastapi.logger import logger


from database import engine
from models import Listing, Facility, Image, InterestPoint, Route, RouteCreate, PlaceNearby


def get_daft_search_result():
    try:
        response = requests.get('http://daft:8000/search_result/')
        response.raise_for_status()
        # Additional code will only run if the request is successful
    except requests.exceptions.HTTPError as error:
        print(error)
    return response.json()


def get_daft_details(url):
    try:
        print(url)
        params = {
            'url': url,
            'method': 'json_details',
        }

        response = requests.get(
            'http://daft:8000/listing_details/', params=params)
        response.raise_for_status()
        return response.json()
        # Additional code will only run if the request is successful
    except requests.exceptions.HTTPError as error:
        logger.error(error)
        return response.json()
    


def get_routes_json(from_lat, from_long, to_lat, to_long):
    try:
        data = {
            "from_point": {"lat": from_lat, "long": from_long},
            "to_point": {"lat": to_lat, "long": to_long}
        }

        response = requests.post(
            'http://location:8000/route/', data=json.dumps(data))
        response.raise_for_status()
        return response.json()
        # Additional code will only run if the request is successful
    except requests.exceptions.HTTPError as error:
        logger.error(error)
        return {}


def get_routes(listing: Listing):
    ret_ = []
    with Session(engine) as session:
        interest_points_sttm = 