from flask import render_template, request, Blueprint
from minibase.database.models import Company, Post, Company_note, Person_note
from minibase.config import themeMinibase, globalVars
from minibase import db

# Declaring a blueprint
main = Blueprint('main', __name__)

# Redirect from / and also /home routes to the /
@main.route("/")
@main.route("/home")
def home():
    # (PAGINATION) Defines the page number that we will start with
    page = request.args.get('page', 1, type=int)
    # (POSTS) Query posts usin SQLAlchemy
    posts = Post.query.order_by(Post.date_posted.asc()).paginate(per_page=2)
    companies = Company.query.order_by(Company.name.asc())
    companie_notes = Company_note.query.order_by(Company_note.title.asc())
    person_notes = Person_note.query.order_by(Person_note.title.asc())
    # (HTML) Renders the template for templates/home.html
    return render_template('home.html',
                           theme=themeMinibase,
                           globalVars=globalVars,
                           posts=posts,
                           companies=companies,
                           companie_notes=companie_notes,
                           person_notes=person_notes)


@main.route("/about")
def about():
    return render_template('about.html',
                            theme=themeMinibase,
                            globalVars=globalVars,
                            title='About')


@main.route("/customer")
def customer():
    return render_template('customer.html',
                            theme=themeMinibase,
                            globalVars=globalVars,
                            title='About')


@main.cli.command("initdb")
def reset_db():
    """Drops and Creates fresh database"""
    db.drop_all()
    db.create_all()

    print("Initialized default DB") 
