import { NextResponse } from "next/server";
import { buildCommercialSnapshot } from "@/lib/commercial";
import { readDb } from "@/lib/store";
import { hasSavedPinterestToken } from "@/lib/pinterest-api";
import { pinterestOAuthConfigured } from "@/lib/pinterest-oauth";

export async function GET() {
  const db = await readDb();
  return NextResponse.json({
    account: {
      ...db.account,
      oauthConfigured: pinterestOAuthConfigured(),
      tokenConfigured: await hasSavedPinterestToken(),
    },
    stats: {
      boards: db.boards.length,
      pinDrafts: db.pinDrafts.length,
      pendingReviewPins: db.pinDrafts.filter((pin) => pin.status === "pending_review").length,
      plans: db.operatorPlans.length,
      tasks: db.tasks.length,
      leads: db.leads.length,
    },
    recent: {
      plans: db.operatorPlans.slice(0, 5),
      pinDrafts: db.pinDrafts.slice(0, 5),
      tasks: db.tasks.slice(0, 5),
      auditLogs: db.auditLogs.slice(0, 5),
    },
    riskLimits: db.riskLimits,
    commercial: buildCommercialSnapshot(db),
  });
}
