/**
 * Payload CMS API functions for porwoll.de
 *
 * Uses @c2s/payload-contracts client for transport.
 * All queries are automatically scoped to tenant "porwoll" (ID: 1).
 */
import { cms } from "./cms"

// Re-export CMS types for convenience
export type { Page, Post, Media, Navigation, SiteSetting, Testimonial, Faq } from "@c2s/payload-contracts/types"
export type { PaginatedResponse } from "@c2s/payload-contracts/types"

// Pages
export async function getPage(slug: string, locale = "de") {
  return cms.pages.getPage(slug, { locale: locale as "de" | "en", depth: 2 })
}

export async function getPages(options: { limit?: number; page?: number; locale?: string } = {}) {
  return cms.pages.getPages({
    limit: options.limit || 100,
    page: options.page || 1,
    locale: (options.locale || "de") as "de" | "en",
    depth: 1,
  })
}

// Posts
export async function getPost(slug: string, locale = "de") {
  return cms.posts.getPost(slug, { locale: locale as "de" | "en", depth: 2 })
}

export async function getPosts(options: {
  type?: string
  category?: string
  limit?: number
  page?: number
  locale?: string
} = {}) {
  try {
    return await cms.posts.getPosts({
      type: options.type,
      category: options.category,
      limit: options.limit || 10,
      page: options.page || 1,
      locale: (options.locale || "de") as "de" | "en",
    })
  } catch {
    return { docs: [], totalDocs: 0, limit: 10, totalPages: 0, page: 1, pagingCounter: 1, hasPrevPage: false, hasNextPage: false, prevPage: null, nextPage: null }
  }
}

// Navigation
export async function getNavigation(type: "header" | "footer" | "mobile" = "header") {
  try {
    return await cms.navigation.getNavigation(type, { depth: 2 })
  } catch {
    return null
  }
}

// Site Settings
export async function getSiteSettings() {
  try {
    return await cms.settings.getSiteSettings({ depth: 2 })
  } catch {
    return null
  }
}

// SEO Settings (Global)
export async function getSeoSettings() {
  try {
    return await cms.settings.getSeoSettings()
  } catch {
    return null
  }
}

// Social Links
export async function getSocialLinks(): Promise<{ platform: string; url: string }[]> {
  try {
    const result = await cms.client.getCollection("social-links", {
      limit: 20,
      depth: 1,
    })
    return (result?.docs || []).map((link: unknown) => {
      const l = link as Record<string, unknown>
      return {
        platform: (l.platform as string) || "",
        url: (l.url as string) || "",
      }
    })
  } catch {
    return []
  }
}

// Testimonials
export async function getTestimonials(options: { limit?: number; locale?: string } = {}) {
  return cms.client.getCollection("testimonials", {
    limit: options.limit || 10,
    locale: (options.locale || "de") as "de" | "en",
    depth: 1,
  })
}

// Bookings (porwoll-specific: photography bookings)
export async function getBookings(options: { limit?: number; locale?: string } = {}) {
  return cms.client.getCollection("bookings", {
    limit: options.limit || 20,
    locale: (options.locale || "de") as "de" | "en",
    depth: 1,
  })
}

// Newsletter Subscription
export async function subscribeNewsletter(
  email: string,
  firstName?: string,
  source = "website"
): Promise<{ success: boolean; message?: string }> {
  return cms.post("/api/newsletter/subscribe", {
    email,
    firstName,
    tenantId: Number(process.env.NEXT_PUBLIC_TENANT_ID || "1"),
    source,
  })
}

// Contact Form Submission
export async function submitContactForm(data: {
  name: string
  email: string
  phone?: string
  subject: string
  message: string
  formId?: number
}): Promise<{ success: boolean; message?: string }> {
  return cms.post("/api/form-submissions", {
    form: data.formId || 1,
    submissionData: [
      { field: "name", value: data.name },
      { field: "email", value: data.email },
      { field: "phone", value: data.phone || "" },
      { field: "subject", value: data.subject },
      { field: "message", value: data.message },
    ],
  })
}
