import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/db';

export async function GET(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 });
    }

    const trees = await prisma.reminderTree.findMany({
      where: { clinicId: clinic.id },
      include: {
        _count: {
          select: {
            nodes: true,
            userJourneys: true
          }
        }
      },
      orderBy: { createdAt: 'desc' }
    });

    return NextResponse.json(trees);
  } catch (error) {
    const msg = error instanceof Error ? error.message : String(error);
    console.error('Failed to fetch reminder trees:', msg);
    return NextResponse.json(
      { error: msg }, 
      { status: 500 }
    );
  }
}