from flask import Blueprint, render_template, request, redirect, url_for

from datetime import date, timedelta

from padel.basededatos import get_db
from flask_login import login_required, current_user


bp = Blueprint("reservas", __name__)

@bp.route("/misreservas")
@login_required
def misreservas():
    db = get_db()

    hoy = date.today()
    manana = hoy + timedelta(days=1)
    pasado = hoy + timedelta(days=2)

    reservas = db.execute(
#        "SELECT id, nombre, fecha, hora FROM reservas WHERE fecha>=? AND fecha<=? ORDER BY fecha DESC", (hoy,pasado)
         "SELECT id, nombre, fecha, hora FROM reservas ORDER BY fecha DESC"
    ).fetchall()

    return render_template("reservas/misreservas.html", reservas=reservas, name=current_user.name)

@bp.route("/inserta/<diaelegido>", methods=['GET', 'POST'])
@login_required
def inserta(diaelegido):
    if request.method == "POST":
        nombre = request.form["nombre"] or "Anónimo"
        fecha = diaelegido
        hora = request.form["hora"]

        db = get_db()
        db.execute(
            "INSERT INTO reservas (nombre, fecha, hora) VALUES (?, ?, ?)", (current_user.name, fecha, hora),
        )
        db.commit()

        return redirect(url_for("reservas.misreservas"))


@bp.route("/reservar/<diaelegido>")
@login_required
def reservar(diaelegido):

    db = get_db()

    hoy = date.today()
    manana = hoy + timedelta(days=1)
    pasado = hoy + timedelta(days=2)

    reservas = db.execute(
        "SELECT id, nombre, fecha, hora FROM reservas WHERE fecha=? ORDER BY fecha DESC", (diaelegido,)
    ).fetchall()

    for fila in reservas:
        print("Fecha ", fila["fecha"] , ". Hora: ", fila["hora"]) 
    
    return render_template("reservas/reservar.html", diaelegido=diaelegido, name=current_user.name)

# Ruta para eliminar una reserva
@bp.route('/eliminar_reserva/<int:id>', methods=['POST'])
def eliminar_reserva(id):

    db = get_db()
    db.execute(
        "DELETE FROM reservas WHERE id=?", (id,)
    )
    db.commit()
    
    return redirect(url_for('reservas.misreservas'))

@bp.route("/eligedia", methods=['GET', 'POST'])
@login_required
def eligedia():
    hoy = date.today()
    manana = hoy + timedelta(days=1)
    pasado = hoy + timedelta(days=2)
    
    print("Hoy es: ", hoy)
    print("Mañana será: ", manana)
    print("Pasado será: ", pasado)

    if request.method == "POST":
        diaelegido = request.form['dias']

        print("Día elegido:", diaelegido)

        db = get_db()

        horashoy = db.execute(
            "SELECT * FROM horas WHERE fecha=?", (diaelegido,)
        ).fetchall()

        db.commit()

        if not horashoy:
            print("No hay horas para hoy")
            db = get_db()

            db.execute(
                "INSERT INTO horas (fecha, hora0900, hora1000, hora1100, hora1200, hora1300, hora1400, hora1500, hora1600, hora1700, hora1800, hora1900, hora2000, hora2100, hora2200) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (diaelegido,"-","-","-","-","-","-","-","-","-","-","-","-","-","-",),
            )
            db.commit()

            horashoy = db.execute(
                "SELECT * FROM horas WHERE fecha=?", (diaelegido,)
            ).fetchall()

            db.commit()

        print("eligedia: horashoy: ", horashoy)
        return redirect(url_for("reservas.reservar",diaelegido=diaelegido))
    
    return render_template("reservas/eligedia.html",hoy=hoy,manana=manana,pasado=pasado)
