"use client";

import dayjs from "dayjs";
import { DashboardAiBanner } from "@/features/ai/components/dashboard-ai-banner";
import { DashboardMagazineCarousel } from "@/features/magazine/components/dashboard-magazine-carousel";
import { DashboardHeader } from "@/features/panel/components/dashboard-header";
import { DashboardMotivationBanner } from "@/features/panel/components/dashboard-motivation-banner";
import { DashboardSubscriptionCard } from "@/features/panel/components/dashboard-subscription-card";
import { DashboardSummaryCard } from "@/features/panel/components/dashboard-summary-card";
import { panelDict } from "@/features/panel/dict";
import {
  formatDashboardProgressPill,
  getGoalsDashboardSummary,
  getHabitsDashboardSummary,
} from "@/features/panel/utils/dashboard-helpers";
import { useGoals } from "@/features/goals/hooks/use-goals";
import { useHabits } from "@/features/habits/hooks/use-habits";
import { useTodayTasks } from "@/features/tasks/hooks/use-tasks";
import { getTaskProgress } from "@/features/tasks/utils/task-helpers";

export function DashboardPageContent() {
  const today = dayjs().format("YYYY-MM-DD");
  const { data: goals = [], isLoading: isLoadingGoals } = useGoals();
  const { data: habits = [], isLoading: isLoadingHabits } = useHabits();
  const { data: tasks = [], isLoading: isLoadingTasks } = useTodayTasks(today);

  const goalsSummary = getGoalsDashboardSummary(goals);
  const habitsSummary = getHabitsDashboardSummary(habits);
  const { total: taskTotal, completed: taskCompleted } = getTaskProgress(tasks);

  return (
    <div className="mx-auto flex w-full max-w-3xl flex-1 flex-col gap-4 px-4 py-4 sm:gap-5 sm:px-6 sm:py-6">
      <DashboardHeader />

      <DashboardMotivationBanner />

      <div className="flex flex-col gap-4 sm:gap-5">
        <DashboardSummaryCard
          variant="goals"
          isLoading={isLoadingGoals}
          activeCount={goalsSummary.active}
          completedCount={goalsSummary.completed}
          totalCount={goalsSummary.total}
          avgProgress={goalsSummary.avgProgress}
          emptyMessage={panelDict.noGoals}
          isEmpty={goalsSummary.total === 0}
          href="/panel/goals"
          linkLabel={panelDict.viewGoals}
        />

        <DashboardSummaryCard
          variant="habits"
          isLoading={isLoadingHabits}
          progressPill={
            habitsSummary.scheduledToday > 0
              ? formatDashboardProgressPill(
                  habitsSummary.completedToday,
                  habitsSummary.scheduledToday,
                )
              : undefined
          }
          activeCount={habitsSummary.total}
          completedCount={habitsSummary.completedToday}
          totalCount={habitsSummary.scheduledToday}
          emptyMessage={panelDict.noHabits}
          isEmpty={habitsSummary.total === 0}
          href="/panel/habits"
          linkLabel={panelDict.viewHabits}
        />

        <DashboardSummaryCard
          variant="tasks"
          isLoading={isLoadingTasks}
          progressPill={
            taskTotal > 0
              ? formatDashboardProgressPill(taskCompleted, taskTotal)
              : undefined
          }
          activeCount={taskTotal}
          completedCount={taskCompleted}
          totalCount={taskTotal}
          emptyMessage={panelDict.noTasksToday}
          isEmpty={taskTotal === 0}
          href="/panel/tasks"
          linkLabel={panelDict.viewTasks}
        />

        <DashboardSubscriptionCard />

        <DashboardAiBanner />

        <DashboardMagazineCarousel />
      </div>
    </div>
  );
}
