FactoryBot.define do
  factory :user do
    sequence(:email) { |num| "user-#{num}@test.com" }
    password_digest { 'password' }

    trait :with_name do
      first_name "John"
      last_name "Doe"
      email "funny@mail.com"
    end

    trait :with_different_name do
      first_name "Specimen"
      last_name "Simpson"
      email "different@mail.com"
    end

    after(:create) do |user|
      if user.accounts.empty?
        account = create(:account)
        auth = create(:account_auth, user: user, account: account, role: :account_manager)
        user.account_auths << auth
      end
    end

    trait :accountless do
      after(:create) do |user|
        user.accounts.delete_all
      end
    end

    trait :admin do
      admin true
    end

    trait :account_manager do
      after(:create) do |user|
        user.update(admin: false)
        user.account_auths.update(role: :account_manager)
      end
    end

    trait :manager do
      after(:create) do |user|
        user.update(admin: false)
        user.account_auths.update(role: :project_manager)
      end
    end

    trait :associate do
      after(:create) do |user|
        user.update(admin: false)
        user.account_auths.update(role: :associate)
      end
    end
  end
end
