require "conduit/suretax/parsers/base"

module Conduit::Driver::Suretax
  class QueryTax::Parser < Parser::Base
    attribute :total_tax do
      BigDecimal(@suretax_response.total_tax.to_f || 0, 6)
    end

    attribute :taxes do
      taxes = @suretax_response.groups.map do |tax_group|
        group_attr = {
          customer_id:    tax_group.customer,
          invoice_id:     tax_group.invoice,
          state_code:     tax_group.state,
          line:           tax_group.line
        }

        tax_group.taxes.map do |tax_item|
          group_attr.merge(
            tax_code:     tax_item.code,
            description:  tax_item.description,
            city:         tax_item.city,
            county:       tax_item.county,
            amount:       BigDecimal(tax_item.amount.to_f, 6),
            revenue:      BigDecimal(tax_item.revenue.to_f, 6),
            revenue_base: BigDecimal(tax_item.revenue_base.to_f, 6),
            rate:         BigDecimal(tax_item.rate.to_f, 6),
            taxable:      BigDecimal(tax_item.taxable.to_f, 6),
            fee_rate:     BigDecimal(tax_item.fee_rate.to_f, 6),
            tax_on_tax:   BigDecimal(tax_item.tax_on_tax.to_f, 6)
          )
        end
      end.flatten

      taxes.map { |tax| OpenStruct.new(tax) }
    end
  end
end
