"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 { ClinicCard, StatWidget } from "@/src/modules/healthcare/design-system";
import {
  ClinicsResourcePage,
  DoctorsResourcePage,
  PatientsResourcePage,
  PharmaciesResourcePage,
  PermissionsCatalogPage,
  AuditExplorerPage,
  SettingsResourcePage,
} from "@/src/modules/healthcare/components/entities";
import {
  AggregateStatsRow,
  AnalyticsPage,
  HealthcarePageLoader,
  MonitoringPage,
  ServiceMetaBadges,
} from "./shared";

export function AdminDashboard() {
  const { tenantId } = useTenantId();
  const { clinics, doctors, patients, isLoading } = useHealthcareLookups();
  const pharmQ = useQuery({
    queryKey: ["healthcare", tenantId, "admin-pharmacies"],
    queryFn: () => healthcareApi.pharmacies.list(tenantId!),
    enabled: !!tenantId,
  });

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

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

export function AdminOrganizations() {
  const { clinics, isLoading } = useHealthcareLookups();
  if (isLoading) return <HealthcarePageLoader />;
  return (
    <div>
      <PageHeader title="سازمان‌ها" description="سازمان‌های درمانی — mapped از clinics API." />
      <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
        {clinics.map((c) => (
          <ClinicCard key={c.id} clinic={c} href="/healthcare/admin/clinics" />
        ))}
      </div>
      {clinics.length === 0 ? <EmptyState title="سازمانی ثبت نشده" /> : null}
    </div>
  );
}

export const AdminClinics = () => <ClinicsResourcePage title="کلینیک‌ها" />;
export const AdminDoctors = () => <DoctorsResourcePage title="پزشکان" />;
export const AdminPatients = () => <PatientsResourcePage title="بیماران" />;
export const AdminPharmacies = () => <PharmaciesResourcePage title="داروخانه‌ها" />;
export const AdminPermissions = PermissionsCatalogPage;
export const AdminSettings = () => <SettingsResourcePage title="تنظیمات سیستم" />;
export const AdminMonitoring = MonitoringPage;
export const AdminReports = () => <AnalyticsPage title="گزارش مدیر سیستم" />;
export const AdminAnalytics = () => <AnalyticsPage title="آنالیتیکس" />;
export const AdminLogs = () => <AuditExplorerPage title="لاگ سیستم" />;

export function AdminUsers() {
  return <DoctorsResourcePage title="کاربران (پزشکان)" />;
}
