|
| 1 | +"use server"; |
| 2 | +import { NextResponse } from "next/server"; |
| 3 | +import { createClient } from "@/lib/supabase/server"; |
| 4 | + |
| 5 | +const CATEGORY_VARIANTS: Record<string, { names: string[]; colors: string[] }> = { |
| 6 | + lipstick: { |
| 7 | + names: ["Ruby Red", "Rose Nude", "Coral Bliss", "Berry Kiss", "Mauve Dream", "Classic Red", "Pink Petal", "Dusty Rose"], |
| 8 | + colors: ["#C0392B", "#E8B4B8", "#FF7F50", "#8E44AD", "#D4A5A5", "#E74C3C", "#FFB6C1", "#C9A0A0"], |
| 9 | + }, |
| 10 | + foundation: { |
| 11 | + names: ["Ivory 01", "Beige 02", "Sand 03", "Honey 04", "Caramel 05", "Mocha 06", "Espresso 07", "Porcelain 00"], |
| 12 | + colors: ["#FFF8F0", "#F5E6D3", "#E8C9A0", "#D4A574", "#C4854A", "#A0522D", "#6B3A2A", "#FDF3E7"], |
| 13 | + }, |
| 14 | + eyeshadow: { |
| 15 | + names: ["Champagne", "Rose Gold", "Smoky Quartz", "Midnight", "Bronze", "Lilac", "Copper", "Pearl"], |
| 16 | + colors: ["#F7E7CE", "#B76E79", "#7B7B7B", "#1A1A2E", "#CD7F32", "#C8A2C8", "#B87333", "#F0EAD6"], |
| 17 | + }, |
| 18 | + blush: { |
| 19 | + names: ["Peachy Keen", "Rose Flush", "Berry Bliss", "Golden Hour", "Coral Glow", "Nude Flush"], |
| 20 | + colors: ["#FFCBA4", "#FFB7C5", "#CC5F8F", "#E8B86D", "#FF6B6B", "#E8B4A0"], |
| 21 | + }, |
| 22 | + default: { |
| 23 | + names: ["Shade 01", "Shade 02", "Shade 03", "Shade 04", "Shade 05", "Shade 06"], |
| 24 | + colors: ["#E8D5C4", "#C4A882", "#A07850", "#785030", "#503010", "#8B6914"], |
| 25 | + }, |
| 26 | +}; |
| 27 | + |
| 28 | +function detectCategory(title: string): string { |
| 29 | + const t = title.toLowerCase(); |
| 30 | + if (t.includes("lip") || t.includes("lipstick") || t.includes("gloss") || t.includes("balm")) return "lipstick"; |
| 31 | + if (t.includes("foundation") || t.includes("concealer") || t.includes("bb") || t.includes("cc")) return "foundation"; |
| 32 | + if (t.includes("eye") || t.includes("shadow") || t.includes("liner")) return "eyeshadow"; |
| 33 | + if (t.includes("blush") || t.includes("bronzer") || t.includes("highlighter")) return "blush"; |
| 34 | + return "default"; |
| 35 | +} |
| 36 | + |
| 37 | +export async function POST(req: Request) { |
| 38 | + try { |
| 39 | + const supabase = await createClient(); |
| 40 | + const { data: { user } } = await supabase.auth.getUser(); |
| 41 | + if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); |
| 42 | + |
| 43 | + const { data: profile } = await supabase.from("profiles").select("role").eq("id", user.id).single(); |
| 44 | + if (profile?.role !== "admin") return NextResponse.json({ error: "Forbidden" }, { status: 403 }); |
| 45 | + |
| 46 | + const body = await req.json(); |
| 47 | + const { title, imageBase64, mode } = body; |
| 48 | + |
| 49 | + if (!title) return NextResponse.json({ error: "Title required" }, { status: 400 }); |
| 50 | + |
| 51 | + const apiKey = process.env.ANTHROPIC_API_KEY; |
| 52 | + if (!apiKey) return NextResponse.json({ error: "ANTHROPIC_API_KEY not configured in .env.local" }, { status: 500 }); |
| 53 | + |
| 54 | + const category = detectCategory(title); |
| 55 | + const variantData = CATEGORY_VARIANTS[category]; |
| 56 | + |
| 57 | + // Build messages for Claude |
| 58 | + const messages: any[] = []; |
| 59 | + const userContent: any[] = []; |
| 60 | + |
| 61 | + if (imageBase64) { |
| 62 | + userContent.push({ |
| 63 | + type: "image", |
| 64 | + source: { type: "base64", media_type: "image/jpeg", data: imageBase64 }, |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + const promptMap: Record<string, string> = { |
| 69 | + description: `You are a luxury beauty copywriter for DINA COSMETIC, a high-end beauty brand. |
| 70 | +Write a compelling product description for: "${title}" |
| 71 | +${imageBase64 ? "Use the product image to inform your description." : ""} |
| 72 | +
|
| 73 | +Return a JSON object with: |
| 74 | +{ |
| 75 | + "description": "2-3 sentence luxury product description (no markdown, plain text)", |
| 76 | + "shortDescription": "One punchy tagline under 10 words", |
| 77 | + "suggestedPrice": number (realistic USD price for luxury beauty), |
| 78 | + "keyBenefits": ["benefit 1", "benefit 2", "benefit 3"], |
| 79 | + "ingredients": "Key ingredient highlights (2-3 ingredients)", |
| 80 | + "howToUse": "Application instructions in 1-2 sentences" |
| 81 | +} |
| 82 | +
|
| 83 | +Write in the voice of Charlotte Tilbury or Dior Beauty. Sophisticated, sensual, confident. No generic phrases. |
| 84 | +Return ONLY the JSON object, no markdown.`, |
| 85 | + |
| 86 | + variants: `You are helping create product variants for a luxury beauty brand. |
| 87 | +Product: "${title}" |
| 88 | +Category detected: ${category} |
| 89 | +
|
| 90 | +Return a JSON object with: |
| 91 | +{ |
| 92 | + "variants": [ |
| 93 | + { "name": "Shade Name", "colorCode": "#hexcode", "suggestedPrice": number } |
| 94 | + ] |
| 95 | +} |
| 96 | +
|
| 97 | +Generate exactly 6 realistic shade/variant names appropriate for this product type. |
| 98 | +Use the following as inspiration but create unique names: ${variantData.names.join(", ")} |
| 99 | +Make names sound luxurious and evocative (e.g., "Midnight Noir" not "Dark Black"). |
| 100 | +Return ONLY the JSON object, no markdown.`, |
| 101 | + }; |
| 102 | + |
| 103 | + userContent.push({ type: "text", text: promptMap[mode] || promptMap.description }); |
| 104 | + messages.push({ role: "user", content: userContent }); |
| 105 | + |
| 106 | + const response = await fetch("https://api.anthropic.com/v1/messages", { |
| 107 | + method: "POST", |
| 108 | + headers: { |
| 109 | + "Content-Type": "application/json", |
| 110 | + "x-api-key": apiKey, |
| 111 | + "anthropic-version": "2023-06-01", |
| 112 | + }, |
| 113 | + body: JSON.stringify({ |
| 114 | + model: "claude-3-5-sonnet-20240620", |
| 115 | + max_tokens: 1024, |
| 116 | + messages, |
| 117 | + }), |
| 118 | + }); |
| 119 | + |
| 120 | + if (!response.ok) { |
| 121 | + const err = await response.text(); |
| 122 | + return NextResponse.json({ error: `Claude API error: ${err}` }, { status: 500 }); |
| 123 | + } |
| 124 | + |
| 125 | + const data = await response.json(); |
| 126 | + const text = data.content?.[0]?.text || "{}"; |
| 127 | + |
| 128 | + let parsed: any = {}; |
| 129 | + try { |
| 130 | + const clean = text.replace(/```json|```/g, "").trim(); |
| 131 | + parsed = JSON.parse(clean); |
| 132 | + } catch { |
| 133 | + parsed = { description: text }; |
| 134 | + } |
| 135 | + |
| 136 | + return NextResponse.json({ success: true, result: parsed, category }); |
| 137 | + } catch (err: any) { |
| 138 | + return NextResponse.json({ error: err.message }, { status: 500 }); |
| 139 | + } |
| 140 | +} |
0 commit comments