from flask import Blueprint, render_template, session, redirect, url_for

from models import (
    Product,
    Category,
    Order
)


dashboard_bp = Blueprint("dashboard", __name__)


@dashboard_bp.route(
    "/admin/dashboard",
    endpoint="admin_dashboard"
)
def admin_dashboard():

    if "user_id" not in session:
        return redirect(url_for("auth.login"))

    total_products = Product.query.count()

    total_categories = Category.query.count()

    total_orders = Order.query.count()

    active_products = Product.query.filter_by(
        is_active=True
    ).count()

    pending_orders = Order.query.filter_by(
        order_status="pending"
    ).count()

    return render_template(
        "admin/dashboard.html",
        total_products=total_products,
        total_categories=total_categories,
        total_orders=total_orders,
        active_products=active_products,
        pending_orders=pending_orders
    )