FactoryBot.define do
  factory :material_release do
    association :project

    name "Test Materials"

    trait :native do
      person_first_name "Jane"
      person_last_name "Doe"
      person_phone "100-555-1001"

      signature do
        path = Rails.root.join("spec", "fixtures", "files", "signature.png")
        Rack::Test::UploadedFile.new(path, "image/png")
      end
    end

    trait :with_photo do
      photos do
        path = Rails.root.join("spec", "fixtures", "files", "material_photo.png")
        [Rack::Test::UploadedFile.new(path, "image/png")]
      end
    end


    trait :non_native do
      contract do
        path = Rails.root.join("spec", "fixtures", "files", "contract.pdf")
        Rack::Test::UploadedFile.new(path, "application/pdf")
      end
    end

    factory :material_release_with_contract_template do
      after(:build) do |material_release, _|
        material_release.contract_template = build(:material_release_contract_template)
      end
    end

    factory :material_release_with_contract_template_and_photo do
      after(:build) do |material_release, _|
        material_release.contract_template = build(:material_release_contract_template)
        path = Rails.root.join("spec", "fixtures", "files", "material_photo.png")
        material_release.photos.attach Rack::Test::UploadedFile.new(path, "image/png")
      end
    end

    factory :material_release_with_photo do
      after(:build) do |material_release, _|
        path = Rails.root.join("spec", "fixtures", "files", "material_photo.png")
        material_release.photos.attach Rack::Test::UploadedFile.new(path, "image/png")
      end
    end

    factory :material_release_with_photos do
      transient do
        photos_count { 2 }
      end

      after(:build) do |material_release, evaluator|
        material_release.photos = evaluator.photos_count.times.map do
          path = Rails.root.join("spec", "fixtures", "files", "material_photo.png")
          Rack::Test::UploadedFile.new(path, "image/png")
        end
      end
    end
  end
end
