import type { SupabaseClient } from '@supabase/supabase-js'; import type { Database } from '@kit/supabase/database'; export function createSiteBuilderApi(client: SupabaseClient) { return { // Pages async listPages(accountId: string) { const { data, error } = await client.from('site_pages').select('*') .eq('account_id', accountId).order('sort_order'); if (error) throw error; return data ?? []; }, async getPage(pageId: string) { const { data, error } = await client.from('site_pages').select('*').eq('id', pageId).single(); if (error) throw error; return data; }, async getPageBySlug(accountId: string, slug: string) { const { data, error } = await client.from('site_pages').select('*') .eq('account_id', accountId).eq('slug', slug).single(); if (error) throw error; return data; }, async getHomepage(accountId: string) { const { data, error } = await client.from('site_pages').select('*') .eq('account_id', accountId).eq('is_homepage', true).eq('is_published', true).maybeSingle(); if (error) throw error; return data; }, async createPage(input: { accountId: string; slug: string; title: string; puckData?: Record; isHomepage?: boolean; metaDescription?: string }, userId: string) { const { data, error } = await client.from('site_pages').insert({ account_id: input.accountId, slug: input.slug, title: input.title, puck_data: (input.puckData ?? {}) as any, is_homepage: input.isHomepage ?? false, meta_description: input.metaDescription, created_by: userId, updated_by: userId, }).select().single(); if (error) throw error; return data; }, async updatePage(pageId: string, input: { title?: string; slug?: string; puckData?: Record; isPublished?: boolean; isHomepage?: boolean; metaDescription?: string; metaImage?: string }, userId: string) { const update: Record = { updated_by: userId }; if (input.title !== undefined) update.title = input.title; if (input.slug !== undefined) update.slug = input.slug; if (input.puckData !== undefined) update.puck_data = input.puckData; if (input.isPublished !== undefined) { update.is_published = input.isPublished; if (input.isPublished) update.published_at = new Date().toISOString(); } if (input.isHomepage !== undefined) update.is_homepage = input.isHomepage; if (input.metaDescription !== undefined) update.meta_description = input.metaDescription; if (input.metaImage !== undefined) update.meta_image = input.metaImage; const { data, error } = await client.from('site_pages').update(update).eq('id', pageId).select().single(); if (error) throw error; return data; }, async deletePage(pageId: string) { const { error } = await client.from('site_pages').delete().eq('id', pageId); if (error) throw error; }, // Settings async getSiteSettings(accountId: string) { const { data, error } = await client.from('site_settings').select('*').eq('account_id', accountId).maybeSingle(); if (error) throw error; return data; }, async upsertSiteSettings(accountId: string, input: Record) { const row: Record = { account_id: accountId }; if (input.siteName !== undefined) row.site_name = input.siteName; if (input.siteLogo !== undefined) row.site_logo = input.siteLogo; if (input.primaryColor !== undefined) row.primary_color = input.primaryColor; if (input.secondaryColor !== undefined) row.secondary_color = input.secondaryColor; if (input.fontFamily !== undefined) row.font_family = input.fontFamily; if (input.navigation !== undefined) row.navigation = input.navigation; if (input.footerText !== undefined) row.footer_text = input.footerText; if (input.contactEmail !== undefined) row.contact_email = input.contactEmail; if (input.contactPhone !== undefined) row.contact_phone = input.contactPhone; if (input.contactAddress !== undefined) row.contact_address = input.contactAddress; if (input.impressum !== undefined) row.impressum = input.impressum; if (input.datenschutz !== undefined) row.datenschutz = input.datenschutz; if (input.isPublic !== undefined) row.is_public = input.isPublic; const { data, error } = await client.from('site_settings').upsert(row as any).select().single(); if (error) throw error; return data; }, // Posts async listPosts(accountId: string, status?: string) { let query = client.from('cms_posts').select('*').eq('account_id', accountId).order('created_at', { ascending: false }); if (status) query = query.eq('status', status); const { data, error } = await query; if (error) throw error; return data ?? []; }, async getPost(postId: string) { const { data, error } = await client.from('cms_posts').select('*').eq('id', postId).single(); if (error) throw error; return data; }, async createPost(input: { accountId: string; title: string; slug: string; content?: string; excerpt?: string; coverImage?: string; status?: string }, userId: string) { const { data, error } = await client.from('cms_posts').insert({ account_id: input.accountId, title: input.title, slug: input.slug, content: input.content, excerpt: input.excerpt, cover_image: input.coverImage, status: input.status ?? 'draft', author_id: userId, published_at: input.status === 'published' ? new Date().toISOString() : null, }).select().single(); if (error) throw error; return data; }, async updatePost(postId: string, input: { title?: string; slug?: string; content?: string; excerpt?: string; coverImage?: string; status?: string }) { const update: Record = {}; if (input.title !== undefined) update.title = input.title; if (input.slug !== undefined) update.slug = input.slug; if (input.content !== undefined) update.content = input.content; if (input.excerpt !== undefined) update.excerpt = input.excerpt; if (input.coverImage !== undefined) update.cover_image = input.coverImage; if (input.status !== undefined) { update.status = input.status; if (input.status === 'published') update.published_at = new Date().toISOString(); } const { data, error } = await client.from('cms_posts').update(update).eq('id', postId).select().single(); if (error) throw error; return data; }, async deletePost(postId: string) { const { error } = await client.from('cms_posts').delete().eq('id', postId); if (error) throw error; }, // Newsletter async subscribe(accountId: string, email: string, name?: string) { const token = crypto.randomUUID(); const { error } = await client.from('newsletter_subscriptions').upsert({ account_id: accountId, email, name, confirmation_token: token, is_active: true, }, { onConflict: 'account_id,email' }); if (error) throw error; return token; }, }; }