"use server";

import { revalidatePath } from "next/cache";

import { DATABASE_ID, TABLES } from "./schema";
import { createAdminClient } from "./server";
import { requireTenant } from "./tenant-guard";

type ActionState = { ok: boolean; error?: string };

export async function markMatchNotifiedAction(id: string): Promise<ActionState> {
  await requireTenant();
  const { tablesDB } = createAdminClient();

  try {
    await tablesDB.updateRow(DATABASE_ID, TABLES.propertyMatches, id, {
      notified: true,
    });
  } catch {
    return { ok: false, error: "Güncellenemedi." };
  }

  revalidatePath("/customers/matches");
  return { ok: true };
}

export async function markAllNotifiedAction(ids: string[]): Promise<ActionState> {
  await requireTenant();
  const { tablesDB } = createAdminClient();

  try {
    await Promise.all(
      ids.map((id) =>
        tablesDB.updateRow(DATABASE_ID, TABLES.propertyMatches, id, { notified: true }),
      ),
    );
  } catch {
    return { ok: false, error: "Toplu güncelleme başarısız." };
  }

  revalidatePath("/customers/matches");
  return { ok: true };
}
