import { PayPeriods } from "prisma/postgres/generated/prisma/client/postgres/client";
import { PayPeriodDto } from "src/time-and-attendance/pay-period/dtos/overview-pay-period.dto";

const toDateString = (date: Date) => date.toISOString().slice(0, 10); // "YYYY-MM-DD"

export function mapPayPeriodToDto(row: PayPeriods): PayPeriodDto {
  const start = toDateString(row.period_start);
  const end = toDateString(row.period_end);
  const pay = toDateString(row.payday);
  return {
    pay_period_no: row.pay_period_no,
    period_start: toDateString(row.period_start),
    period_end: toDateString(row.period_end),
    payday:pay,
    // pay_year: new Date(pay).getFullYear(),
    pay_year: row.pay_year,
    label: `${start}.${end}`,
  };
}

export function mapMany(rows: PayPeriods[]): PayPeriodDto[] {
  return rows.map(mapPayPeriodToDto);
}
