"use client";

import { useQuery } from "@tanstack/react-query";
import { PageHeader, EmptyState, Badge, DataTable } from "@/components/ds";
import { beautyBusinessApi } from "@/modules/beauty/services/beauty-business-api";
import { useTenantId } from "@/hooks/useTenantId";
import { useBeautyLookups } from "@/modules/beauty/hooks/useBeautyLookups";
import { BeautyResourceWorkspace } from "@/modules/beauty/components/crud/BeautyResourceWorkspace";
import {
  organizationResource,
  branchResource,
  staffResource,
  customerResource,
} from "@/modules/beauty/configs/owner-resources";
import {
  AggregateStatsRow,
  AnalyticsPage,
  BeautyPageError,
  BeautyPageLoader,
  MonitoringPage,
  ServiceMetaBadges,
  SettingsListPage,
} from "./shared";

export function AdminDashboard() {
  const { tenantId } = useTenantId();
  const { organizations, branches, customers, staff, isLoading } = useBeautyLookups();

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

  return (
    <div>
      <PageHeader
        title="داشبورد مدیر سیستم"
        description="مدیریت tenant و گزارش."
        actions={<ServiceMetaBadges />}
      />
      <AggregateStatsRow
        stats={[
          { label: "سازمان‌ها", value: organizations.length },
          { label: "شعب", value: branches.length },
          { label: "مشتریان", value: customers.length },
          { label: "پرسنل", value: staff.length },
        ]}
      />
    </div>
  );
}

export function AdminOrganizations() {
  return <BeautyResourceWorkspace config={organizationResource} readOnly />;
}

export function AdminBranches() {
  return <BeautyResourceWorkspace config={branchResource} readOnly />;
}

export function AdminStaff() {
  return <BeautyResourceWorkspace config={staffResource} readOnly />;
}

export function AdminCustomers() {
  return <BeautyResourceWorkspace config={customerResource} readOnly />;
}

export function AdminPermissions() {
  const { tenantId } = useTenantId();
  const q = useQuery({
    queryKey: ["beauty", tenantId, "admin-permissions"],
    queryFn: () => beautyBusinessApi.permissions.catalog(tenantId!),
    enabled: !!tenantId,
  });

  if (!tenantId || q.isLoading) return <BeautyPageLoader />;
  if (q.error) return <BeautyPageError error={q.error} onRetry={() => q.refetch()} />;

  return (
    <div>
      <PageHeader title="دسترسی‌ها" description="permissions catalog API." />
      <p className="mb-4 text-sm text-[var(--muted)]">prefix: {q.data?.prefix}</p>
      <DataTable
        columns={[{ key: "permission", header: "دسترسی" }]}
        rows={(q.data?.permissions ?? []).map((p) => ({ permission: p })) as Record<string, unknown>[]}
        empty={<EmptyState title="دسترسی‌ای ثبت نشده" />}
      />
    </div>
  );
}

export function AdminReports() {
  return <AnalyticsPage title="گزارش مدیر" />;
}

export function AdminAnalytics() {
  return <AnalyticsPage title="آنالیتیکس مدیر" />;
}

export function AdminMonitoring() {
  return <MonitoringPage />;
}

export function AdminSettings() {
  return <SettingsListPage title="تنظیمات مدیر" description="settings API." />;
}
