import { notFound } from "next/navigation";
import { api } from "@/server/api/client";
import { TRPCError } from "@trpc/server";
import DayView from "@/components/dashboard/days/DayView";
import spacetime from "spacetime";
import { useTimezone } from "@lifetracker/shared-react/hooks/timezones";
import { date } from "drizzle-orm/pg-core";

export default async function DayPage({ params }: { params: { dateQuery: string }; }) {
    const { dateQuery } = await params;
    let day;
    const tzName = await api.users.getTimezone();
    // console.log(`(dashboard/day/[dateQuery]/page.tsx) Loading ${spacetime.now(tzName).format("yyyy-MM-dd")} in ${tzName}.`);
    try {
        day = await api.days.get({
            dateQuery: spacetime(dateQuery, tzName).format("yyyy-MM-dd"),
            timezone: tzName,
        });
    } catch (e) {
        console.log("DATEQ", params);
        if (e instanceof TRPCError) {
            if (e.code == "NOT_FOUND") {
                notFound();
            }
        }
        throw e;
    }

    return (
        <>
            <DayView
                day={day}
            />
        </>
    );
}
