import { NextResponse } from "next/server";
import { apiHandler, ApiError } from "@/lib/api";
import { refinePlan } from "@/lib/ai";
import { getAuthUserId } from "@/lib/auth";

export const POST = apiHandler(async (req) => {
  await getAuthUserId(req);
  const { instruction, days } = await req.json();
  if (!instruction?.trim()) throw new ApiError("指令不能为空", 400);
  if (!Array.isArray(days) || days.length === 0) throw new ApiError("days 无效", 400);

  const newDays = await refinePlan(days, instruction);
  if (!newDays) throw new ApiError("AI 调整失败，请重试", 500);
  return NextResponse.json({ days: newDays });
});
