import {
  formatCurrency,
  formatDecimal,
  formatInteger,
  formatPercent,
} from "./utils";

export function buildMetricCards(summary, options = {}) {
  const { isAllPropertiesSelected = false, referenceLabel = "" } = options;

  const totalExpenses = Number(summary.expenses ?? 0);
  const netBalance = Number(
    summary.net_revenue ??
      summary.net_balance_amount ??
      Number(summary.reserve_total ?? 0) -
        Number(summary.total_forward_fee_all ?? 0),
  );
  const grossRevenue = Number(summary.reserve_total ?? 0);
  const netRevenue = netBalance;
  const netPayout = Number(summary.final_payout_amount ?? 0);

  const occupancyCaption = isAllPropertiesSelected
    ? "percentual consolidado do período"
    : `${formatInteger(summary.occupied_nights_in_month)} de ${formatInteger(summary.days_in_month)} dias`;

  return [
    {
      label: "Faturamento Bruto",
      value: formatCurrency(grossRevenue),
      caption: referenceLabel || "Mês selecionado",
    },
    {
      label: "Faturamento Líquido",
      value: formatCurrency(netRevenue),
      caption: "Bruto - taxa OTA",
    },
    {
      label: "Diária Média",
      value: formatCurrency(summary.average_price_per_night),
      caption: "Bruto ÷ diárias",
    },
    {
      label: "Ticket Médio/Reserva",
      value: formatCurrency(summary.average_reservation_ticket),
      caption: isAllPropertiesSelected
        ? "Todos apartamentos"
        : "No imóvel selecionado",
    },
    {
      label: "Repasse Total",
      value: formatCurrency(netPayout),
      caption: isAllPropertiesSelected
        ? "Todos apartamentos"
        : "No imóvel selecionado",
    },
    {
      label: "Ocupação",
      value: formatPercent(summary.occupancy_rate, 1),
      caption: occupancyCaption,
    },
    {
      label: "Total de Reservas",
      value: formatInteger(summary.reservations_count),
      caption: "no período",
    },
    {
      label: "Dias por Reserva",
      value: formatDecimal(summary.average_nights_per_reservation),
      caption: "média de permanência",
    },
    {
      label: "Limpeza",
      value: formatInteger(summary.cleanings_count),
      caption: "total no período",
    },
    {
      label: "Despesas",
      value: formatCurrency(totalExpenses),
      caption: "manutenção e operação",
    },
  ];
}
