import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';
import { createClient } from '@/lib/auth/server';
import { isMockAppointment } from '@/lib/cortex/mock-data/noshow';

const CancelNoShowSchema = z.object({
  appointmentId: z.string().min(1, 'appointmentId is verplicht'),
  patientId: z.string().min(1, 'patientId is verplicht'),
});

export async function POST(request: NextRequest) {
  const supabase = await createClient();
  const { data: { user }, error: authError } = await supabase.auth.getUser();
  if (authError || !user) {
    return NextResponse.json({ error: 'Niet geautoriseerd' }, { status: 401 });
  }

  let body: z.infer<typeof CancelNoShowSchema>;
  try {
    body = CancelNoShowSchema.parse(await request.json());
  } catch {
    return NextResponse.json({ error: 'Ongeldige invoer' }, { status: 400 });
  }

  const { appointmentId, patientId } = body;

  // Mock path — demo flow
  if (isMockAppointment(appointmentId)) {
    return NextResponse.json({
      success: true,
      appointmentId,
      newStatus: 'cancelled_no_show',
      message: 'Afspraak geregistreerd als no show (demo)',
    });
  }

  // Productie path — 'cancelled' is de dichtstbijzijnde geldige DB status
  // In productie zou je een aparte no_show kolom of notitieveld gebruiken
  const { error } = await supabase
    .from('encounters')
    .update({ status: 'cancelled' as 'cancelled' })
    .eq('id', appointmentId)
    .eq('patient_id', patientId);

  if (error) {
    console.error('[noshow/cancel] DB error:', error);
    return NextResponse.json({ error: 'Annuleren mislukt' }, { status: 500 });
  }

  return NextResponse.json({ success: true, appointmentId, newStatus: 'cancelled_no_show' });
}
