import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func
import datetime as dt

import numpy as np
import pandas as pd

from flask import Flask, jsonify
from flask import current_app, g
from flask.cli import with_appcontext

import sqlite3
import click

database_path = '/Users/christinemagnotta/Booty/10-Advanced-Data-Storage-and-Retrieval/HW/Instructions/Resources/hawaii.sqlite'
engine = create_engine(f"sqlite:///{database_path}")


session = Session(engine)
Base = automap_base()
Base.prepare(engine, reflect=True)

Measurement = Base.classes.measurement
Station = Base.classes.station



app = Flask(__name__)
@app.route("/")
def home():
    print("Server received request for 'Home' page...")
    return "Aloha! Check out this data on Hawaiian climate!"

    

@app.route("/about")
def about():
    print("Server received request for 'routes' page...")
    return "The routes on this site are: precipitation, stations and temperature."




#Return a JSON list of stations from the dataset.
@app.route("/api/v1.0/stations")
def stations():
    stations = session.query(Station.station).all()
    stations = list(np.ravel(stations))
    return jsonify(stations = stations)
    

#Convert the query results to a dictionary using date as the key and prcp as the value.
#Return the JSON representation of your dictionary.

@app.route("/api/v1.0/precipitation")
def precipitation():
    #/api/v1.0/precipitation
    previousYear = dt.date(2017, 8, 23) - dt.timedelta(days=365)
    precipitation = session.query(Measurement.date, Measurement.prcp).filter(Measurement.date >= previousYear).all() 
    precipitation = {"date":precipitation}
    return jsonify(precipitation)


#Query the dates and temperature observations of the most active station for the last year of data.
#Return a JSON list of temperature observations (TOBS) for the previous year.
@app.route("/api/v1.0/tobs")
def temperature():
    station_temperature = session.query(Measurement.tobs).filter(Measurement.station == 'USC00519281').all()
    station_temperature = list(np.ravel(station_temperature))
    return jsonify(station_temperature = station_temperature)


if __name__ == "__main__":
    app.run(debug=True)


