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

export async function GET() {
  try {
    const projects = await db.photoProject.findMany({
      orderBy: { createdAt: 'desc' },
      include: {
        edits: {
          orderBy: { order: 'asc' },
          select: {
            id: true,
            order: true,
            toolType: true,
            toolLabel: true,
            prompt: true,
            originalPath: true,
            editedPath: true,
            createdAt: true,
          },
        },
      },
    });

    return NextResponse.json(projects);
  } catch (error: unknown) {
    const message = error instanceof Error ? error.message : 'خطأ في جلب المشاريع';
    return NextResponse.json({ error: message }, { status: 500 });
  }
}