"use client";

import { useQuery } from "@tanstack/react-query";
import { PageHeader, EmptyState } from "@/src/shared/ui";
import { healthcareApi } from "@/src/modules/healthcare/services/healthcare-api";
import { useTenantId } from "@/hooks/useTenantId";
import { useHealthcareLookups } from "@/src/modules/healthcare/hooks/useHealthcareLookups";
import {
  BranchesResourcePage,
  DepartmentsResourcePage,
  DoctorsResourcePage,
  PatientsResourcePage,
  StaffAssignmentsResourcePage,
  ClinicServicesResourcePage,
  ClinicPoliciesResourcePage,
  SchedulesResourcePage,
  ExternalProvidersResourcePage,
} from "@/src/modules/healthcare/components/entities";
import { AggregateStatsRow, AnalyticsPage, HealthcarePageLoader } from "./shared";

export function ClinicDashboard() {
  const { tenantId } = useTenantId();
  const { clinics, doctors, patients, isLoading } = useHealthcareLookups();
  const deptsQ = useQuery({
    queryKey: ["healthcare", tenantId, "clinic-depts"],
    queryFn: () => healthcareApi.departments.list(tenantId!),
    enabled: !!tenantId,
  });

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

  return (
    <div>
      <PageHeader title="داشبورد کلینیک" description="نمای کلی مدیریت کلینیک." />
      <AggregateStatsRow
        stats={[
          { label: "کلینیک‌ها", value: clinics.length },
          { label: "پزشکان", value: doctors.length },
          { label: "بیماران", value: patients.length },
          { label: "بخش‌ها", value: deptsQ.data?.length ?? 0 },
        ]}
      />
    </div>
  );
}

export const ClinicDoctors = () => <DoctorsResourcePage title="پزشکان کلینیک" />;
export const ClinicReceptionists = () => <StaffAssignmentsResourcePage title="پذیرش / پرسنل" />;
export const ClinicDepartments = () => <DepartmentsResourcePage title="بخش‌ها" />;
export const ClinicRooms = () => <BranchesResourcePage title="اتاق‌ها / شعب" />;
export const ClinicSchedules = () => <SchedulesResourcePage title="برنامه‌های کلینیک" />;
export function ClinicResources() {
  return (
    <div className="space-y-8">
      <ClinicServicesResourcePage title="خدمات" />
      <ClinicPoliciesResourcePage title="سیاست‌ها" />
    </div>
  );
}
export const ClinicPatients = () => <PatientsResourcePage title="بیماران کلینیک" />;
export const ClinicReports = () => <AnalyticsPage title="گزارش کلینیک" />;
export const ClinicCrmIntegration = () => <ExternalProvidersResourcePage kind="crm" title="یکپارچگی CRM" />;
export const ClinicAccountingIntegration = () => (
  <ExternalProvidersResourcePage kind="accounting" title="یکپارچگی حسابداری" />
);
