import Link from "next/link";
import { requireSession } from "@/lib/auth";
import { loadOverview, sumDays } from "@/lib/data";
import { duration, efficiency, isoDate, num, precise } from "@/lib/format";
import { DailyEnergyChart, EfficiencyChart } from "@/components/charts";
import { Row, Shell, Tile } from "@/components/shell";
export const dynamic = "force-dynamic";
export default async function ConsumptionPage({
searchParams,
}: {
searchParams: Promise<{ date?: string }>;
}) {
await requireSession();
const { date: selectedDate } = await searchParams;
const { installationId, days } = await loadOverview(90, 1);
if (!installationId || days.length === 0) {
return (
No daily consumption records yet. The car uploads them as it drives.
);
}
const chartDays = days.map((day) => ({ date: day.date, kwh: day.day.kwh, km: day.day.km }));
const total = sumDays(days);
// Lifetime is derived: the sum of every day's totals, same as the backend computes.
const lifetime = { km: total.km, kwh: total.kwh, hours: total.hours, socDrop: 0 };
// Newest first for reading; the charts above keep chronological order.
const table = [...days].reverse();
// Find the selected day's record (if any). table is newest-first; a Map would be overkill
// for ≤90 entries.
const selected = selectedDate ? table.find((d) => d.date === selectedDate) : null;
const hasFilter = !!selectedDate;
const validFilter = hasFilter && !!selected;
// Date input bounds: oldest record to today.
const minDate = days[0].date; // days is chronological (oldest first after loadOverview reverse)
const maxDate = new Date().toISOString().slice(0, 10);
return (
{/* Date filter */}
{/* Selected day detail — shown above the period tiles when a filter is active */}
{hasFilter && (