File size: 2,543 Bytes
333c51a
 
30ad7ef
333c51a
30ad7ef
 
 
 
 
 
333c51a
 
30ad7ef
 
 
333c51a
 
 
 
 
 
 
 
30ad7ef
 
 
333c51a
 
 
30ad7ef
 
 
333c51a
 
 
 
 
 
30ad7ef
333c51a
 
 
 
30ad7ef
 
 
333c51a
 
30ad7ef
333c51a
30ad7ef
 
 
333c51a
30ad7ef
 
 
333c51a
30ad7ef
 
 
333c51a
30ad7ef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { NextRequest, NextResponse } from "next/server";
import ZAI from "z-ai-web-dev-sdk";
import { db } from "@/lib/db";

const TRENDS = [
  { platform: "Instagram", name: "#aesthetic", growth: 15.5, category: "lifestyle" },
  { platform: "TikTok", name: "Storytime", growth: 32.1, category: "narrative" },
  { platform: "YouTube", name: "Shorts", growth: 25.6, category: "video" },
  { platform: "Instagram", name: "Pets content", growth: 55.8, category: "pets" }
];

const VIRAL_STRATEGIES = [
  { name: "Hook en 3 segundos", successRate: 85, platforms: ["tiktok", "instagram"] },
  { name: "Storytime con suspenso", successRate: 78, platforms: ["tiktok"] },
  { name: "Pet Reveal", successRate: 88, platforms: ["instagram", "tiktok"] }
];

export async function GET(request: NextRequest) {
  try {
    const { searchParams } = new URL(request.url);
    const platform = searchParams.get("platform");
    const includePets = searchParams.get("includePets") === "true";

    let trends = [...TRENDS];
    if (platform) trends = trends.filter(t => t.platform.toLowerCase() === platform.toLowerCase());
    if (includePets) trends = trends.filter(t => t.category === "pets");

    trends.sort((a, b) => b.growth - a.growth);

    return NextResponse.json({ success: true, trends, viralStrategies: VIRAL_STRATEGIES });
  } catch {
    return NextResponse.json({ success: false, error: "Error" }, { status: 500 });
  }
}

export async function POST(request: NextRequest) {
  try {
    const body = await request.json();
    const { niche, platform } = body;

    const zai = await ZAI.create();
    const completion = await zai.chat.completions.create({
      messages: [
        { role: "system", content: "Eres analista de tendencias. Responde JSON con {currentTrends:[], recommendations:[], predictedViralPotential:0-100}" },
        { role: "user", content: "Analiza tendencias para nicho: " + (niche || "general") + " plataforma: " + (platform || "todas") }
      ]
    });

    let analysis: Record<string, unknown> = {};
    try {
      const match = completion.choices[0]?.message?.content?.match(/\{[\s\S]*\}/);
      if (match) analysis = JSON.parse(match[0]);
    } catch {}

    await db.trend.create({
      data: { platform: platform || "all", type: "analysis", name: "Analisis " + (niche || "general"), contentIdeas: JSON.stringify(analysis) }
    }).catch(() => {});

    return NextResponse.json({ success: true, analysis });
  } catch {
    return NextResponse.json({ success: false, error: "Error" }, { status: 500 });
  }
}