"use client";

import { useQuery } from "@tanstack/react-query";
import { beautyBusinessApi } from "@/modules/beauty/services/beauty-business-api";
import { useTenantId } from "@/hooks/useTenantId";
import { formatMoney } from "@/lib/utils";
import {
  PageHeader,
  DataTable,
  EmptyState,
  LoadingState,
  ErrorState,
  Badge,
} from "@/components/ds";

export default function MembershipsPage() {
  const { tenantId } = useTenantId();

  const listQ = useQuery({
    queryKey: ["beauty", tenantId, "membership-plans"],
    queryFn: () => beautyBusinessApi.membershipPlans.list(tenantId!),
    enabled: !!tenantId,
  });

  if (!tenantId || listQ.isLoading) return <LoadingState />;
  if (listQ.error) {
    return <ErrorState message={listQ.error.message} onRetry={() => listQ.refetch()} />;
  }

  return (
    <div>
      <PageHeader title="عضویت‌ها" description="طرح‌های عضویت و اشتراک." />
      <DataTable
        columns={[
          { key: "code", header: "کد" },
          { key: "name", header: "نام" },
          { key: "billing_interval_days", header: "دوره (روز)" },
          {
            key: "price",
            header: "قیمت",
            render: (r) => (r.price != null ? formatMoney(String(r.price)) : "—"),
          },
          {
            key: "status",
            header: "وضعیت",
            render: (r) => <Badge>{String(r.status)}</Badge>,
          },
        ]}
        rows={(listQ.data ?? []) as unknown as Record<string, unknown>[]}
        empty={<EmptyState title="طرح عضویتی ثبت نشده" />}
      />
    </div>
  );
}
