require 'faker'

FactoryGirl.define do
  factory :user, :class => JamRuby::User do
    transient do
      specific_instruments nil
    end

    sequence(:email) { |n| "person_#{n}@example.com" }
    sequence(:first_name) { |n| "Person" }
    sequence(:last_name) { |n| "#{n}" }
    password "foobar"
    password_confirmation "foobar"
    email_confirmed true
    city "Apex"
    state "NC"
    country "US"
    musician true
    terms_of_service true
    last_jam_audio_latency 5
    reuse_card true
    has_redeemable_jamtrack true
    gifted_jamtracks 0
    remaining_free_lessons 1
    remaining_test_drives 0
    stored_credit_card false

    #u.association :musician_instrument, factory: :musician_instrument, user: u

    before(:create) do |user, evaluator|
      if evaluator.specific_instruments
        evaluator.specific_instruments.each do |instrument|
          user.musician_instruments << FactoryGirl.build(:musician_instrument, player: user, instrument: instrument)
        end
      else
        user.musician_instruments << FactoryGirl.build(:musician_instrument, player: user)
      end
    end

    factory :fan do
      musician false
    end

    factory :admin do
      admin true
    end

    factory :austin_user do
      first_name 'Austin'
      sequence(:last_name) { |n| "#{n}" }
      state 'TX'
      city 'Austin'
      last_jam_locidispid { austin_geoip[:locidispid] }
      last_jam_addr { austin_ip }
    end

    factory :dallas_user do
      first_name 'Dallas'
      sequence(:last_name) { |n| "#{n}" }
      state 'TX'
      city 'Dallas'
      last_jam_locidispid { dallas_geoip[:locidispid] }
      last_jam_addr { dallas_ip }
    end

    factory :houston_user do
      first_name 'Houston'
      sequence(:last_name) { |n| "#{n}" }
      state 'TX'
      city 'Houston'
      last_jam_locidispid { houston_geoip[:locidispid] }
      last_jam_addr { houston_ip }
    end

    factory :miami_user do
      first_name 'Miami'
      sequence(:last_name) { |n| "#{n}" }
      state 'FL'
      city 'Miami'
      last_jam_locidispid { miami_geoip[:locidispid] }
      last_jam_addr { miami_ip }
    end

    factory :seattle_user do
      first_name 'Seattle'
      sequence(:last_name) { |n| "#{n}" }
      state 'WA'
      city 'Seattle'
      last_jam_locidispid { seattle_geoip[:locidispid] }
      last_jam_addr { seattle_ip }
    end
    factory :single_user_session do
      after(:create) do |user, evaluator|
        active_music_session = FactoryGirl.create(:active_music_session, :creator => user)
        connection = FactoryGirl.create(:connection, :user => user, :music_session => active_music_session)
      end
    end
    factory :teacher_user do
      after(:create) do |user, evaluator|
        teacher = FactoryGirl.create(:teacher, user: user, price_per_lesson_60_cents: 3000, price_per_month_60_cents: 3000)
        user.is_a_teacher = true
        user.save!
      end
    end
  end

  factory :teacher, :class => JamRuby::Teacher do
    association :user, factory: :user
    price_per_lesson_60_cents 3000
    price_per_month_60_cents 3000
    short_bio 'abc def uueue doc neck'
  end

  factory :musician_instrument, :class => JamRuby::MusicianInstrument do
    instrument { JamRuby::Instrument.find('electric guitar') }
    proficiency_level 1
    priority 0
  end


  factory :active_music_session_no_user_history, :class => JamRuby::ActiveMusicSession do

    association :creator, factory: :user

    transient do
      name "My Music Session"
      description "Come Music Session"
      fan_chat true
      fan_access true
      approval_required false
      musician_access true
      legal_terms true
      genre JamRuby::Genre.first
      band nil
      language 'eng'
    end


    before(:create) do |session, evaluator|
      if evaluator.music_session
        session.id = evaluator.music_session.id
      else
        music_session = FactoryGirl.create(:music_session, name: evaluator.name, description: evaluator.description, fan_chat: evaluator.fan_chat,
                                           fan_access: evaluator.fan_access, approval_required: evaluator.approval_required, musician_access: evaluator.musician_access,
                                           genre: evaluator.genre, creator: evaluator.creator, band: evaluator.band, language: evaluator.language)
        session.id = music_session.id
      end
    end

    factory :active_music_session do
      after(:create) { |session|
        FactoryGirl.create(:music_session_user_history, :history => session.music_session, :user => session.creator)
      }

      factory :active_music_session_with_mount do
        association :mount, :factory => :icecast_mount
      end
    end
  end

  factory :music_session, :class => JamRuby::MusicSession do
    ignore do
      student nil
    end
    sequence(:name) { |n| "Music Session #{n}" }
    sequence(:description) { |n| "Music Session Description #{n}" }
    fan_chat true
    fan_access true
    approval_required false
    musician_access true
    legal_terms true
    language 'eng'
    timezone 'UTC,Etc/UTC'
    legal_policy 'standard'
    recurring_mode 'once'
    genre JamRuby::Genre.first
    association :creator, :factory => :user
    open_rsvps true
    scheduled_start Time.now
    scheduled_duration 3600

    factory :recurring_music_session_weekly do
      recurring_mode 'weekly'
    end

    after(:create) do |music_session|
      FactoryGirl.create(:chosen_rsvp_slot, user: music_session.creator, music_session: music_session)
    end
  end

  factory :music_session_user_history, :class => JamRuby::MusicSessionUserHistory do
    transient do
      history nil
      user nil
    end

    instruments 'guitar'
    music_session_id { history.id }
    user_id { user.id }
    sequence(:client_id) { |n| "Connection #{n}" }
  end

  factory :connection, :class => JamRuby::Connection do
    sequence(:client_id) { |n| "Client#{n}" }
    ip_address "1.1.1.1"
    as_musician true
    addr 0
    locidispid 0
    client_type 'client'
    gateway 'gateway1'
    last_jam_audio_latency { user.last_jam_audio_latency if user }
    sequence(:channel_id) { |n| "Channel#{n}" }
    association :user, factory: :user
    scoring_timeout Time.now
  end

  factory :invitation, :class => JamRuby::Invitation do

  end

  factory :friendship, :class => JamRuby::Friendship do

  end

  factory :friend_request, :class => JamRuby::FriendRequest do

  end

  factory :band_musician, :class => JamRuby::BandMusician do

  end

  factory :band, :class => JamRuby::Band do
    sequence(:name) { |n| "Band" }
    biography "My Biography"
    city "Apex"
    state "NC"
    country "US"
    before(:create) { |band|
      band.genres << Genre.first
    }
  end

  factory :genre, :class => JamRuby::Genre do
    description { |n| "Genre #{n}" }
  end

  factory :language, :class => JamRuby::Language do
    id { |n| "Language #{n}" }
    description { |n| "Language #{n}" }
  end

  factory :subject, :class => JamRuby::Subject do
    id { |n| "Subject #{n}" }
    description { |n| "Subject #{n}" }
  end

  factory :join_request, :class => JamRuby::JoinRequest do
    text 'let me in to the session!'
  end

  factory :track, :class => JamRuby::Track do
    sound "mono"
    sequence(:client_track_id) { |n| "client_track_id#{n}" }
    sequence(:client_resource_id) { |n| "resource_id#{n}" }
  end

  factory :backing_track, :class => JamRuby::BackingTrack do
    sequence(:client_track_id) { |n| "client_track_id#{n}" }
    filename 'foo.mp3'
  end

  factory :video_source, :class => JamRuby::VideoSource do
    #client_video_source_id "test_source_id"
    sequence(:client_video_source_id) { |n| "client_video_source_id#{n}" }
  end

  factory :recorded_track, :class => JamRuby::RecordedTrack do
    instrument JamRuby::Instrument.find('acoustic guitar')
    sound 'stereo'
    sequence(:client_id) { |n| "client_id-#{n}" }
    sequence(:track_id) { |n| "track_id-#{n}" }
    sequence(:client_track_id) { |n| "client_track_id-#{n}" }
    md5 'abc'
    length 1
    fully_uploaded true
    association :user, factory: :user
    association :recording, factory: :recording
  end

  factory :recorded_backing_track, :class => JamRuby::RecordedBackingTrack do
    sequence(:client_id) { |n| "client_id-#{n}" }
    sequence(:backing_track_id) { |n| "track_id-#{n}" }
    sequence(:client_track_id) { |n| "client_track_id-#{n}" }
    sequence(:filename) { |n| "filename-{#n}" }
    sequence(:url) { |n| "/recordings/blah/#{n}" }
    md5 'abc'
    length 1
    fully_uploaded true
    association :user, factory: :user
    association :recording, factory: :recording
  end

  factory :recorded_video, :class => JamRuby::RecordedVideo do
    sequence(:client_video_source_id) { |n| "client_video_source_id-#{n}" }
    fully_uploaded true
    length 1
    association :user, factory: :user
    association :recording, factory: :recording
  end

  factory :recorded_jam_track_track, :class => JamRuby::RecordedJamTrackTrack do
    association :user, factory: :user
    association :recording, factory: :recording
    association :jam_track_track, factory: :jam_track_track
  end

  factory :instrument, :class => JamRuby::Instrument do
    description { |n| "Instrument #{n}" }
  end

  factory :recording, :class => JamRuby::Recording do

    association :owner, factory: :user
    association :music_session, factory: :active_music_session
    association :band, factory: :band

    factory :recording_with_track do
      before(:create) { |recording|
        recording.recorded_tracks << FactoryGirl.create(:recorded_track, recording: recording, user: recording.owner)
      }
    end
  end

  factory :claimed_recording, :class => JamRuby::ClaimedRecording do
    sequence(:name) { |n| "name-#{n}" }
    sequence(:description) { |n| "description-#{n}" }
    is_public true
    association :genre, factory: :genre
    association :user, factory: :user

    before(:create) { |claimed_recording, evaluator|
      claimed_recording.recording = FactoryGirl.create(:recording_with_track, owner: claimed_recording.user) unless evaluator.recording
    }


  end

  factory :mix, :class => JamRuby::Mix do
    transient do
      autowire true
    end
    started_at Time.now
    completed_at Time.now
    ogg_md5 'abc'
    ogg_length 1
    sequence(:ogg_url) { |n| "recordings/ogg/#{n}" }
    mp3_md5 'abc'
    mp3_length 1
    sequence(:mp3_url) { |n| "recordings/mp3/#{n}" }
    completed true

    before(:create) { |mix, evaluator|
      if evaluator.autowire
        user = FactoryGirl.create(:user)
        mix.recording = FactoryGirl.create(:recording_with_track, owner: user)
        mix.recording.claimed_recordings << FactoryGirl.create(:claimed_recording, user: user, recording: mix.recording)
      end
    }
  end

  factory :quick_mix, :class => JamRuby::QuickMix do
    transient do
      autowire true
    end
    started_at nil
    completed_at nil
    ogg_md5 nil
    ogg_length 0
    ogg_url nil
    mp3_md5 nil
    mp3_length 0
    mp3_url nil
    completed false

    before(:create) { |mix, evaluator|
      if evaluator.autowire
        user = FactoryGirl.create(:user)
        mix.user = user
        mix.recording = FactoryGirl.create(:recording_with_track, owner: user)
        mix.recording.claimed_recordings << FactoryGirl.create(:claimed_recording, user: user, recording: mix.recording)
      end
    }


    factory :quick_mix_completed do
      started_at 1.minute.ago
      completed_at Time.now
      ogg_md5 'a'
      ogg_length 1
      ogg_url 'recordings/ogg'
      mp3_md5 'a'
      mp3_length 1
      mp3_url 'recordings/mp3'
      completed true
    end
  end

  factory :invited_user, :class => JamRuby::InvitedUser do
    sequence(:email) { |n| "user#{n}@someservice.com" }
    autofriend false
  end

  factory :music_session_perf_data, :class => JamRuby::MusicSessionPerfData do
    association :music_session => :music_session
  end

  factory :crash_dump, :class => JamRuby::CrashDump do
  end


  factory :promo_buzz, :class => JamRuby::PromoBuzz do
    text_short Faker::Lorem.characters(10)
    text_long Faker::Lorem.paragraphs(3).join("\n")
  end

  factory :icecast_limit, :class => JamRuby::IcecastLimit do
    clients 5
    sources 1
    queue_size 102400
    client_timeout 30
    header_timeout 15
    source_timeout 10
    burst_size 65536
  end

  factory :icecast_admin_authentication, :class => JamRuby::IcecastAdminAuthentication do
    source_pass Faker::Lorem.characters(10)
    admin_user Faker::Lorem.characters(10)
    admin_pass Faker::Lorem.characters(10)
    relay_user Faker::Lorem.characters(10)
    relay_pass Faker::Lorem.characters(10)
  end

  factory :icecast_directory, :class => JamRuby::IcecastDirectory do
    yp_url_timeout 15
    yp_url Faker::Lorem.characters(10)
  end

  factory :icecast_master_server_relay, :class => JamRuby::IcecastMasterServerRelay do
    master_server Faker::Lorem.characters(10)
    master_server_port 8000
    master_update_interval 120
    master_username Faker::Lorem.characters(10)
    master_pass Faker::Lorem.characters(10)
    relays_on_demand 1
  end

  factory :icecast_path, :class => JamRuby::IcecastPath do
    base_dir Faker::Lorem.characters(10)
    log_dir Faker::Lorem.characters(10)
    pid_file Faker::Lorem.characters(10)
    web_root Faker::Lorem.characters(10)
    admin_root Faker::Lorem.characters(10)
  end

  factory :icecast_logging, :class => JamRuby::IcecastLogging do
    access_log Faker::Lorem.characters(10)
    error_log Faker::Lorem.characters(10)
    log_level 3
    log_archive nil
    log_size 10000
  end

  factory :icecast_security, :class => JamRuby::IcecastSecurity do
    chroot 0
  end

  factory :icecast_mount, :class => JamRuby::IcecastMount do
    sequence(:name) { |n| "/mount_#{n}" }
    source_username Faker::Lorem.characters(10)
    source_pass Faker::Lorem.characters(10)
    max_listeners 100
    max_listener_duration 3600
    fallback_mount Faker::Lorem.characters(10)
    fallback_override 1
    fallback_when_full 1
    is_public -1
    stream_name Faker::Lorem.characters(10)
    stream_description Faker::Lorem.characters(10)
    stream_url Faker::Lorem.characters(10)
    genre Faker::Lorem.characters(10)
    hidden 0
    association :server, factory: :icecast_server_with_overrides

    factory :icecast_mount_with_auth do
      association :authentication, :factory => :icecast_user_authentication

      factory :iceast_mount_with_template do
        association :mount_template, :factory => :icecast_mount_template

        factory :iceast_mount_with_music_session do
          association :music_session, :factory => :active_music_session
        end
      end
    end
  end

  factory :icecast_source_change, :class => JamRuby::IcecastSourceChange do
    source_direction true
    success true
    sequence(:client_id) { |n| "client_id#{n}" }
    change_type JamRuby::IcecastSourceChange::CHANGE_TYPE_CLIENT
    association :user, :factory => :user
    association :mount, :factory => :iceast_mount_with_music_session

  end

  factory :icecast_listen_socket, :class => JamRuby::IcecastListenSocket do
    port 8000
  end

  factory :icecast_relay, :class => JamRuby::IcecastRelay do
    port 8000
    mount Faker::Lorem.characters(10)
    server Faker::Lorem.characters(10)
    on_demand 1
  end

  factory :icecast_user_authentication, :class => JamRuby::IcecastUserAuthentication do
    authentication_type 'url'
    unused_username Faker::Lorem.characters(10)
    unused_pass Faker::Lorem.characters(10)
    mount_add Faker::Lorem.characters(10)
    mount_remove Faker::Lorem.characters(10)
    listener_add Faker::Lorem.characters(10)
    listener_remove Faker::Lorem.characters(10)
    auth_header 'icecast-auth-user: 1'
    timelimit_header 'icecast-auth-timelimit:'
  end

  factory :icecast_server, :class => JamRuby::IcecastServer do
    sequence(:hostname) { |n| "hostname-#{n}" }
    sequence(:server_id) { |n| "test-server-#{n}" }

    factory :icecast_server_minimal do
      association :template, :factory => :icecast_template_minimal
      association :mount_template, :factory => :icecast_mount_template

      factory :icecast_server_with_overrides do
        association :limit, :factory => :icecast_limit
        association :admin_auth, :factory => :icecast_admin_authentication
        association :path, :factory => :icecast_path
        association :logging, :factory => :icecast_logging
        association :security, :factory => :icecast_security

        before(:create) do |server|
          server.listen_sockets << FactoryGirl.build(:icecast_listen_socket)
        end
      end
    end
  end

  factory :icecast_mount_template, :class => JamRuby::IcecastMountTemplate do
    sequence(:name) { |n| "name-#{n}" }
    source_username Faker::Lorem.characters(10)
    source_pass Faker::Lorem.characters(10)
    max_listeners 100
    max_listener_duration 3600
    fallback_mount Faker::Lorem.characters(10)
    fallback_override 1
    fallback_when_full 1
    is_public -1
    stream_name Faker::Lorem.characters(10)
    stream_description Faker::Lorem.characters(10)
    stream_url Faker::Lorem.characters(10)
    genre Faker::Lorem.characters(10)
    hidden 0
    association :authentication, :factory => :icecast_user_authentication
  end

  factory :icecast_template, :class => JamRuby::IcecastTemplate do

    sequence(:name) { |n| "name-#{n}" }
    sequence(:location) { |n| "location-#{n}" }

    factory :icecast_template_minimal do
      association :limit, :factory => :icecast_limit
      association :admin_auth, :factory => :icecast_admin_authentication
      association :path, :factory => :icecast_path
      association :logging, :factory => :icecast_logging
      association :security, :factory => :icecast_security

      before(:create) do |template|
        template.listen_sockets << FactoryGirl.build(:icecast_listen_socket)
      end
    end
  end

  factory :facebook_signup, :class => JamRuby::FacebookSignup do
    sequence(:lookup_id) { |n| "lookup-#{n}" }
    sequence(:first_name) { |n| "first-#{n}" }
    sequence(:last_name) { |n| "last-#{n}" }
    gender 'M'
    sequence(:email) { |n| "jammin-#{n}@jamkazam.com" }
    sequence(:uid) { |n| "uid-#{n}" }
    sequence(:token) { |n| "token-#{n}" }
    token_expires_at Time.now
  end


  factory :recording_comment, :class => JamRuby::RecordingComment do
    sequence(:comment) { |n| "comment-#{n}" }
    association :recording, factory: :recording
    association :user, factory: :recording
  end

  factory :playable_play, :class => JamRuby::PlayablePlay do
    association :user, factory: :user
  end

  factory :recording_like, :class => JamRuby::RecordingLiker do

  end

  factory :music_session_like, :class => JamRuby::MusicSessionLiker do

  end

  factory :event, :class => JamRuby::Event do
    sequence(:slug) { |n| "slug-#{n}" }
    title 'event title'
    description 'event description'
  end

  factory :event_session, :class => JamRuby::EventSession do
  end

  factory :email_batch, :class => JamRuby::EmailBatch do
    subject Faker::Lorem.sentence
    body "#{JamRuby::EmailBatch::VAR_FIRST_NAME} " + Faker::Lorem.paragraphs(3).join("\n")
    test_emails 4.times.collect { Faker::Internet.safe_email }.join(',')
  end

  factory :email_batch_new_musician, :class => JamRuby::EmailBatchNewMusician do
  end

  factory :email_batch_progression, :class => JamRuby::EmailBatchProgression do
  end

  factory :email_batch_scheduled_session, :class => JamRuby::EmailBatchScheduledSessions do
  end

  factory :notification, :class => JamRuby::Notification do

    factory :notification_text_message do
      description 'TEXT_MESSAGE'
      message Faker::Lorem.characters(10)
    end
  end

  factory :diagnostic, :class => JamRuby::Diagnostic do
    type JamRuby::Diagnostic::NO_HEARTBEAT_ACK
    creator JamRuby::Diagnostic::CLIENT
    data Faker::Lorem.sentence
  end

  factory :rsvp_slot, :class => JamRuby::RsvpSlot do

    proficiency_level "beginner"
    instrument { Instrument.find('electric guitar') }

    factory :chosen_rsvp_slot do
      transient do
        user nil
      end

      after(:create) do |rsvp_slot, evaluator|
        rsvp_request = FactoryGirl.create(:rsvp_request, user: evaluator.user)
        rsvp_request_rsvp_slot = FactoryGirl.create(:rsvp_request_rsvp_slot, chosen: true, rsvp_request: rsvp_request, rsvp_slot: rsvp_slot)
      end
    end
  end

  factory :rsvp_request, class: JamRuby::RsvpRequest do
    canceled false
    cancel_all false
    association :user, :factory => :user

    # creates *number* slots for a new rsvp_request (the case were you are too lazy / don't care to set up slots)
    factory :rsvp_request_for_multiple_slots do
      transient do
        #music_session nil
        number 1
        #chosen nil
      end

      after(:create) { |rsvp_request, evaluator|
        evaluator.number.times do |i|
          slot = FactoryGirl.create(:rsvp_slot, music_session: evaluator.music_session, instrument: Instrument.order(:id).limit(1).offset(i).first, proficiency_level: 1)
          FactoryGirl.create(:rsvp_request_rsvp_slot, chosen: evaluator.chosen, rsvp_request: rsvp_request, rsvp_slot: slot)
        end
      }
    end

    # creates a rsvp_request for the specified slots (helps when you already have a slot(s), and need to request to them)
    factory :rsvp_request_for_slots do
      transient do
        slots nil
        chosen nil
      end

      after(:create) { |rsvp_request, evaluator|
        evaluator.slots.each do |slot|
          FactoryGirl.create(:rsvp_request_rsvp_slot, chosen: evaluator.chosen, rsvp_request: rsvp_request, rsvp_slot: slot)
        end
      }
    end
  end

  factory :rsvp_request_rsvp_slot, :class => JamRuby::RsvpRequestRsvpSlot do
    chosen false
  end

  factory :latency_tester, :class => JamRuby::LatencyTester do
    transient do
      connection nil
      make_connection true
    end

    sequence(:client_id) { |n| "LatencyTesterClientId-#{n}" }

    after(:create) do |latency_tester, evaluator|
      latency_tester.connection = evaluator.connection if evaluator.connection
      latency_tester.connection = FactoryGirl.create(:connection, client_type: Connection::TYPE_LATENCY_TESTER, client_id: latency_tester.client_id) if evaluator.make_connection
      latency_tester.save
    end
  end

  factory :max_mind_release, :class => JamRuby::MaxMindRelease do
    released_at Time.now.to_date
  end

  factory :jam_track_licensor, :class => JamRuby::JamTrackLicensor do
    sequence(:name) { |n| "licensor-#{n}" }
    sequence(:description) { |n| "description-#{n}" }
    sequence(:attention) { |n| "attention-#{n}" }
    sequence(:address_line_1) { |n| "address1-#{n}" }
    sequence(:address_line_2) { |n| "address2-#{n}" }
    sequence(:city) { |n| "city-#{n}" }
    sequence(:state) { |n| "state-#{n}" }
    sequence(:zip_code) { |n| "zipcode-#{n}" }
    sequence(:contact) { |n| "contact-#{n}" }
    sequence(:email) { |n| "email-#{n}" }
    sequence(:phone) { |n| "phone-#{n}" }
  end

  factory :jam_track_mixdown, :class => JamRuby::JamTrackMixdown do
    association :user, factory: :user
    association :jam_track, factory: :jam_track
    sequence(:name) { |n| "mixdown-#{n}" }
    settings '{"speed":5}'
  end

  factory :jam_track_mixdown_package, :class => JamRuby::JamTrackMixdownPackage do
    file_type JamRuby::JamTrackMixdownPackage::FILE_TYPE_OGG
    sample_rate 48
    signing false
    signed false

    association :jam_track_mixdown, factory: :jam_track_mixdown
  end


  factory :jam_track, :class => JamRuby::JamTrack do
    sequence(:name) { |n| "jam-track-#{n}" }
    sequence(:description) { |n| "description-#{n}" }
    sequence(:slug) { |n| "slug-#{n}" }
    time_signature '4/4'
    status 'Production'
    recording_type 'Cover'
    sequence(:original_artist) { |n| "original-artist-#{n}" }
    sequence(:songwriter) { |n| "songwriter-#{n}" }
    sequence(:publisher) { |n| "publisher-#{n}" }
    sales_region 'United States'
    price 1.99
    download_price 2.99
    reproduction_royalty true
    public_performance_royalty true
    reproduction_royalty_amount 0.999
    licensor_royalty_amount 0.999
    sequence(:plan_code) { |n| "jamtrack-#{n}" }

    genres [JamRuby::Genre.first]
    association :licensor, factory: :jam_track_licensor

    factory :jam_track_with_tracks do
      after(:create) do |jam_track, evaluator|
        FactoryGirl.create(:jam_track_track, jam_track: jam_track)
      end
    end
  end

  factory :jam_track_track, :class => JamRuby::JamTrackTrack do
    position 1
    part 'lead guitar'
    track_type 'Track'
    instrument JamRuby::Instrument.find('electric guitar')
    association :jam_track, factory: :jam_track
  end

  factory :jam_track_right, :class => JamRuby::JamTrackRight do
    association :jam_track, factory: :jam_track
    association :user, factory: :user
    signing_44 false
    signing_48 false
  end


  factory :download_tracker, :class => JamRuby::DownloadTracker do
    remote_ip '1.1.1.1'
    paid false
    association :user, factory: :user
    association :jam_track, factory: :jam_track
  end

  factory :sale, :class => JamRuby::Sale do
    order_total 0
    association :user, factory: user
  end

  factory :recurly_transaction_web_hook, :class => JamRuby::RecurlyTransactionWebHook do

    transaction_type JamRuby::RecurlyTransactionWebHook::SUCCESSFUL_PAYMENT
    sequence(:recurly_transaction_id) { |n| "recurly-transaction-id-#{n}" }
    sequence(:subscription_id) { |n| "subscription-id-#{n}" }
    sequence(:invoice_id) { |n| "invoice-id-#{n}" }
    sequence(:invoice_number) { |n| 1000 + n }
    invoice_number_prefix nil
    action 'purchase'
    status 'success'
    transaction_at Time.now
    amount_in_cents 199
    reference 100000
    message 'meh'
    association :user, factory: :user

    factory :recurly_transaction_web_hook_failed do
      transaction_type JamRuby::RecurlyTransactionWebHook::FAILED_PAYMENT
    end
  end

  factory :broadcast_notification, :class => JamRuby::BroadcastNotification do
    title Faker::Lorem.sentence[0...50]
    message Faker::Lorem.paragraph[0...200]
    button_label Faker::Lorem.words(2).join(' ')[0...14]
    frequency 3
  end

  factory :affiliate_partner, class: 'JamRuby::AffiliatePartner' do
    sequence(:partner_name) { |n| "partner-#{n}" }
    entity_type 'Individual'
    signed_at Time.now
    association :partner_user, factory: :user
  end

  factory :affiliate_quarterly_payment, class: 'JamRuby::AffiliateQuarterlyPayment' do
    year 2015
    quarter 0
    association :affiliate_partner, factory: :affiliate_partner
  end

  factory :affiliate_monthly_payment, class: 'JamRuby::AffiliateMonthlyPayment' do
    year 2015
    month 0
    association :affiliate_partner, factory: :affiliate_partner
  end

  factory :affiliate_referral_visit, class: 'JamRuby::AffiliateReferralVisit' do
    ip_address '1.1.1.1'
    association :affiliate_partner, factory: :affiliate_partner
  end

  factory :affiliate_legalese, class: 'JamRuby::AffiliateLegalese' do
    legalese Faker::Lorem.paragraphs(6).join("\n\n")
  end

  factory :gift_card, class: 'JamRuby::GiftCard' do
    sequence(:code) { n.to_s }
    card_type JamRuby::GiftCardType::JAM_TRACKS_5
  end

  factory :gift_card_type, class: 'JamRuby::GiftCardType' do
    card_type JamRuby::GiftCardType::JAM_TRACKS_5
  end

  factory :gift_card_purchase, class: 'JamRuby::GiftCardPurchase' do

    association :user, factory: :user
  end

  factory :posa_card, class: 'JamRuby::PosaCard' do
    sequence(:code) { |n| n.to_s }
    card_type JamRuby::PosaCard::JAM_TRACKS_5
    credits 5
    requires_purchase false
    purchased true

    factory :posa_card_lesson_2 do
      card_type JamRuby::PosaCard::JAM_CLASS_2
      credits 2
      lesson_package_type { JamRuby::LessonPackageType.test_drive_2 }
      is_lesson true
    end

    factory :posa_card_lesson_4 do
      card_type JamRuby::PosaCard::JAM_CLASS_4
      credits 4
      lesson_package_type { JamRuby::LessonPackageType.test_drive_4 }
      is_lesson true
    end

    factory :amazon_test_drive_free_2 do
      card_type JamRuby::PosaCard::JAM_CLASS_2
      credits 2
      lesson_package_type { JamRuby::LessonPackageType.amazon_test_drive_free_2 }
      is_lesson true
      is_test false
      purchased true
      preactivate true
    end
  end

  factory :posa_card_type, class: 'JamRuby::PosaCardType' do
    card_type JamRuby::PosaCardType::JAM_TRACKS_5
  end

  factory :posa_card_purchase, class: 'JamRuby::PosaCardPurchase' do
    association :user, factory: :user
  end

  factory :jamblaster, class: 'JamRuby::Jamblaster' do

    association :user, factory: :user

    sequence(:serial_no) { |n| "serial_no#{n}" }
    sequence(:client_id) { |n| "client_id#{n}" }
  end

  factory :jamblaster_pairing_request, class: 'JamRuby::JamblasterPairingRequest' do

    association :user, factory: :user
    association :jamblaster, factory: :jamblaster

    sequence(:jamblaster_client_id) { |n| "jamblaster_client_id#{n}" }
    sequence(:sibling_key) { |n| "sibling_key#{n}" }
  end

  factory :school, class: 'JamRuby::School' do
    association :user, factory: :user
    sequence(:name) { |n| "Dat Music School #{n}" }
    enabled true
    scheduling_communication 'teacher'
  end

  factory :school_invitation, class: 'JamRuby::SchoolInvitation' do
    association :school, factory: :school
    note "hey come in in"
    as_teacher true
    sequence(:email) { |n| "school_person#{n}@example.com" }
    sequence(:first_name) { |n| "FirstName" }
    sequence(:last_name) { |n| "LastName" }
    accepted false
  end

  factory :retailer, class: 'JamRuby::Retailer' do
    association :user, factory: :user
    sequence(:name) { |n| "Dat Music Retailer" }
    sequence(:slug) { |n| "retailer-#{n}" }
    enabled true
  end

  factory :retailer_invitation, class: 'JamRuby::RetailerInvitation' do
    association :retailer, factory: :retailer
    note "hey come in in"
    sequence(:email) { |n| "retail_person#{n}@example.com" }
    sequence(:first_name) { |n| "FirstName" }
    sequence(:last_name) { |n| "LastName" }
    accepted false
  end

  factory :lesson_booking_slot, class: 'JamRuby::LessonBookingSlot' do
    factory :lesson_booking_slot_single do
      slot_type 'single'
      preferred_day { Date.today + 3 }
      day_of_week nil
      hour 12
      minute 30
      timezone 'UTC'
    end

    factory :lesson_booking_slot_recurring do
      slot_type 'recurring'
      preferred_day nil
      day_of_week 0
      hour 12
      minute 30
      timezone 'UTC'
    end
  end

  factory :lesson_booking, class: 'JamRuby::LessonBooking' do
    association :user, factory: :user
    association :teacher, factory: :teacher_user
    card_presumed_ok false
    sent_notices false
    recurring false
    lesson_length 30
    lesson_type JamRuby::LessonBooking::LESSON_TYPE_FREE
    payment_style JamRuby::LessonBooking::PAYMENT_STYLE_ELSEWHERE
    description "Oh my goodness!"
    status JamRuby::LessonBooking::STATUS_REQUESTED
    before(:create) do |lesson_booking, evaluator|
      lesson_booking.lesson_booking_slots = [FactoryGirl.build(:lesson_booking_slot_single, lesson_booking: lesson_booking),
                                             FactoryGirl.build(:lesson_booking_slot_single, lesson_booking: lesson_booking)]
    end
    #lesson_booking_slots [FactoryGirl.build(:lesson_booking_slot_single), FactoryGirl.build(:lesson_booking_slot_single)]
  end

  factory :lesson_package_purchase, class: "JamRuby::LessonPackagePurchase" do
    lesson_package_type { JamRuby::LessonPackageType.single }
    association :user, factory: :user
    association :teacher, factory: :teacher_user
    price 30.00

    factory :test_drive_purchase do
      lesson_package_type { JamRuby::LessonPackageType.test_drive_4 }
      association :lesson_booking, factory: :lesson_booking
      price 49.99
    end
  end

  factory :lesson_session, class: 'JamRuby::LessonSession' do

    ignore do
      student nil
    end

    music_session { FactoryGirl.create(:music_session, creator: student) }
    lesson_booking { FactoryGirl.create(:lesson_booking, user: student, teacher: teacher) }
    association :teacher, factory: :teacher_user
    lesson_type JamRuby::LessonSession::LESSON_TYPE_SINGLE
    duration 30
    booked_price 49.99
    status JamRuby::LessonSession::STATUS_REQUESTED
    #teacher_complete true
    #student_complete true
  end

  factory :charge, class: 'JamRuby::Charge' do
    type 'JamRuby::Charge'
    amount_in_cents 1000
  end

  factory :teacher_payment_charge, parent: :charge, class: 'JamRuby::TeacherPaymentCharge' do
    type 'JamRuby::TeacherPaymentCharge'
    association :user, factory: :user
  end


  factory :lesson_payment_charge, parent: :charge, class: 'JamRuby::LessonPaymentCharge' do
    type 'JamRuby::LessonPaymentCharge'
    association :user, factory: :user
  end


  factory :teacher_payment, class: 'JamRuby::TeacherPayment' do
    association :teacher, factory: :teacher_user
    association :teacher_payment_charge, factory: :teacher_payment_charge
    amount_in_cents 1000
    fee_in_cents 0
  end

  # you gotta pass either lesson_session or lesson_package_purchase for this to make sense
  factory :teacher_distribution, class: 'JamRuby::TeacherDistribution' do
    association :teacher, factory: :teacher_user
    association :teacher_payment, factory: :teacher_payment
    ready false
    amount_in_cents 1000
  end


  factory :ip_blacklist, class: "JamRuby::IpBlacklist" do
    remote_ip '1.1.1.1'
  end

  factory :ip_whitelist, class: "JamRuby::IpWhitelist" do
    remote_ip '1.1.1.1'
  end

  factory :user_blacklist, class: "JamRuby::UserBlacklist" do
    association :user, factory: :user
  end

  factory :user_whitelist, class: "JamRuby::UserWhitelist" do
    association :user, factory: :user
  end

  factory :email_blacklist, class: "JamRuby::EmailBlacklist" do
    sequence(:email) { |n| "person_#{n}@example.com" }
  end

  factory :music_notation, class: "JamRuby::MusicNotation" do
    attachment_type { JamRuby::MusicNotation::TYPE_NOTATION }
    association :user, factory: :user
    file_url 'abc'
    size 100
    file_name 'some_file.jpg'
  end

  factory :test_drive_package, class: "JamRuby::TestDrivePackage" do

    sequence(:name) { |n| "package-#{n}" }

    trait :one_pack do
      package_type 1
      after(:create) do |package, evaluator|
        1.times.each do
          FactoryGirl.create(:test_drive_package_teachers, test_drive_package: package)
        end
      end
    end

    trait :two_pack do
      package_type 2
      after(:create) do |package, evaluator|
        2.times.each do
          FactoryGirl.create(:test_drive_package_teachers, test_drive_package: package)
        end
      end
    end

    trait :four_pack do
      package_type 4
      after(:create) do |package, evaluator|
        4.times.each do
          FactoryGirl.create(:test_drive_package_teachers, test_drive_package: package)
        end
      end
    end

  end

  factory :test_drive_package_teachers, class: "JamRuby::TestDrivePackageTeacher" do
    association :user, factory: :teacher_user
    association :test_drive_package, factory: [:test_drive_package, :four_pack]
  end

  factory :test_drive_package_choice, class: "JamRuby::TestDrivePackageChoice" do
    association :user, factory: :user
    association :test_drive_package, factory: [:test_drive_package, :four_pack]
  end

  factory :test_drive_package_choice_teacher, class: "JamRuby::TestDrivePackageChoiceTeacher" do
    association :teacher, factory: :teacher_user
    association :test_drive_package_choice, factory: :test_drive_package_choice
  end
end

