/**
 * POST   /api/watches/:id   { action: "cancel" } — stop an active watch.
 * DELETE /api/watches/:id   — remove the row.
 *
 * Writes go to workflow.db; the runner (chat-standalone) reads state each
 * tick, so a cancel here takes effect within one tick without IPC.
 */
import { NextResponse } from 'next/server';
import { cancelWatch, deleteWatch, getWatch } from '@/lib/watch/watch-store';

export async function POST(req: Request, { params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const body = await req.json().catch(() => ({}));
  const action = String(body?.action || 'cancel');
  if (action !== 'cancel') return NextResponse.json({ error: `unknown action "${action}"` }, { status: 400 });
  if (!getWatch(id)) return NextResponse.json({ error: 'watch not found' }, { status: 404 });
  const ok = cancelWatch(id, Date.now());
  return NextResponse.json({ ok, watch: getWatch(id) });
}

export async function DELETE(_req: Request, { params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const ok = deleteWatch(id);
  return NextResponse.json({ ok });
}
