# frozen_string_literal: true

FactoryGirl.define do
  sequence :login, 1000 do |n|
    "user#{n}"
  end

  factory :ldap_jill, class: User do
    login { 'jilluser' }
    display_name { 'Jill Z. User' }
    title { 'LDAP User' }
  end

  factory :user do
    transient do
      event { nil }
      proxy_for { nil }
    end

    login
    display_name { 'Joe Example' }
    title { 'User' }

    # Scholarsphere grants uploading rights to PSU users. We know
    # that a user is a PSU user if their information is in LDAP. This
    # is where we stub that information out to force it to be true.
    ldap_available { true }
    ldap_last_update { Time.zone.now }
    groups_last_update { Time.zone.now }
    group_list { 'umg/up.dlt.scholarsphere-users' }

    # This user should be able to log in and modify metadata, but not
    # upload files.
    factory :non_psu_user do
      ldap_available { false }
    end

    factory :administrator do
      login { 'administrator1' }
      display_name { 'Administrator 1' }
      title { 'Administrator' }
      group_list { ScholarSphere::Application.config.admin_group }
    end

    factory :first_proxy do
      display_name { 'First Proxy' }
    end

    factory :second_proxy do
      display_name { 'Second Proxy' }
    end

    factory :archivist do
      login { 'archivist1' }
      title { 'Archivist' }
    end

    factory :random_user do
      display_name { 'Random User' }
    end

    factory :jill do
      login { 'jilluser' }
      display_name { 'Jill Z. User' }
    end

    trait :with_event do
      after(:create) do |u, attrs|
        u.log_profile_event(Sufia::Event.create(attrs.event, Time.current.to_i))
      end
    end

    trait :with_proxy do
      after(:create) do |u, attrs|
        u.can_make_deposits_for << attrs.proxy_for
      end
    end

    trait :with_two_groups do
      after(:create) do |u|
        group_list_array = ['umg/up.dlt.scholarsphere-admin.admin1', 'umg/up.dlt.scholarsphere-admin.admin2']
        u.update_attribute :group_list, group_list_array.join(';?;')
        u.update_attribute :groups_last_update, Time.now
      end
    end

    trait :with_many_groups do
      after(:create) do |u|
        group_list_array = (0..100).map { |i| "umg/up.dlt.scholarsphere-admin.admin#{i}" }
        u.update_attribute :group_list, group_list_array.join(';?;')
        u.update_attribute :groups_last_update, Time.now
      end
    end
  end
end
