import { NextResponse } from 'next/server';

const REMINDER_SVC = process.env.REMINDER_SERVICE_URL ||
  'http://reminder-service.ai-agents.svc.cluster.local:6001';

// GET /api/reminders  — list reminders (optional ?status=pending)
export async function GET(request) {
  try {
    const { searchParams } = new URL(request.url);
    const status = searchParams.get('status') || '';
    const url    = `${REMINDER_SVC}/reminders${status ? `?status=${status}` : ''}`;
    const res    = await fetch(url, { next: { revalidate: 0 } });
    const data   = await res.json();
    return NextResponse.json(data);
  } catch (err) {
    return NextResponse.json({ error: err.message }, { status: 500 });
  }
}

// POST /api/reminders/batch  — schedule multiple at once
export async function POST(request) {
  try {
    const body = await request.json();
    const res  = await fetch(`${REMINDER_SVC}/reminders/batch`, {
      method:  'POST',
      headers: { 'Content-Type': 'application/json' },
      body:    JSON.stringify(body),
    });
    const data = await res.json();
    return NextResponse.json(data, { status: res.ok ? 200 : res.status });
  } catch (err) {
    return NextResponse.json({ error: err.message }, { status: 500 });
  }
}

// DELETE /api/reminders?id=rem_xxx  — cancel a reminder
export async function DELETE(request) {
  try {
    const { searchParams } = new URL(request.url);
    const id  = searchParams.get('id');
    if (!id) return NextResponse.json({ error: 'id required' }, { status: 400 });
    const res  = await fetch(`${REMINDER_SVC}/reminders/${id}`, { method: 'DELETE' });
    const data = await res.json();
    return NextResponse.json(data, { status: res.ok ? 200 : res.status });
  } catch (err) {
    return NextResponse.json({ error: err.message }, { status: 500 });
  }
}
