# 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_members.members'))) do |sheet|
  ## data table
  # heading labels
  columns = [t('export_members.id'),
             t('export_members.external_id'),
             t('export_members.surname'),
             t('export_members.first_name'),
             t('export_members.email'),
             t('export_members.newsletter'),
             t('export_members.last_login'),
             t('export_members.gender'),
             t('export_members.age'),
             t('export_members.address'),
             t('export_members.phone'),
             t('export_members.website'),
             t('export_members.job'),
             t('export_members.interests'),
             t('export_members.cad_software_mastered'),
             t('export_members.group'),
             t('export_members.subscription'),
             t('export_members.subscription_end_date'),
             t('export_members.validated_trainings'),
             t('export_members.tags'),
             t('export_members.number_of_invoices'),
             t('export_members.projects'),
             t('export_members.facebook'),
             t('export_members.twitter'),
             t('export_members.echo_sciences'),
             t('export_members.organization'),
             t('export_members.organization_address'),
             t('export_members.note')]
  sheet.add_row columns, style: header

  # data rows
  members.each do |member|
    expiration = member&.subscription&.expired_at
    data = [
      member.id,
      member.invoicing_profile.external_id || '',
      member.profile.last_name,
      member.profile.first_name,
      member.email,
      member.is_allow_newsletter,
      member.last_sign_in_at&.to_date,
      member.statistic_profile.gender ? t('export_members.man') : t('export_members.woman'),
      member.statistic_profile.age,
      member.invoicing_profile&.address&.address || '',
      member.profile.phone,
      member.profile.website,
      member.profile.job,
      member.profile.interest,
      member.profile.software_mastered,
      member.group&.name,
      expiration && DateTime.current < expiration ? member.subscription.plan.name : t('export_members.without_subscriptions'),
      expiration && DateTime.current < expiration ? member.subscription.expired_at.to_date : nil,
      member.trainings.map(&:name).join("\n"),
      member.tags.map(&:name).join("\n"),
      member.invoices.size,
      member.projects.map(&:name).join("\n"),
      member.profile.facebook || '',
      member.profile.twitter || '',
      member.profile.echosciences || '',
      member.invoicing_profile&.organization&.name || '',
      member.invoicing_profile&.organization&.address&.address || '',
      ActionController::Base.helpers.sanitize(member.profile.note, tags: []) || ''
    ]
    styles = [nil, nil, nil, nil, nil, nil, date, nil, nil, nil, nil, nil, nil, nil,
              nil, nil, nil, date, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]

    types = %i[integer string string string string boolean date string integer string string string string string
               string string string date string string integer string string string string string string string string]

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