import type { APIRoute } from 'astro';
import { getCollectionProducts } from '../../../lib/collections/collection-manager.js';

export const GET: APIRoute = async ({ params, url }) => {
  try {
    const slug = params.slug!;
    const page = parseInt(url.searchParams.get('page') || '1');
    const limit = parseInt(url.searchParams.get('limit') || '24');

    const result = await getCollectionProducts(slug, { page, limit });
    if (!result) {
      return new Response(JSON.stringify({ error: 'Collection not found' }), { status: 404 });
    }
    return new Response(JSON.stringify(result), {
      headers: { 'Content-Type': 'application/json' },
    });
  } catch (e: any) {
    return new Response(JSON.stringify({ error: e.message }), { status: 500 });
  }
};
