import { NextResponse } from "next/server";
import { db } from "@/lib/db";
import { cookies } from "next/headers";
import { verifyToken } from "@/lib/auth";

export async function GET() {
  try {
    const cookieStore = await cookies();
    const token = cookieStore.get("token")?.value;
    const decoded: any = verifyToken(token || "");

    if (!decoded) return NextResponse.json({ unreadCount: 0 });

    // Contamos cuántas campañas tiene asignadas que no ha leído
    const [rows]: any = await db.query(
      "SELECT COUNT(*) as total FROM campaign_tracking WHERE user_id = ? AND is_read = 0",
      [decoded.id]
    );

    return NextResponse.json({ unreadCount: rows[0].total });
  } catch (error) {
    return NextResponse.json({ unreadCount: 0 });
  }
}