/**
 * Pure mapping for the reporting scenario combobox. Lives apart from the db
 * function so the row-shaping (including the string|Date timestamp quirk of
 * raw db.execute) is unit-tested without a database.
 */

export interface ReportingScenario {
  id: number;
  name: string;
  goal: string;
  goalValue: string | null;
  budget: number | null;
  housingType: string;
  createdAt: string;
  status: "modelled" | "awaiting_modelling";
}

export interface RawScenarioRow {
  id: string | number | bigint;
  name: string | null;
  goal: string;
  goal_value: string | null;
  budget: number | null;
  housing_type: string;
  created_at: string | Date;
}

/** Raw db.execute returns created_at as a string; Drizzle .select() as a Date. */
function toIso(value: string | Date): string {
  return value instanceof Date ? value.toISOString() : value;
}

export function toReportingScenario(
  row: RawScenarioRow,
  modelledIds: Set<string>,
): ReportingScenario {
  return {
    id: Number(row.id),
    name: row.name ?? `Scenario ${row.id}`,
    goal: row.goal,
    goalValue: row.goal_value,
    budget: row.budget,
    housingType: row.housing_type,
    createdAt: toIso(row.created_at),
    status: modelledIds.has(String(row.id)) ? "modelled" : "awaiting_modelling",
  };
}
