"use server";

import {prisma} from "@/lib/db/prisma";
import {getTenantDb} from "@/lib/db/tenant";

export async function getCustomers(tenantSlug: string) {
  const tenant = await prisma.tenant.findUnique({where: {slug: tenantSlug}});
  if (!tenant) return [];
  
  const db = getTenantDb(tenant.id);
  const profiles = await db.customerProfile.findMany({
    include: {
      user: {select: {firstName: true, lastName: true, email: true, phone: true}},
      _count: {select: {bookings: true}}
    },
    orderBy: {createdAt: "desc"}
  });

  return profiles.map((p) => ({
    id: p.id,
    userId: p.userId,
    name: p.user.firstName ? `${p.user.firstName} ${p.user.lastName ?? ""}` : p.user.email,
    email: p.user.email,
    phone: p.user.phone ?? "—",
    totalBookings: p._count.bookings,
    blacklisted: p.blacklisted
  }));
}
