import { NextRequest, NextResponse } from "next/server"; import ZAI from "z-ai-web-dev-sdk"; import { db } from "@/lib/db"; const FAMOUS_INFLUENCERS = [ { name: "Lil Miquela", handle: "@lilmiquela", platform: "Instagram", followers: 2500000, engagement: 3.2, niche: "Fashion", petCompanion: false, lessons: ["Consistencia visual", "Narrativa creible"] }, { name: "Rozy", handle: "@rozy.gram", platform: "Instagram", followers: 180000, engagement: 4.5, niche: "K-pop", petCompanion: true, petType: "dog", lessons: ["Tendencias K-pop", "Mascota como diferenciador"] }, { name: "Imma", handle: "@imma.gram", platform: "Instagram", followers: 390000, engagement: 3.8, niche: "Fashion & Art", petCompanion: false, lessons: ["Fusionar arte y moda"] }, { name: "Ayla", handle: "@ayla_virtual", platform: "TikTok", followers: 850000, engagement: 8.3, niche: "Dance", petCompanion: true, petType: "dog", lessons: ["Participar en trends", "Alta frecuencia"] } ]; export async function GET(request: NextRequest) { try { const { searchParams } = new URL(request.url); const niche = searchParams.get("niche"); const withPets = searchParams.get("withPets"); let influencers = [...FAMOUS_INFLUENCERS]; if (niche) influencers = influencers.filter(i => i.niche.toLowerCase().includes(niche.toLowerCase())); if (withPets === "true") influencers = influencers.filter(i => i.petCompanion); const dbInfluencers = await db.aIInfluencer.findMany({ where: { isActive: true } }); return NextResponse.json({ success: true, influencers, customInfluencers: dbInfluencers }); } catch { return NextResponse.json({ success: false, error: "Error" }, { status: 500 }); } } export async function POST(request: NextRequest) { try { const body = await request.json(); const { targetNiche, includePets } = body; const zai = await ZAI.create(); const completion = await zai.chat.completions.create({ messages: [ { role: "system", content: "Eres experto en marketing de influencers. Responde JSON con {contentPatterns:[], visualStyles:[], recommendations:[]}" }, { role: "user", content: "Analiza estrategias para nicho: " + (targetNiche || "general") } ] }); let analysis: Record = {}; try { const match = completion.choices[0]?.message?.content?.match(/\{[\s\S]*\}/); if (match) analysis = JSON.parse(match[0]); } catch {} return NextResponse.json({ success: true, influencers: FAMOUS_INFLUENCERS.slice(0, 4), analysis }); } catch { return NextResponse.json({ success: false, error: "Error" }, { status: 500 }); } } export async function PUT(request: NextRequest) { try { const body = await request.json(); const { name, handle, platform, followers, engagement, niche, petCompanion, petType } = body; const influencer = await db.aIInfluencer.create({ data: { name, handle: handle || null, platform, followers: followers || null, engagement: engagement || null, niche: niche || null, petCompanion: petCompanion || false, petType: petType || null } }); return NextResponse.json({ success: true, influencer }); } catch { return NextResponse.json({ success: false, error: "Error" }, { status: 500 }); } }