import { q as queryOptions, s as supabase } from "./router-CHpUX3w1.js";
async function fetchAdminOrders() {
  const { data, error } = await supabase.from("orders").select("*").order("created_at", { ascending: false }).limit(500);
  if (error) throw new Error(error.message);
  return data ?? [];
}
const adminOrdersQueryOptions = () => queryOptions({
  queryKey: ["admin", "orders"],
  queryFn: fetchAdminOrders,
  staleTime: 1e4
});
async function fetchAdminProducts() {
  const { data, error } = await supabase.from("products").select(
    "id, slug, name, category, fabric, description, price, old_price, sizes, is_active, is_new, is_bestseller, display_order, created_at, product_variants(id, color_name, color_hex, image_url, display_order)"
  ).order("display_order", { ascending: true });
  if (error) throw new Error(error.message);
  return data ?? [];
}
const adminProductsQueryOptions = () => queryOptions({
  queryKey: ["admin", "products"],
  queryFn: fetchAdminProducts,
  staleTime: 1e4
});
async function fetchBlacklist() {
  const { data, error } = await supabase.from("blacklisted_phones").select("id, phone, reason, created_at").order("created_at", { ascending: false });
  if (error) throw new Error(error.message);
  return data ?? [];
}
const blacklistQueryOptions = () => queryOptions({
  queryKey: ["admin", "blacklist"],
  queryFn: fetchBlacklist,
  staleTime: 3e4
});
async function fetchDashboardStats() {
  const [orders, products] = await Promise.all([
    supabase.from("orders").select("status, payment_status, total_amount, shipping_cost, customer_phone, is_flagged, created_at"),
    supabase.from("products").select("is_active")
  ]);
  if (orders.error) throw new Error(orders.error.message);
  if (products.error) throw new Error(products.error.message);
  const now = Date.now();
  const oneDayAgo = now - 24 * 60 * 60 * 1e3;
  const o = orders.data ?? [];
  const p = products.data ?? [];
  const uniquePhones = new Set(o.map((x) => x.customer_phone)).size;
  const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
  const currentYear = (/* @__PURE__ */ new Date()).getFullYear();
  const monthlyMap = /* @__PURE__ */ new Map();
  for (let i = 0; i < 12; i++) monthlyMap.set(i, { orders: 0, revenue: 0 });
  for (const order of o) {
    const d = new Date(order.created_at);
    if (d.getFullYear() !== currentYear) continue;
    const m = d.getMonth();
    const slot = monthlyMap.get(m);
    slot.orders += 1;
    if (order.payment_status === "paid" || order.status === "delivered") {
      slot.revenue += Number(order.total_amount);
    }
  }
  const monthlyOrders = monthNames.map((name, i) => ({
    month: name,
    orders: monthlyMap.get(i).orders,
    revenue: monthlyMap.get(i).revenue
  }));
  return {
    totalSale: o.filter((x) => x.status === "delivered").length,
    totalProducts: p.length,
    activeProducts: p.filter((x) => x.is_active).length,
    totalCustomers: uniquePhones,
    pendingOrders: o.filter((x) => x.status === "pending").length,
    confirmedOrders: o.filter((x) => x.status === "confirmed").length,
    shippedOrders: o.filter((x) => x.status === "shipped").length,
    deliveredOrders: o.filter((x) => x.status === "delivered").length,
    cancelledOrders: o.filter((x) => x.status === "cancelled").length,
    flaggedOrders: o.filter((x) => x.is_flagged).length,
    totalRevenue: o.filter((x) => x.status === "delivered").reduce((sum, x) => sum + Number(x.total_amount), 0),
    deliveryEarned: o.filter((x) => x.status === "delivered").reduce((sum, x) => sum + Number(x.shipping_cost), 0),
    pendingAmount: o.filter((x) => x.payment_status === "unpaid" && x.status !== "cancelled").reduce((sum, x) => sum + Number(x.total_amount), 0),
    todayOrders: o.filter((x) => new Date(x.created_at).getTime() > oneDayAgo).length,
    totalOrders: o.length,
    monthlyOrders
  };
}
const dashboardStatsQueryOptions = () => queryOptions({
  queryKey: ["admin", "dashboard-stats"],
  queryFn: fetchDashboardStats,
  staleTime: 1e4
});
function formatBDT(amount) {
  return `৳${Number(amount).toLocaleString("bn-BD", { maximumFractionDigits: 0 })}`;
}
function formatDateTime(value) {
  const d = typeof value === "string" ? new Date(value) : value;
  return d.toLocaleString("bn-BD", {
    day: "2-digit",
    month: "short",
    year: "numeric",
    hour: "2-digit",
    minute: "2-digit"
  });
}
const ORDER_STATUS_LABEL = {
  pending: "অপেক্ষমান",
  confirmed: "নিশ্চিত",
  shipped: "শিপড",
  delivered: "ডেলিভারড",
  cancelled: "বাতিল"
};
const PAYMENT_STATUS_LABEL = {
  unpaid: "অপরিশোধিত",
  paid: "পরিশোধিত",
  refunded: "রিফান্ডেড"
};
export {
  ORDER_STATUS_LABEL as O,
  PAYMENT_STATUS_LABEL as P,
  adminProductsQueryOptions as a,
  adminOrdersQueryOptions as b,
  formatDateTime as c,
  dashboardStatsQueryOptions as d,
  blacklistQueryOptions as e,
  formatBDT as f
};
