import { NextRequest, NextResponse } from "next/server";
import { requireAdmin } from "@/lib/api-auth";
import { sendEmail, verifyEmailHtml } from "@/lib/email";

export async function POST(req: NextRequest) {
  const { response } = await requireAdmin();
  if (response) return response;

  const { to } = await req.json() as { to: string };
  if (!to) return NextResponse.json({ error: "Missing 'to'" }, { status: 400 });

  const baseUrl = process.env["BETTER_AUTH_URL"];
  if (!baseUrl) {
    return NextResponse.json({ error: "BETTER_AUTH_URL is not configured" }, { status: 500 });
  }

  try {
    await sendEmail({
      to,
      subject: "Epicure — test email",
      html: verifyEmailHtml(`${baseUrl}/verify-email?token=test`),
    });
    return NextResponse.json({ ok: true });
  } catch (err) {
    return NextResponse.json({ error: String(err) }, { status: 500 });
  }
}
