import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/db';
import { createAllDemoTrees } from '@/scripts/demo-reminder-trees';

export async function POST(request: NextRequest) {
  try {
    // Get the first clinic (for demo purposes)
    const clinic = await prisma.clinic.findFirst();
    if (!clinic) {
      return NextResponse.json({ error: 'No clinic found' }, { status: 404 });
    }

    // Check if demo trees already exist
    const existingTrees = await prisma.reminderTree.count({
      where: { clinicId: clinic.id }
    });

    if (existingTrees > 0) {
      return NextResponse.json(
        { error: 'Demo trees already exist for this clinic' }, 
        { status: 400 }
      );
    }

    // Create all demo trees
    await createAllDemoTrees(clinic.id);

    return NextResponse.json({ 
      success: true, 
      message: 'Demo trees created successfully' 
    });
  } catch (error) {
    console.error('Failed to create demo trees:', error);
    return NextResponse.json(
      { error: 'Failed to create demo trees: ' + (error as Error).message }, 
      { status: 500 }
    );
  }
}