module ClubBillingProfile
  extend ActiveSupport::Concern

  BILLING_ENTITY_TYPES = {
    "company" => "Società / ASD con P.IVA",
    "individual" => "Persona fisica",
    "nonprofit" => "Associazione senza scopo di lucro"
  }.freeze

  included do
    validates :billing_entity_type, inclusion: { in: BILLING_ENTITY_TYPES.keys }, allow_nil: true
    validates :billing_email, format: { with: URI::MailTo::EMAIL_REGEXP }, allow_blank: true
    validates :billing_recipient_code, length: { is: 7 }, allow_blank: true
    validates :billing_province, length: { is: 2 }, allow_blank: true
    validates :billing_country, length: { is: 2 }, allow_blank: true
    validate :billing_profile_for_invoicing, on: :billing_profile
  end

  def billing_profile_complete?
    billing_profile_errors.empty?
  end

  # Campi minimi per intestazione fattura e invio (email PDF + SDI/PEC).
  def billing_profile_errors
    errors = []
    errors << "Tipo intestatario" if billing_entity_type.blank?
    errors << "Ragione sociale o nome intestatario" if billing_legal_name.blank?
    errors << "Email di fatturazione" if billing_email.blank?
    errors << "Indirizzo" if billing_address_line.blank?
    errors << "Città" if billing_city.blank?
    errors << "Provincia (sigla 2 lettere)" if billing_province.blank? || billing_province.to_s.length != 2
    errors << "CAP" if billing_postal_code.blank?
    errors << "Paese (ISO)" if billing_country.blank? || billing_country.to_s.length != 2
    errors.concat(billing_tax_id_errors)
    errors << "Codice destinatario SDI (7 caratteri) o PEC" if billing_recipient_code.blank? && billing_pec.blank?
    errors << "Codice destinatario SDI (7 caratteri)" if billing_recipient_code.present? && billing_recipient_code.length != 7
    errors
  end

  def billing_tax_id_errors
    case billing_entity_type
    when "company"
      billing_vat_number.blank? ? ["Partita IVA"] : []
    when "individual"
      billing_fiscal_code.blank? ? ["Codice Fiscale"] : []
    else
      billing_vat_number.blank? && billing_fiscal_code.blank? ? ["P.IVA o Codice Fiscale"] : []
    end
  end

  def billing_profile_summary
    parts = [billing_legal_name.presence, billing_vat_number.presence && "P.IVA #{billing_vat_number}"]
    parts.compact.join(" · ")
  end

  # Righe per intestazione fattura in admin (etichetta, valore).
  def billing_profile_invoice_lines
    lines = []
    lines << ["Tipo", BILLING_ENTITY_TYPES[billing_entity_type]] if billing_entity_type.present?
    lines << ["Intestatario", billing_legal_name]
    lines << ["P.IVA", billing_vat_number] if billing_vat_number.present?
    lines << ["Codice fiscale", billing_fiscal_code] if billing_fiscal_code.present?
    lines << ["Email fatturazione", billing_email]
    lines << ["Telefono", billing_phone] if billing_phone.present?
    addr = [billing_address_line, billing_postal_code, billing_city, billing_province, billing_country].compact.join(", ")
    lines << ["Indirizzo", addr] if addr.present?
    lines << ["SDI", billing_recipient_code] if billing_recipient_code.present?
    lines << ["PEC", billing_pec] if billing_pec.present?
    lines
  end

  private

  def billing_profile_for_invoicing
    billing_profile_errors.each do |message|
      errors.add(:base, message)
    end
  end
end
