import { prisma } from '@/lib/prisma';
import type { Locale } from '@/i18n/config';

// Thin server-side query layer. Kept separate from components so the
// admin dashboard and the public site share the same read logic.

export async function getPublishedServices(locale: Locale, take?: number) {
  const services = await prisma.service.findMany({
    where: { published: true },
    orderBy: { order: 'asc' },
    take,
    include: { translations: { where: { locale } } }
  });
  return services
    .filter((s) => s.translations[0])
    .map((s) => ({
      slug: s.slug,
      heroImage: s.heroImage ?? undefined,
      ...s.translations[0]
    }));
}

export async function getPublishedProducts(locale: Locale, take?: number) {
  const products = await prisma.product.findMany({
    where: { published: true },
    take,
    orderBy: { createdAt: 'desc' },
    include: {
      translations: { where: { locale } },
      category: { include: { translations: { where: { locale } } } }
    }
  });
  return products
    .filter((p) => p.translations[0])
    .map((p) => ({
      id: p.id,
      slug: p.slug,
      sku: p.sku,
      price: p.price ? Number(p.price) : null,
      currency: p.currency,
      available: p.available,
      image: p.images[0],
      categorySlug: p.category.slug as string,
      categoryName: p.category.translations[0]?.name ?? p.category.slug,
      name: p.translations[0].name,
      shortDescription: p.translations[0].shortDescription
    }));
}

export async function getCategories(locale: Locale) {
  const categories = await prisma.category.findMany({
    orderBy: { order: 'asc' },
    include: { translations: { where: { locale } } }
  });
  return categories.map((c) => ({
    id: c.id,
    slug: c.slug,
    name: c.translations[0]?.name ?? c.slug
  }));
}

export async function getPublishedPosts(locale: Locale, take?: number) {
  const posts = await prisma.post.findMany({
    where: { published: true },
    take,
    orderBy: { publishedAt: 'desc' },
    include: {
      translations: { where: { locale } },
      author: { include: { translations: { where: { locale } } } },
      tags: { include: { translations: { where: { locale } } } }
    }
  });
  return posts
    .filter((p) => p.translations[0])
    .map((p) => ({
      slug: p.slug,
      coverImage: p.coverImage ?? undefined,
      publishedAt: (p.publishedAt ?? p.createdAt).toISOString(),
      readingMinutes: p.readingMinutes ?? undefined,
      authorName: p.author.translations[0]?.name ?? '',
      tagSlugs: p.tags.map((t) => t.slug),
      title: p.translations[0].title,
      excerpt: p.translations[0].excerpt
    }));
}

export async function getTags(locale: Locale) {
  const tags = await prisma.tag.findMany({
    orderBy: { slug: 'asc' },
    include: { translations: { where: { locale } } }
  });
  return tags.map((t) => ({
    slug: t.slug,
    name: t.translations[0]?.name ?? t.slug
  }));
}

export async function getPublishedProjects(locale: Locale, take?: number) {
  const projects = await prisma.project.findMany({
    where: { published: true },
    take,
    orderBy: { completedAt: 'desc' },
    include: { translations: { where: { locale } } }
  });
  return projects
    .filter((p) => p.translations[0])
    .map((p) => ({
      slug: p.slug,
      coverImage: p.images[0],
      location: p.location ?? undefined,
      installationType: p.installationType ?? undefined,
      systemSizeKwc: p.systemSizeKwc ? Number(p.systemSizeKwc) : undefined,
      title: p.translations[0].title
    }));
}

export async function getPublishedTestimonials(locale: Locale) {
  const testimonials = await prisma.testimonial.findMany({
    where: { published: true },
    include: { translations: { where: { locale } } }
  });
  return testimonials
    .filter((t) => t.translations[0])
    .map((t) => ({
      quote: t.translations[0].quote,
      authorName: t.authorName,
      authorRole: t.authorRole ?? undefined,
      companyName: t.companyName ?? undefined
    }));
}
