# This file should ensure the existence of records required to run the application in every environment (production,
# development, test). The code here should be idempotent so that it can be executed at any point in every environment.
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
#
# Example:
#
#   ["Action", "Comedy", "Drama", "Horror"].each do |genre_name|
#     MovieGenre.find_or_create_by!(name: genre_name)
#   end

Faker::Config.locale = :de

# Admins
User.create!(email: "david.boehm@hs-rm.de", firstname: "David", lastname: "Böhm", role: :admin, password_digest: BCrypt::Password.create("admin"), verified: true)
User.create!(email: "maximilian.lasser@hs-rm.de", firstname: "Max", lastname: "Lasser", role: :admin, password_digest: BCrypt::Password.create("admin"), verified: true)

# Operators
operators = []
operators << User.create!(email: "tutor.operator@hs-rm.de", firstname: "Tutor", lastname: "Operator", role: :operator, password_digest: BCrypt::Password.create("operator"), verified: true)
operators << User.create!(email: "tutor2.operator@hs-rm.de", firstname: "Tutor2", lastname: "Operator", role: :operator, password_digest: BCrypt::Password.create("operator"), verified: true)

# Students
User.create!(email: "stud.student@student.hs-rm.de", firstname: "Student", lastname: "Student", password_digest: BCrypt::Password.create("stud"), verified: true)
User.create!(email: "stud2.student@student.hs-rm.de", firstname: "Student2", lastname: "Student", password_digest: BCrypt::Password.create("stud"), verified: true)

# Students with jobs
students = []
10.times do
  firstname = Faker::Name.unique.first_name
  lastname = Faker::Name.unique.last_name
  # created_at = Faker::Time.backward(days: 60, period: :day)
  created_at = Faker::Time.between_dates(from: Date.today - 60, to: Date.today - 30, period: :day)
  email=I18n.transliterate "#{firstname}.#{lastname}@student.hs-rm.de"
  puts "Create Student: " + email
  students << User.new(email: email, firstname: firstname, lastname: lastname, password_digest: BCrypt::Password.create("password"), verified: true, created_at: created_at)
  students.last.save!
end

# Students without jobs
100.times do
  firstname = Faker::Name.unique.first_name
  lastname = Faker::Name.unique.last_name
  created_at = Faker::Time.backward(days: 60, period: :day)
  # created_at = Faker::Time.between_dates(from: Date.today - 60, to: Date.today, period: :day)
  email=I18n.transliterate "#{firstname}.#{lastname}@student.hs-rm.de"
  puts "Create Student: " + email
  User.new(email: email, firstname: firstname, lastname: lastname, password_digest: BCrypt::Password.create("password"), verified: true, created_at: created_at).save!
end

# Students without jobs not validated email
10.times do
  firstname = Faker::Name.unique.first_name
  lastname = Faker::Name.unique.last_name
  created_at = Faker::Time.backward(days: 60, period: :day)
  # created_at = Faker::Time.between_dates(from: Date.today - 60, to: Date.today, period: :day)
  email=I18n.transliterate "#{firstname}.#{lastname}@student.hs-rm.de"
  puts "Create Student: " + email
  User.new(email: email, firstname: firstname, lastname: lastname, password_digest: BCrypt::Password.create("password"), verified: false, created_at: created_at).save!
end

# Jobs paid (and some canceled) in the far past
10.times do
  [ 'GanzWichtig.pdf', 'IchBinIn5MinDran.pdf', 'DerPlanDerImmerProblemeMacht.pdf',
  'DieFarbenGefallenMirNicht.pdf', 'MachHinIchHabsEilig.pdf', 'WarumDauertDasSoLange.pdf',
  'DenPlanBezahleIchNicht.pdf', 'IchWarAlsErstesDran.pdf', 'WarumIstDerPlotterDefekt.pdf',
  'DasNächsteMalGeheIchWoAndersHin.pdf' ].shuffle.each do |pdf|
    status = %i[paid paid paid paid paid paid paid canceled].sample
    created_at = Faker::Time.backward(days: 1.year.in_days, period: :day)
    if status == :paid
      printed_at = created_at + 30.minutes
      paid_at = created_at + 45.minutes
      status_changed_at = paid_at
      updated_at = status_changed_at
    else
      updated_at = created_at + rand(4..44).minutes
      status_changed_at = updated_at
    end
    job = Job.new(status:, privacy_policy: true)
    job.pdf = File.open(Rails.root.join('db/pdfs/', pdf))
    student = students[rand(0..9)]
    job.customer = student
    job.creator = student
    operator = operators[rand(0...1)]
    job.operator = operator if status != :open
    job.cashier = operator if status == :paid
    job.save!
    job.update_column :created_at, created_at # write with update_column to avoid before_save action
    job.update_column :printed_at, printed_at # write with update_column to avoid before_save action
    job.update_column :status_changed_at, status_changed_at # write with update_column to avoid before_save action
    job.update_column :paid_at, paid_at if status == :paid # write with update_column to avoid before_save action
    job.update_column :updated_at, updated_at # write with update_column to avoid before_save action
  end
end


# Jobs paid (and some canceled) in the past
2.times do
  [ 'GanzWichtig.pdf', 'IchBinIn5MinDran.pdf', 'DerPlanDerImmerProblemeMacht.pdf',
  'DieFarbenGefallenMirNicht.pdf', 'MachHinIchHabsEilig.pdf', 'WarumDauertDasSoLange.pdf',
  'DenPlanBezahleIchNicht.pdf', 'IchWarAlsErstesDran.pdf', 'WarumIstDerPlotterDefekt.pdf',
  'DasNächsteMalGeheIchWoAndersHin.pdf' ].shuffle.each do |pdf|
    status = %i[paid paid paid paid paid paid paid canceled].sample
    created_at = Faker::Time.backward(days: 5, period: :day)
    if status == :paid
      printed_at = created_at + 30.minutes
      paid_at = created_at + 45.minutes
      status_changed_at = paid_at
      updated_at = status_changed_at
    else
      updated_at = created_at + rand(4..44).minutes
      status_changed_at = updated_at
    end
    job = Job.new(status:, privacy_policy: true)
    job.pdf = File.open(Rails.root.join('db/pdfs/', pdf))
    student = students[rand(0...9)]
    job.customer = student
    job.creator = student
    operator = operators[rand(0...1)]
    job.operator = operator if status == :paid
    job.cashier = operator if status == :paid
    job.save!
    job.update_column :created_at, created_at # write with update_column to avoid before_save action
    job.update_column :printed_at, printed_at # write with update_column to avoid before_save action
    job.update_column :status_changed_at, status_changed_at # write with update_column to avoid before_save action
    job.update_column :paid_at, paid_at if status == :paid # write with update_column to avoid before_save action
    job.update_column :updated_at, updated_at # write with update_column to avoid before_save action
  end
end

# Jobs
3.times do |i|
  status_pool = %i[canceled open open printing printing pickup pickup paid paid paid]
  [ 'GanzWichtig.pdf', 'IchBinIn5MinDran.pdf', 'DerPlanDerImmerProblemeMacht.pdf',
  'DieFarbenGefallenMirNicht.pdf', 'MachHinIchHabsEilig.pdf', 'WarumDauertDasSoLange.pdf',
  'DenPlanBezahleIchNicht.pdf', 'IchWarAlsErstesDran.pdf', 'WarumIstDerPlotterDefekt.pdf',
  'DasNächsteMalGeheIchWoAndersHin.pdf' ].shuffle.each do |pdf|
    status = status_pool.pop
    status = :open if i > 0
    job = Job.new(status:, privacy_policy: true)
    job.pdf = File.open(Rails.root.join('db/pdfs/', pdf))
    student = students[rand(0...4)]
    job.customer = student
    job.creator = student
    operator = operators[rand(0...1)]
    job.operator = operator if status != :open
    job.cashier = operator if status == :paid
    job.save!
  end
end

# Jobs created from operator
[ 'GanzWichtig.pdf', 'IchBinIn5MinDran.pdf', 'DerPlanDerImmerProblemeMacht.pdf',
'DieFarbenGefallenMirNicht.pdf', 'MachHinIchHabsEilig.pdf', 'WarumDauertDasSoLange.pdf',
'DenPlanBezahleIchNicht.pdf', 'IchWarAlsErstesDran.pdf', 'WarumIstDerPlotterDefekt.pdf',
'DasNächsteMalGeheIchWoAndersHin.pdf' ].shuffle.each do |pdf|
  job = Job.new(privacy_policy: true)
  job.pdf = File.open(Rails.root.join('db/pdfs/', pdf))
  student = students[rand(0...9)]
  job.customer = [ student, student, false ].sample
  job.customer_firstname = student.firstname
  job.customer_lastname = student.lastname
  job.creator = operators[rand(0...1)]
  job.created_by_operator = true
  job.inspect
  job.save!
end
