module EhXero
  module Mappers
    class Payslip
      extend Base

      class << self
        def fields_mapping
          {
            payslip_id: :external_id,
            deductions: :total_deduction,
            net_pay: :net_pay,
            super: :super,
            wages: :wages,
            reimbursements: :reimbursements,
            tax: :tax,
            first_name: :first_name,
            last_name: :last_name
          }
        end

        def to_eh(input_attrs, payrun_attrs, organisation_attrs, member_attrs, deduction_types)
          attrs = super(input_attrs)
          earnings_lines         = input_attrs[:earnings_lines] + input_attrs[:leave_earnings_lines] + input_attrs[:timesheet_earnings_lines]
          pay_categories_attrs   = organisation_attrs[:pay_categories]
          leave_categories_attrs = organisation_attrs[:leave_categories]

          attrs[:name]                            = "#{input_attrs[:first_name]} #{input_attrs[:last_name]}"
          attrs[:total_hours]                     = input_attrs[:earnings_lines].map {|line| line[:number_of_units]}.compact.inject(:+)
          attrs[:earnings_lines_attributes]       = earnings_lines.map {|line| Mappers::Payslips::PayslipEarningsLine.to_eh(line, pay_categories_attrs) }
          attrs[:accrued_leaves_attributes]       = input_attrs[:leave_accrual_lines].map {|line| Mappers::Payslips::LeaveAccruedLine.to_eh(line, leave_categories_attrs) }
          attrs[:deductions_attributes]           = input_attrs[:deduction_lines].map {|line| Mappers::Payslips::DeductionLine.to_eh(line, deduction_types) }
          attrs[:superannuation_lines_attributes] = input_attrs[:superannuation_lines].map {|line| Mappers::Payslips::SuperannuationLine.to_eh(line) }
          attrs[:tax_lines_attributes]            = input_attrs[:tax_lines].map {|line| Mappers::Payslips::TaxLine.to_eh(line) }
          attrs[:reimbursement_lines_attributes]  = input_attrs[:reimbursement_lines].map {|line| Mappers::Payslips::ReimbursementLine.to_eh(line) }
          attrs[:taxable_earnings]                = input_attrs[:tax]

          attrs[:pay_period_starting]             = payrun_attrs[:period_start_date]
          attrs[:pay_period_ending]               = payrun_attrs[:period_end_date]
          attrs[:business_name]                   = organisation_attrs[:name]
          attrs[:business_address]                = organisation_attrs[:displayed_address]
          attrs[:business_abn]                    = organisation_attrs[:abn]
          attrs[:member_id]                       = member_attrs[:id]
          attrs[:employment_type]                 = member_attrs[:employment_type]

          if member_attrs[:address]
            attrs[:address_line1]                 = member_attrs[:address][:line_1]
            attrs[:address_line2]                 = member_attrs[:address][:line_2]
            attrs[:postcode]                      = member_attrs[:address][:postcode]
            attrs[:address_state]                 = member_attrs[:address][:state]
            attrs[:suburb]                        = member_attrs[:address][:city]
          end
          attrs
        end
      end
    end
  end
end
