"""Realty Info"""

import os
import requests

from dotenv import load_dotenv
from fastapi import APIRouter, Depends
import sqlalchemy
from pydantic import BaseModel, SecretStr

from app import config
from app.walk_score import *

load_dotenv()

router = APIRouter()

headers = {'x-rapidapi-key': os.getenv('api_key'),
           'x-rapidapi-host': os.getenv('host') }


@router.get('/streamlined_rent_list')
async def streamlined_rent_list(api_key = config.settings.api_key, 
             city: str = "New York City", 
             state: str= "NY", 
             prop_type: str = "condo", 
             limit: int = 4):

    """
    Parameters:
        api_key
        city: str 
        state: str
        prop_type: str ('condo', 'single_family', 'multi_family')
        limit: int number of results to populate

    Returns:
        information about properties for rent
    """
    url = os.getenv('url_list_for_rent')
    querystring = {"city": city,
                 "state_code": state,
                 "limit": limit,
                 "offset": "0",                 
                 "sort":"relevance",
                 "prop_type": prop_type}

    response_for_rent = requests.request("GET", url, params = querystring, headers = headers,)
    response = response_for_rent.json()['properties']

    rental_list = []
    for i in range(limit):
      line = response[i]['address']['line']
      city = response[i]['address']['city']
      state = response[i]['address']['state']
      lat = response[i]['address']['lat']
      lon = response[i]['address']['lon']
      photos = response[i]['photos']
      address = line +" "+  city + " "+ state
      walk_score = just_walk_score(address, lat, lon)
      element = {'address': address, 
                'lat': lat, 
                'lon': lon, 
                'city':city, 
                'state':state, 
                'photos': photos,  
                'walk_score': walk_score}

      rental_list.append(element)

    return rental_list
  

@router.get('/for_rent_list')
async def for_rent_list(api_key = config.settings.api_key, 
             city: str = "New York City", 
             state: str= "NY", 
             prop_type: str = "condo", 
             limit: int = 4):

    """
    Parameters:
        api_key
        city: str 
        state: str
        prop_type: str ('condo', 'single_family', 'multi_family')
        limit: int number of results to populate

    Returns:
        information about properties for rent
    """
    url = os.getenv('url_list_for_rent')
    querystring = {"city": city,
                 "state_code": state,
                 "limit": limit,
                 "offset": "0",                 
                 "sort":"relevance",
                 "prop_type": prop_type}

    response_for_rent = requests.request("GET", url, params = querystring, headers = headers,)
    return response_for_rent.json()['properties']


@router.get('/for_rent_list/{property_id}')
async def property_detail(property_id: str = "O3599084026"):
    """
    Parameters: 
        property_id
    Returns:
        detailed information about the property
    """
    url = os.getenv('url_property_detail')
    querystring = {"property_id":property_id}
    
    response_prop_detail = requests.request("GET", url, headers=headers, params=querystring)
    return response_prop_detail.json()['properties']


@router.get('/for_sale_list')
async def for_sale_list(api_key = config.settings.api_key, 
             city = "New York City", 
             state= "NY",             
             limit = 4):

    
    url = os.getenv('url_list_for_sale')
    querystring = {"city": city ,"limit": limit,"offset":"0","state_code": state,"sort":"relevance"}

    response_for_sale = requests.request("GET", url, headers=headers, params=querystring)
    return response_for_sale.json()['properties']




[((14, 0, 14, 13), 'dotenv.load_dotenv', 'load_dotenv', ({}, {}), '()', False, 'from dotenv import load_dotenv\n'), ((16, 9, 16, 20), 'fastapi.APIRouter', 'APIRouter', ({}, {}), '()', False, 'from fastapi import APIRouter, Depends\n'), ((18, 29, 18, 49), 'os.getenv', 'os.getenv', ({(18, 39, 18, 48): '"""api_key"""'}, {}), "('api_key')", False, 'import os\n'), ((19, 30, 19, 47), 'os.getenv', 'os.getenv', ({(19, 40, 19, 46): '"""host"""'}, {}), "('host')", False, 'import os\n'), ((40, 10, 40, 40), 'os.getenv', 'os.getenv', ({(40, 20, 40, 39): '"""url_list_for_rent"""'}, {}), "('url_list_for_rent')", False, 'import os\n'), ((48, 24, 48, 94), 'requests.request', 'requests.request', (), '', False, 'import requests\n'), ((92, 10, 92, 40), 'os.getenv', 'os.getenv', ({(92, 20, 92, 39): '"""url_list_for_rent"""'}, {}), "('url_list_for_rent')", False, 'import os\n'), ((100, 24, 100, 94), 'requests.request', 'requests.request', (), '', False, 'import requests\n'), ((112, 10, 112, 42), 'os.getenv', 'os.getenv', ({(112, 20, 112, 41): '"""url_property_detail"""'}, {}), "('url_property_detail')", False, 'import os\n'), ((115, 27, 115, 92), 'requests.request', 'requests.request', (), '', False, 'import requests\n'), ((126, 10, 126, 40), 'os.getenv', 'os.getenv', ({(126, 20, 126, 39): '"""url_list_for_sale"""'}, {}), "('url_list_for_sale')", False, 'import os\n'), ((129, 24, 129, 89), 'requests.request', 'requests.request', (), '', False, 'import requests\n')]