import "server-only"; import { DATABASE_ID, Q, TABLES, tablesDB } from "@/lib/appwrite-rest"; import { getSessionSecret } from "@/lib/auth"; import type { BlogPostRow, ContactMessageRow, ProjectRow, ServiceRow, SeoPageRow, SeoSettingsRow, TestimonialRow, } from "@/lib/types"; async function safeList(tableId: string, queries: string[]): Promise { try { const res = await tablesDB.listRows(DATABASE_ID, tableId, queries); return res.rows; } catch { return []; } } async function safeListAuth(tableId: string, queries: string[]): Promise { try { const secret = await getSessionSecret(); const res = await tablesDB.listRows( DATABASE_ID, tableId, queries, secret ?? undefined, ); return res.rows; } catch { return []; } } export async function listServices(opts?: { featured?: boolean }) { const q = [Q.orderAsc("order"), Q.limit(50)]; if (opts?.featured) q.unshift(Q.equal("featured", true)); return safeList(TABLES.services, q); } export async function listProjects(opts?: { featured?: boolean; limit?: number; serviceSlug?: string; }) { const q = [Q.orderDesc("year"), Q.limit(opts?.limit ?? 50)]; if (opts?.featured) q.unshift(Q.equal("featured", true)); if (opts?.serviceSlug) q.unshift(Q.equal("service_slug", opts.serviceSlug)); return safeList(TABLES.projects, q); } export async function getServiceBySlug(slug: string): Promise { const res = await safeList(TABLES.services, [ Q.equal("slug", slug), Q.limit(1), ]); return res[0] ?? null; } export async function getProjectBySlug(slug: string): Promise { const res = await safeList(TABLES.projects, [ Q.equal("slug", slug), Q.limit(1), ]); return res[0] ?? null; } export async function listPublishedPosts(opts?: { limit?: number }) { return safeList(TABLES.blogPosts, [ Q.equal("status", "published"), Q.orderDesc("published_at"), Q.limit(opts?.limit ?? 50), ]); } export async function listAllPosts() { return safeListAuth(TABLES.blogPosts, [ Q.orderDesc("$createdAt"), Q.limit(200), ]); } export async function getPostBySlug(slug: string): Promise { const res = await safeList(TABLES.blogPosts, [ Q.equal("slug", slug), Q.limit(1), ]); return res[0] ?? null; } export async function listTestimonials(opts?: { featured?: boolean }) { const q = [Q.orderAsc("order"), Q.limit(50)]; if (opts?.featured) q.unshift(Q.equal("featured", true)); return safeList(TABLES.testimonials, q); } export async function listMessages(status?: ContactMessageRow["status"]) { const q = [Q.orderDesc("$createdAt"), Q.limit(200)]; if (status) q.unshift(Q.equal("status", status)); return safeListAuth(TABLES.contactMessages, q); } export async function getSeoPage(path: string): Promise { const res = await safeList(TABLES.seoPages, [ Q.equal("path", path), Q.limit(1), ]); return res[0] ?? null; } export async function listSeoPages() { return safeListAuth(TABLES.seoPages, [ Q.orderAsc("path"), Q.limit(200), ]); } export async function getSeoSettings(): Promise { try { return await tablesDB.getRow( DATABASE_ID, TABLES.seoSettings, "global", ); } catch { return null; } } export async function getRow( tableId: string, rowId: string, ): Promise { try { const secret = await getSessionSecret(); return await tablesDB.getRow( DATABASE_ID, tableId, rowId, secret ?? undefined, ); } catch { return null; } }