"use server";

import { listLeadActivities } from "@/lib/appwrite/lead-queries";
import { requireTenant } from "@/lib/appwrite/tenant-guard";
import type { ActivityRow } from "./types";

export async function listLeadActivitiesForClient(leadId: string): Promise<ActivityRow[]> {
  try {
    const ctx = await requireTenant();
    const rows = await listLeadActivities(ctx.tenantId, leadId);
    return rows.map((a) => ({
      id: a.$id,
      leadId: a.leadId,
      type: a.type,
      content: a.content,
      calendarEventId: a.calendarEventId ?? null,
      occurredAt: a.occurredAt ?? null,
      createdAt: a.$createdAt,
      createdByName: "",
    }));
  } catch {
    return [];
  }
}
