from folium import Map, Marker
from pandas import DataFrame, json_normalize

from app.data_request import request_dataset
from constants import COLUMNS, COORDINATES


def create_dataframe(dataset) -> DataFrame:
    """
    Creates a DataFrame from a JSON response
    """
    json = request_dataset(dataset)
    df = json_normalize(data=json, record_path=["records"], errors="ignore",)
    filtered_df = df.filter(items=COLUMNS[dataset])
    return filtered_df


def create_map(df):
    """
    Creates a Map with markers from the DataFrame
    """
    m = Map(location=COORDINATES, zoom_start=12)
    for index, row in df.iterrows():
        Marker(location=row["fields.geo_shape.coordinates"]).add_to(m)
    m.save("app/templates/map.html")
