# frozen_string_literal: true

wb = xlsx_package.workbook

header = wb.styles.add_style b: true, bg_color: Stylesheet.primary.upcase.gsub('#', 'FF'), fg_color: 'FFFFFFFF'
date = wb.styles.add_style format_code: Rails.application.secrets.excel_date_format

wb.add_worksheet(name: ExcelService.name_safe(t('export_reservations.reservations'))) do |sheet|

  ## data table
  # heading labels
  columns = [t('export_reservations.customer_id'), t('export_reservations.customer'), t('export_reservations.email'),
             t('export_reservations.reservation_date'), t('export_reservations.reservation_type'), t('export_reservations.reservation_object'),
             t('export_reservations.slots_number_hours_tickets'), t('export_reservations.payment_method'), t('export_reservations.coupon')]
  sheet.add_row columns, style: header

  # data rows
  reservations.each do |resrv|
    invoice = resrv.original_invoice
    data = [
      resrv.user&.id,
      resrv.user&.profile&.full_name || t('export_reservations.deleted_user'),
      resrv.user&.email,
      resrv.created_at.to_date,
      resrv.reservable_type,
      resrv.reservable.nil? ? '' : resrv.reservable.name,
      resrv.reservable_type == 'Event' ? resrv.total_booked_seats : resrv.slots.count,
      invoice&.paid_by_card? ? t('export_reservations.online_payment') : t('export_reservations.local_payment'),
      invoice&.coupon&.nil? ? '' : invoice&.coupon&.name
    ]
    styles = [nil, nil, nil, date, nil, nil, nil, nil]
    types = %i[integer string string date string string integer string]

    sheet.add_row data, style: styles, types: types
  end
end
