"use server";

import { z } from "zod";
import { db } from "@/lib/db";
import { clients } from "@/lib/db/schema";
import { auth } from "@/lib/auth";
import { revalidatePath } from "next/cache";
import { eq } from "drizzle-orm";

const clientSchema = z.object({
  name: z.string().min(1, "Name is required"),
  email: z.string().email("Valid email address required"),
});

export async function createClient(
  _prevState: { error?: string; success?: boolean } | null,
  formData: FormData
): Promise<{ error?: string; success?: boolean }> {
  const session = await auth();
  if (!session) {
    return { error: "Unauthorized" };
  }

  const parsed = clientSchema.safeParse({
    name: formData.get("name"),
    email: formData.get("email"),
  });

  if (!parsed.success) {
    return { error: parsed.error.issues[0].message };
  }

  await db.insert(clients).values({
    name: parsed.data.name,
    email: parsed.data.email,
  });

  revalidatePath("/portal/clients");
  return { success: true };
}

export async function updateClient(
  id: string,
  _prevState: { error?: string; success?: boolean } | null,
  formData: FormData
): Promise<{ error?: string; success?: boolean }> {
  const session = await auth();
  if (!session) {
    return { error: "Unauthorized" };
  }

  const parsed = clientSchema.safeParse({
    name: formData.get("name"),
    email: formData.get("email"),
  });

  if (!parsed.success) {
    return { error: parsed.error.issues[0].message };
  }

  await db
    .update(clients)
    .set({ name: parsed.data.name, email: parsed.data.email, updatedAt: new Date() })
    .where(eq(clients.id, id));

  revalidatePath("/portal/clients");
  revalidatePath("/portal/clients/" + id);
  return { success: true };
}

export async function deleteClient(
  id: string
): Promise<{ error?: string; success?: boolean }> {
  const session = await auth();
  if (!session) {
    return { error: "Unauthorized" };
  }

  await db.delete(clients).where(eq(clients.id, id));

  revalidatePath("/portal/clients");
  return { success: true };
}
