"""Example web application for postgres_air."""
# Run with: flask --app flight run --debug

import psycopg
from flask import Flask, render_template, request

app = Flask(__name__)
con = psycopg.connect(host="localhost", user="demo", password="demo", dbname="air")
cur = con.cursor()


@app.route("/")
def flight_search():
    # Get form inputs with default values
    airport = request.args.get("airport", "IAD")
    beg_date = request.args.get("beg_date", "2023-10-29")
    end_date = request.args.get("end_date", "2023-10-30")
    # If the user submitted the form
    if request.args:
        cur.execute(
            "SELECT * FROM flight "
            "WHERE departure_airport = %s"
            "  AND scheduled_departure > %s"
            "  AND scheduled_departure < %s",
            (airport, beg_date, end_date),
        )
        data = cur.fetchall()
    else:
        data = None
    return render_template("flight.jinja",
        airport=airport, beg_date=beg_date, end_date=end_date, data=data)
