#!/usr/bin/python3
import json
import sqlite3
import requests
import time 
from flask import Flask

# Initialize the Flask app
app = Flask(__name__)

# Dump the contents of a SQLite database to the response
@app.route("/dump/<ask_timestamp>")
def dump(ask_timestamp):
    # Open the database connection
    con = sqlite3.connect("../btc_timeseries.db")


    # Create a cursor to navigate the database
    cur = con.cursor()

    # Fetch all rows from the table
    rows = cur.execute("SELECT * FROM timeseries").fetchall()

    data = {
            "parameter": ask_timestamp,
            "rows": rows
            }

    # Build the response as a string
    response = json.dumps(data)

    con.close()

    for row in rows:
        old_timestamp = time.strptime(row[0], "%Y-%m-%d %H:%M:%S")
        unix_timestamp = time.mktime(old_timestamp)
        if int(ask_timestamp) < int(unix_timestamp):
            print('EQUALS: ', ask_timestamp, ' AND ', unix_timestamp)
        else:
            print('NOPE ', row[0], ' AS ', unix_timestamp)

    return response

#@app.route("/dump/timestamp")
#def dump(timestamp):
#    # Open the database connection
#    con = sqlite3.connect("../btc_timeseries.db")
#
#    print(timestamp)
#
#    # Create a cursor to navigate the database
#    cur = con.cursor()
#
#    # Fetch all rows from the table
#    rows = cur.execute("SELECT * FROM timeseries").fetchall()
#
#    # Build the response as a string
#    response = ""
#    for row in rows:
#        response += ", ".join(row) + "\n"
#
#    # Close the database connection
#    con.close()
#
#    # Return the response
#    return response

# Run the app
if __name__ == "__main__":
    app.run()
