from datetime import date

from flask import render_template

from app.blueprints.dashboard import dashboard_bp
from app.models.settings import Settings
from app.models.paydown_plan import PaydownPlan
from app.services.summary import get_dashboard_data


@dashboard_bp.route('/')
def index():
    today = date.today()

    # Extract all context at the route layer — service receives plain values
    settings = Settings.query.first()
    monthly_income = settings.monthly_income if settings else None

    active_plan = PaydownPlan.query.filter_by(status='active').first()
    active_plan_id = active_plan.id if active_plan else None

    data = get_dashboard_data(
        year=today.year,
        month=today.month,
        active_plan_id=active_plan_id,
        monthly_income=monthly_income,
    )

    return render_template(
        'dashboard/index.html',
        data=data,
        today=today,
        active_page='dashboard',
    )
