import { NextResponse, type NextRequest } from "next/server";
import { prisma } from "@/lib/db";
const SocialPostStatus = {
  DRAFT: "DRAFT",
  SCHEDULED: "SCHEDULED",
  PUBLISHED: "PUBLISHED",
  FAILED: "FAILED",
} as const;
import { getStaffSession } from "@/lib/staff-session";

const ADMIN_ROLES = ["ADMIN", "SUPER_ADMIN"];

export async function POST(request: NextRequest) {
  const user = await getStaffSession();
  if (!user || !ADMIN_ROLES.includes(user.role)) {
    return NextResponse.json(
      { error: "Admin role required to publish" },
      { status: 403 },
    );
  }

  const body = await request.json();
  const { id } = body;

  if (!id) {
    return NextResponse.json({ error: "id is required" }, { status: 400 });
  }

  const post = await prisma.social_posts.findUnique({ where: { id } });
  if (!post) {
    return NextResponse.json(
      { error: "Social post not found" },
      { status: 404 },
    );
  }

  const record = await prisma.social_posts.update({
    where: { id },
    data: {
      status: SocialPostStatus.PUBLISHED,
      publishedAt: new Date(),
      updatedAt: new Date(),
      updatedBy: user.id,
    },
  });

  return NextResponse.json({ data: record });
}
