"use client";

import { useQuery } from "@tanstack/react-query";
import { PageHeader } 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 {
  ClinicsResourcePage,
  DepartmentsResourcePage,
  DoctorsResourcePage,
  BranchesResourcePage,
  AppointmentsResourcePage,
  StaffAssignmentsResourcePage,
  ClinicServicesResourcePage,
  ClinicPoliciesResourcePage,
} from "@/src/modules/healthcare/components/entities";
import { AggregateStatsRow, AnalyticsPage, HealthcarePageLoader } from "./shared";

export function HospitalDashboard() {
  const { tenantId } = useTenantId();
  const { clinics, doctors, patients, isLoading } = useHealthcareLookups();
  const deptsQ = useQuery({
    queryKey: ["healthcare", tenantId, "hospital-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: deptsQ.data?.length ?? 0 },
          { label: "پزشکان", value: doctors.length },
          { label: "بیماران", value: patients.length },
        ]}
      />
    </div>
  );
}

export const HospitalDepartments = () => <DepartmentsResourcePage title="بخش‌های بیمارستان" />;
export const HospitalDoctors = () => <DoctorsResourcePage title="پزشکان" />;
export const HospitalBeds = () => <BranchesResourcePage title="تخت / شعب" />;
export const HospitalClinics = () => <ClinicsResourcePage title="کلینیک‌های زیرمجموعه" readOnly />;
export const HospitalAppointments = () => <AppointmentsResourcePage title="نوبت‌های بیمارستان" />;
export const HospitalStaff = () => <StaffAssignmentsResourcePage title="پرسنل" />;
export function HospitalResources() {
  return (
    <div className="space-y-8">
      <ClinicServicesResourcePage title="خدمات" />
      <ClinicPoliciesResourcePage title="سیاست‌ها" />
    </div>
  );
}
export const HospitalReports = () => <AnalyticsPage title="گزارش بیمارستان" />;
