FactoryBot.define do
  factory :order_with_totals_and_distribution, parent: :order_with_distributor do
    transient do
      shipping_fee 3
    end

    order_cycle { create(:simple_order_cycle) }

    after(:create) do |order, proxy|
      product = create(:simple_product)
      create(:line_item_with_shipment, shipping_fee: proxy.shipping_fee,
                                       order: order,
                                       product: product)
      order.reload
    end
  end

  factory :order_with_distributor, parent: :order do
    distributor { create(:distributor_enterprise) }
  end

  factory :order_with_taxes, parent: :completed_order_with_totals do
    transient do
      product_price 0
      tax_rate_amount 0
      tax_rate_name ""
    end

    distributor { create(:distributor_enterprise) }
    order_cycle { create(:simple_order_cycle) }

    after(:create) do |order, proxy|
      order.distributor.update_attribute(:charges_sales_tax, true)
      Spree::Zone.global.update_attribute(:default_tax, true)

      p = FactoryBot.create(:taxed_product, zone: Spree::Zone.global,
                                            price: proxy.product_price,
                                            tax_rate_amount: proxy.tax_rate_amount,
                                            tax_rate_name: proxy.tax_rate_name)
      FactoryBot.create(:line_item, order: order, product: p, price: p.price)
      order.reload
    end
  end

  factory :order_with_credit_payment, parent: :completed_order_with_totals do
    distributor { create(:distributor_enterprise) }
    order_cycle { create(:simple_order_cycle) }

    after(:create) do |order|
      create(:payment, amount: order.total + 10_000, order: order, state: "completed")
      order.reload
    end
  end

  factory :order_without_full_payment, parent: :completed_order_with_totals do
    distributor { create(:distributor_enterprise) }
    order_cycle { create(:simple_order_cycle) }

    after(:create) do |order|
      create(:payment, amount: order.total - 1, order: order, state: "completed")
      order.reload
    end
  end

  factory :completed_order_with_fees, parent: :order_with_distributor do
    transient do
      payment_fee 5
      shipping_fee 3
    end

    ship_address { create(:address) }
    order_cycle { create(:simple_order_cycle) }

    after(:create) do |order, evaluator|
      create(:line_item, order: order)
      product = create(:simple_product)
      create(:line_item, order: order, product: product)

      payment_calculator = build(:calculator_per_item, preferred_amount: evaluator.payment_fee)
      payment_method = create(:payment_method, calculator: payment_calculator)
      create(:payment, order: order,
                       amount: order.total,
                       payment_method: payment_method,
                       state: 'checkout')

      create(:shipping_method_with, :shipping_fee, shipping_fee: evaluator.shipping_fee,
                                                   distributors: [order.distributor])

      order.reload
      while !order.completed? do break unless order.next! end
    end
  end
end

FactoryBot.modify do
  factory :order do
    trait :with_line_item do
      transient do
        variant { FactoryGirl.create(:variant) }
      end

      after(:create) do |order, evaluator|
        create(:line_item, order: order, variant: evaluator.variant)
      end
    end

    trait :completed do
      transient do
        payment_method { create(:payment_method, distributors: [distributor]) }
      end

      after(:create) do |order, evaluator|
        order.create_shipment!
        create(:payment, state: "checkout", order: order, amount: order.total,
                         payment_method: evaluator.payment_method)
        order.update_distribution_charge!
        while !order.completed? do break unless order.next! end
      end
    end
  end
end
