export const dynamic = 'force-dynamic';
export const revalidate = 0;

import { NextResponse } from 'next/server';
import { getSql } from '../../../lib/db.js';
import { getOrgId } from '../../../lib/org.js';
import { computeRoiFromRows } from '../../../lib/claude-code/subagent-roi.js';
import { listSubagentToolUseAttribution } from '../../../lib/repositories/code-sessions.repository.js';

export async function GET(request) {
  const url = new URL(request.url);
  const projectId = url.searchParams.get('project_id');
  const sql = getSql();
  const orgId = getOrgId(request);

  // The raw SQL lives in the repository per the route-level SQL guardrail; the
  // filter + ROI computation is the shared helper the project-page UI uses too.
  const rows = await listSubagentToolUseAttribution(sql, orgId, { projectId });
  return NextResponse.json({ project_id: projectId, roi: computeRoiFromRows(rows) });
}
