|
| 1 | + |
| 2 | +"use server"; |
| 3 | + |
| 4 | +import { prismadb } from "@/lib/prisma"; |
| 5 | +import { revalidatePath } from "next/cache"; |
| 6 | +import { logActivity } from "@/actions/audit"; |
| 7 | + |
| 8 | +// Mock templates for different industries |
| 9 | +const templates: any = { |
| 10 | + home: { |
| 11 | + hero: { title: "Future of {{brand}}", subtitle: "Experience the premium standard in {{industry}}.", primaryAction: "Get Started" }, |
| 12 | + features: [ |
| 13 | + { title: "Premium Quality", description: "Only the finest materials.", icon: "Shield" }, |
| 14 | + { title: "Global Reach", description: "Serving customers worldwide.", icon: "Globe" }, |
| 15 | + { title: "24/7 Support", description: "We are here when you need us.", icon: "Zap" } |
| 16 | + ] |
| 17 | + }, |
| 18 | + about: { |
| 19 | + hero: { title: "About {{brand}}", subtitle: "Our story of excellence.", primaryAction: "Contact Us" }, |
| 20 | + content: "Founded with a vision to revolutionize {{industry}}, {{brand}} has set the gold standard." |
| 21 | + }, |
| 22 | + pricing: { |
| 23 | + hero: { title: "Simple Pricing", subtitle: "Choose the plan that fits you.", primaryAction: "View Plans" }, |
| 24 | + plans: [ |
| 25 | + { plan: "Starter", price: "$29", features: "Basic Access, Support", recommended: "false" }, |
| 26 | + { plan: "Pro", price: "$99", features: "All Features, Priority Support", recommended: "true" } |
| 27 | + ] |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +export async function generateSite(data: { |
| 32 | + brandName: string; |
| 33 | + description: string; |
| 34 | + pages: Record<string, boolean>; |
| 35 | + customReqs?: string; |
| 36 | +}) { |
| 37 | + console.log("Generating site for:", data.brandName); |
| 38 | + |
| 39 | + try { |
| 40 | + const createdPages = []; |
| 41 | + |
| 42 | + for (const [pageKey, isSelected] of Object.entries(data.pages)) { |
| 43 | + if (!isSelected) continue; |
| 44 | + |
| 45 | + const template = templates[pageKey] || templates.home; // Fallback |
| 46 | + const title = pageKey.charAt(0).toUpperCase() + pageKey.slice(1); |
| 47 | + const slug = pageKey === 'home' ? 'home' : `${pageKey}`; |
| 48 | + |
| 49 | + // Replace placeholders |
| 50 | + const heroTitle = template.hero?.title.replace("{{brand}}", data.brandName) || title; |
| 51 | + const heroSubtitle = template.hero?.subtitle.replace("{{industry}}", data.description) || "Welcome to our site."; |
| 52 | + |
| 53 | + // Construct Puck JSON |
| 54 | + // We'll create a structured page based on the type |
| 55 | + let content: any = []; |
| 56 | + |
| 57 | + if (pageKey === 'header' || pageKey === 'footer') { |
| 58 | + // Component pages |
| 59 | + content = [ |
| 60 | + { type: "ContainerBlock", props: { className: "p-4 bg-slate-900 text-white", id: "nav" } } |
| 61 | + ]; |
| 62 | + } else { |
| 63 | + // Formatting content for Puck |
| 64 | + content.push({ |
| 65 | + type: "HeroBlock", |
| 66 | + props: { |
| 67 | + title: heroTitle, |
| 68 | + subtitle: heroSubtitle, |
| 69 | + primaryAction: template.hero?.primaryAction || "Learn More", |
| 70 | + bgImage: "https://images.unsplash.com/photo-1620121692029-d088224ddc74?auto=format&fit=crop&w=1600&q=80" |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + if (template.features) { |
| 75 | + content.push({ |
| 76 | + type: "FeaturesGridBlock", |
| 77 | + props: { |
| 78 | + title: "Why Choose Us", |
| 79 | + features: template.features |
| 80 | + } |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + if (pageKey === 'contact') { |
| 85 | + content.push({ |
| 86 | + type: "HeadingBlock", |
| 87 | + props: { title: "Contact Us", align: "center", size: "xl" } |
| 88 | + }); |
| 89 | + content.push({ |
| 90 | + type: "TextBlock", |
| 91 | + props: { content: "Get in touch with our team at support@ledger.ai", align: "center" } |
| 92 | + }); |
| 93 | + // Add a mockup form via CodeBlock for now |
| 94 | + content.push({ |
| 95 | + type: "CodeBlock", |
| 96 | + props: { code: '<form class="max-w-md mx-auto space-y-4 text-white"><input type="email" placeholder="Email" class="w-full p-2 rounded bg-white/10 border border-white/20" /><button class="w-full bg-indigo-600 p-2 rounded">Send</button></form>' } |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + if (pageKey === 'pricing') { |
| 101 | + content.push({ |
| 102 | + type: "PricingBlock", |
| 103 | + props: template.plans[1] // Add the Pro plan |
| 104 | + }); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Create or Update Page in DB |
| 109 | + const page = await prismadb.landingPage.upsert({ |
| 110 | + where: { slug: slug }, |
| 111 | + update: { |
| 112 | + content: { content, root: { props: { title: data.brandName + " - " + title } } }, |
| 113 | + title: `${data.brandName} - ${title}`, |
| 114 | + description: `Generated ${title} page for ${data.brandName}` |
| 115 | + }, |
| 116 | + create: { |
| 117 | + slug: slug, |
| 118 | + title: `${data.brandName} - ${title}`, |
| 119 | + description: `Generated ${title} page for ${data.brandName}`, |
| 120 | + content: { content, root: { props: { title: data.brandName + " - " + title } } } |
| 121 | + } |
| 122 | + }); |
| 123 | + createdPages.push(page); |
| 124 | + } |
| 125 | + |
| 126 | + await logActivity("AI Site Generation", "Genius Mode", `Generated ${createdPages.length} pages for brand: ${data.brandName}`); |
| 127 | + |
| 128 | + revalidatePath("/cms/landing"); |
| 129 | + return { success: true, pages: createdPages }; |
| 130 | + |
| 131 | + } catch (error) { |
| 132 | + console.error("Genius Mode Error:", error); |
| 133 | + return { success: false, error: "Failed to generate site." }; |
| 134 | + } |
| 135 | +} |
0 commit comments