import { NextResponse } from 'next/server';
import { getPrompt, updatePrompt, deletePrompt } from '@/lib/prompts/store';

// GET /api/prompts/[name]
export async function GET(_req: Request, { params }: { params: Promise<{ name: string }> }) {
  const { name } = await params;
  const p = getPrompt(name);
  if (!p) return NextResponse.json({ error: 'not found' }, { status: 404 });
  return NextResponse.json(p);
}

// PATCH /api/prompts/[name]
//   body: { prompt?, executor?: { agent?, skills?, config? } }
export async function PATCH(req: Request, { params }: { params: Promise<{ name: string }> }) {
  const { name } = await params;
  let body: any;
  try {
    body = await req.json();
  } catch {
    return NextResponse.json({ error: 'invalid json body' }, { status: 400 });
  }
  try {
    const p = updatePrompt(name, body);
    return NextResponse.json(p);
  } catch (e: any) {
    const notFound = String(e?.message || '').includes('not found');
    return NextResponse.json({ error: e.message }, { status: notFound ? 404 : 400 });
  }
}

// DELETE /api/prompts/[name]
export async function DELETE(_req: Request, { params }: { params: Promise<{ name: string }> }) {
  const { name } = await params;
  return deletePrompt(name)
    ? NextResponse.json({ ok: true })
    : NextResponse.json({ error: 'not found' }, { status: 404 });
}
