import { DashboardHomePage } from "../features/Pages/DashboardHomePage/DashboardHomePage";
import { transformHealthDataToStats } from "../features/GeneralHealthCard/utils";
import { fetchHealthDataService } from "../services/health";
import { getDefaultDateRange } from "../utils/formatDate";

export default async function DashboardPage() {
  // Fetch initial health data server-side with default 24h range
  const defaultDates = getDefaultDateRange();
  let initialHealthData = null;
  let initialStats = null;

  try {
    const healthData = await fetchHealthDataService({
      dateStart: defaultDates.dateStart,
      dateEnd: defaultDates.dateEnd,
    });
    initialHealthData = healthData;
    // console.log("[healthData]", healthData);
    initialStats = healthData.stats ?? transformHealthDataToStats(healthData);
  } catch (_error: unknown) {
    // If fetch fails, component will handle it client-side
    // console.error("Failed to fetch initial health data:", error);
    const error = _error as Error;
    console.error("Failed to fetch initial health data:", error.cause);
  }

  console.log("[initialStats]", initialStats);

  return (
    <DashboardHomePage
      initialHealthData={initialHealthData}
      initialStats={initialStats}
      initialDateRange={[
        {
          startDate: new Date(defaultDates.dateStart),
          endDate: new Date(defaultDates.dateEnd),
          key: "selection",
        },
      ]}
    />
  );
}
