"use server";

import { prisma } from "@/lib/prisma";
import { revalidatePath } from "next/cache";
import { uploadFile } from "@/lib/upload";

// Carousel Actions
export async function getCarouselImages() {
  return await prisma.carouselImage.findMany({ orderBy: { orderIndex: 'asc' } });
}
export async function addCarouselImage(formData: FormData) {
  const file = formData.get("image") as File;
  const title = formData.get("title") as string;
  const description = formData.get("description") as string;
  
  let url = formData.get("url") as string;
  if (file && file.size > 0) {
    const uploadedUrl = await uploadFile(file);
    if (uploadedUrl) url = uploadedUrl;
  }
  
  await prisma.carouselImage.create({ data: { url, title, description } });
  revalidatePath("/admin/carousel");
  revalidatePath("/", "layout");
}
export async function deleteCarouselImage(id: string) {
  await prisma.carouselImage.delete({ where: { id } });
  revalidatePath("/admin/carousel");
  revalidatePath("/", "layout");
}

// Blog Actions
export async function getBlogs() {
  return await prisma.blogPost.findMany({ orderBy: { createdAt: 'desc' } });
}
export async function addBlog(formData: FormData) {
  const title = formData.get("title") as string;
  const content = formData.get("content") as string;
  const author = formData.get("author") as string;
  
  const file = formData.get("imageFile") as File;
  let image = formData.get("image") as string;
  if (file && file.size > 0) {
    const uploadedUrl = await uploadFile(file);
    if (uploadedUrl) image = uploadedUrl;
  }

  await prisma.blogPost.create({ data: { title, content, author, image, published: true } });
  revalidatePath("/admin/blogs");
  revalidatePath("/blog");
}
export async function deleteBlog(id: string) {
  await prisma.blogPost.delete({ where: { id } });
  revalidatePath("/admin/blogs");
  revalidatePath("/blog");
}

// Client Logo Actions
export async function getClients() {
  return await prisma.clientLogo.findMany({ orderBy: { orderIndex: 'asc' } });
}
export async function addClient(formData: FormData) {
  const name = formData.get("name") as string;
  
  const file = formData.get("logoFile") as File;
  let logoUrl = formData.get("logoUrl") as string;
  if (file && file.size > 0) {
    const uploadedUrl = await uploadFile(file);
    if (uploadedUrl) logoUrl = uploadedUrl;
  }

  await prisma.clientLogo.create({ data: { name, logoUrl } });
  revalidatePath("/admin/clients");
  revalidatePath("/", "layout");
}
export async function deleteClient(id: string) {
  await prisma.clientLogo.delete({ where: { id } });
  revalidatePath("/admin/clients");
  revalidatePath("/", "layout");
}

// Testimonial Actions
export async function getTestimonials() {
  return await prisma.testimonial.findMany({ orderBy: { orderIndex: 'asc' } });
}
export async function addTestimonial(formData: FormData) {
  const author = formData.get("author") as string;
  const company = formData.get("company") as string;
  const content = formData.get("content") as string;
  await prisma.testimonial.create({ data: { author, company, content, rating: 5 } });
  revalidatePath("/admin/testimonials");
  revalidatePath("/", "layout");
}
export async function deleteTestimonial(id: string) {
  await prisma.testimonial.delete({ where: { id } });
  revalidatePath("/admin/testimonials");
  revalidatePath("/", "layout");
}

// Settings Actions (Footer)
export async function getSettings() {
  const settings = await prisma.siteSetting.findMany();
  return settings.reduce((acc, curr) => ({ ...acc, [curr.key]: curr.value }), {} as Record<string, string>);
}
export async function updateSetting(key: string, value: string) {
  await prisma.siteSetting.upsert({
    where: { key },
    update: { value },
    create: { key, value }
  });
  revalidatePath("/admin/footer");
  revalidatePath("/", "layout");
}
