"use client";

import { PageHeader } from "@/src/shared/ui";
import { useHealthcareLookups } from "@/src/modules/healthcare/hooks/useHealthcareLookups";
import {
  AppointmentsResourcePage,
  AppointmentsCalendarPage,
  WaitingListResourcePage,
  VisitSessionsResourcePage,
  DoctorsResourcePage,
  PatientsResourcePage,
  ExternalProvidersResourcePage,
} from "@/src/modules/healthcare/components/entities";
import { AggregateStatsRow, AnalyticsPage, HealthcarePageLoader } from "./shared";
import { useQuery } from "@tanstack/react-query";
import { healthcareApi } from "@/src/modules/healthcare/services/healthcare-api";
import { useTenantId } from "@/hooks/useTenantId";

export function ReceptionDashboard() {
  const { tenantId } = useTenantId();
  const apptsQ = useQuery({
    queryKey: ["healthcare", tenantId, "reception-appts"],
    queryFn: () => healthcareApi.appointments.list(tenantId!, { page_size: 10 }),
    enabled: !!tenantId,
  });
  const waitQ = useQuery({
    queryKey: ["healthcare", tenantId, "waiting-list"],
    queryFn: () => healthcareApi.waitingList.list(tenantId!),
    enabled: !!tenantId,
  });
  const visitsQ = useQuery({
    queryKey: ["healthcare", tenantId, "visit-sessions-queue"],
    queryFn: () => healthcareApi.visitSessions.list(tenantId!, { status: "queued" }),
    enabled: !!tenantId,
  });

  if (!tenantId || apptsQ.isLoading) return <HealthcarePageLoader />;

  return (
    <div>
      <PageHeader title="داشبورد پذیرش" description="صف، نوبت‌ها و لیست انتظار." />
      <AggregateStatsRow
        stats={[
          { label: "نوبت‌های امروز", value: apptsQ.data?.total ?? 0 },
          { label: "لیست انتظار", value: waitQ.data?.length ?? 0 },
          { label: "در صف ویزیت", value: visitsQ.data?.length ?? 0 },
        ]}
      />
    </div>
  );
}

export const ReceptionQueue = () => <VisitSessionsResourcePage title="صف نوبت" defaultStatus="queued" />;
export const ReceptionWalkIn = () => <WaitingListResourcePage title="مراجعه حضوری" />;
export const ReceptionCalendar = () => <AppointmentsCalendarPage title="تقویم نوبت" />;
export const ReceptionDoctors = () => <DoctorsResourcePage title="پزشکان" readOnly />;
export const ReceptionPatients = () => <PatientsResourcePage title="بیماران" />;
export const ReceptionCheckIn = () => <AppointmentsResourcePage title="پذیرش" defaultStatus="confirmed" />;
export const ReceptionCheckOut = () => <VisitSessionsResourcePage title="ترخیص" defaultStatus="in_progress" />;
export const ReceptionPayments = () => (
  <ExternalProvidersResourcePage kind="accounting" title="پرداخت پذیرش" />
);
export const ReceptionReports = () => <AnalyticsPage title="گزارش پذیرش" />;
