export const dynamic = 'force-dynamic';

import { NextResponse } from 'next/server';
import { getSql } from '../../../../lib/db.js';
import { getOrgId } from '../../../../lib/org.js';
import { apiErrorResponse } from '../../../../lib/apiErrors.js';
import { listArtifacts } from '../../../../lib/repositories/artifacts.repository.js';

export async function GET(request, { params }) {
  try {
    const { actionId } = await params;
    const sql = getSql();
    const orgId = getOrgId(request);
    const { searchParams } = new URL(request.url);

    const result = await listArtifacts(sql, orgId, {
      action_id: actionId,
      limit: searchParams.get('limit') || 50,
      offset: searchParams.get('offset') || 0,
    });

    return NextResponse.json(result);
  } catch (error) {
    return apiErrorResponse(error, 'ACTION_ARTIFACTS');
  }
}
