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 { getCurrentPeriod } from '../../../lib/usage.js';

export async function GET(request) {
  try {
    const sql = getSql();
    const orgId = getOrgId(request);
    const { searchParams } = new URL(request.url);
    const period = searchParams.get('period') || getCurrentPeriod();

    const periodStart = `${period}-01T00:00:00Z`;
    const [year, month] = period.split('-').map(Number);
    const nextMonth = month === 12 ? `${year + 1}-01` : `${year}-${String(month + 1).padStart(2, '0')}`;
    const periodEnd = `${nextMonth}-01T00:00:00Z`;

    const breakdown = await sql`
      SELECT action_type, COUNT(*)::int AS count,
        COALESCE(SUM(cost_estimate), 0)::real AS cost_usd
      FROM action_records
      WHERE org_id = ${orgId}
        AND timestamp_start >= ${periodStart}
        AND timestamp_start < ${periodEnd}
        AND cost_estimate > 0
      GROUP BY action_type ORDER BY cost_usd DESC
    `;

    const daily = await sql`
      SELECT DATE(timestamp_start) AS date, COUNT(*)::int AS actions,
        COALESCE(SUM(cost_estimate), 0)::real AS cost_usd
      FROM action_records
      WHERE org_id = ${orgId}
        AND timestamp_start >= ${periodStart}
        AND timestamp_start < ${periodEnd}
      GROUP BY DATE(timestamp_start) ORDER BY date
    `;

    const totals = await sql`
      SELECT COUNT(*)::int AS total_actions,
        COALESCE(SUM(cost_estimate), 0)::real AS total_cost_usd
      FROM action_records
      WHERE org_id = ${orgId}
        AND timestamp_start >= ${periodStart}
        AND timestamp_start < ${periodEnd}
    `;

    const breakdownMap = {};
    for (const row of breakdown) {
      breakdownMap[row.action_type] = {
        count: row.count,
        cost_usd: Math.round(row.cost_usd * 1000) / 1000,
      };
    }

    return NextResponse.json({
      period,
      total_cost_usd: Math.round((totals[0]?.total_cost_usd || 0) * 1000) / 1000,
      total_actions: totals[0]?.total_actions || 0,
      breakdown: breakdownMap,
      daily: daily.map((d) => ({
        date: d.date,
        actions: d.actions,
        cost_usd: Math.round(d.cost_usd * 1000) / 1000,
      })),
    });
  } catch (error) {
    return apiErrorResponse(error, 'USAGE_COSTS');
  }
}
