import { useQuery } from "@tanstack/react-query";
import { apiGet } from "@/lib/api/client";
import type { CafeTheme } from "@/lib/cafe-theme";
import { useAuthStore } from "@/lib/stores/auth.store";

export type CafeSettings = {
  id: string;
  name: string;
  slug?: string;
  phone?: string;
  address?: string;
  city?: string;
  description?: string;
  logoUrl?: string;
  coverImageUrl?: string;
  snappfoodVendorId?: string;
  planTier: string;
  theme: CafeTheme;
  defaultTaxRate?: number;
  allowBranchTaxOverride?: boolean;
};

export function cafeSettingsQueryKey(cafeId: string) {
  return ["cafe-settings", cafeId] as const;
}

export function useCafeSettings(cafeId?: string | null) {
  const authCafeId = useAuthStore((s) => s.user?.cafeId);
  const id = cafeId ?? authCafeId;

  return useQuery<CafeSettings>({
    queryKey: cafeSettingsQueryKey(id ?? ""),
    queryFn: () => {
      if (!id) throw new Error("Missing cafe id");
      return apiGet<CafeSettings>(`/api/cafes/${id}/settings`);
    },
    enabled: !!id,
  });
}
