import { addUtcDays, getLastNWeekStarts, toDateOnlyString } from "./dateBuckets.js";
const DEFAULT_WEEK_COUNT = 7;
async function computeProjectHoursTrend(scope) {
  const weekCount = scope.weekCount ?? DEFAULT_WEEK_COUNT;
  const now = scope.now ?? /* @__PURE__ */ new Date();
  const weekStarts = getLastNWeekStarts(weekCount, now);
  const oldestStart = weekStarts[0];
  const endExclusive = addUtcDays(weekStarts[weekStarts.length - 1], 7);
  const result = /* @__PURE__ */ new Map();
  for (const id of scope.projectIds) {
    result.set(id, { hoursWeek: 0, hoursTrend: new Array(weekCount).fill(0) });
  }
  if (scope.projectIds.length === 0) return result;
  const memberFilter = scope.staffMemberId ? "AND staff_member_id = ?" : "";
  const projectPlaceholders = scope.projectIds.map(() => "?").join(", ");
  const params = [
    scope.organizationId,
    scope.tenantId,
    ...scope.projectIds,
    toDateOnlyString(oldestStart),
    toDateOnlyString(endExclusive)
  ];
  if (scope.staffMemberId) params.push(scope.staffMemberId);
  const sql = `
    SELECT
      time_project_id AS project_id,
      date_trunc('week', date)::date AS week_start,
      COALESCE(SUM(duration_minutes), 0)::bigint AS total_minutes
    FROM staff_time_entries
    WHERE organization_id = ?
      AND tenant_id = ?
      AND time_project_id IN (${projectPlaceholders})
      AND deleted_at IS NULL
      AND date >= ?
      AND date < ?
      ${memberFilter}
    GROUP BY 1, 2
  `;
  const rows = await scope.em.getConnection().execute(sql, params);
  const weekIndex = /* @__PURE__ */ new Map();
  weekStarts.forEach((d, idx) => weekIndex.set(toDateOnlyString(d), idx));
  for (const row of rows) {
    const bucket = result.get(row.project_id);
    if (!bucket) continue;
    const weekKey = typeof row.week_start === "string" ? row.week_start.slice(0, 10) : toDateOnlyString(row.week_start);
    const idx = weekIndex.get(weekKey);
    if (idx === void 0) continue;
    const minutes = typeof row.total_minutes === "string" ? Number(row.total_minutes) : row.total_minutes;
    const hours = Math.round(minutes / 60 * 10) / 10;
    bucket.hoursTrend[idx] = hours;
  }
  for (const bucket of result.values()) {
    bucket.hoursWeek = bucket.hoursTrend[bucket.hoursTrend.length - 1] ?? 0;
  }
  return result;
}
export {
  computeProjectHoursTrend
};
//# sourceMappingURL=computeProjectHoursTrend.js.map
