/**
 * GET /api/social/calendar/upcoming
 * Get next N upcoming scheduled posts
 */

import { NextRequest, NextResponse } from "next/server";
import { auth } from "@/lib/auth";
import { SocialCalendarService } from "@/lib/services/social-calendar.service";
import { logger } from "@/lib/logger";

export async function GET(request: NextRequest) {
  try {
    const session = await auth();
    if (!session?.user?.id) {
      return NextResponse.json(
        { success: false, error: "Unauthorized" },
        { status: 401 },
      );
    }

    const limit = parseInt(
      request.nextUrl.searchParams.get("limit") || "20",
      10,
    );

    const items = await SocialCalendarService.getUpcoming(
      session.user.id,
      Math.min(limit, 50),
    );

    return NextResponse.json({ items, success: true });
  } catch (error) {
    logger.error("Failed to get upcoming posts", { error });
    return NextResponse.json(
      { success: false, error: "Failed to get upcoming" },
      { status: 500 },
    );
  }
}
