# 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

# Conference.find_or_create_by(slug: "rp2023").tap do |c|
#   c.name = "re:publica 2023"
#   c.time_zone = "Berlin"
#   c.starts_at = DateTime.parse("Mon, 05 Jun 2023 08:30:00.000000000 UTC +00:00")
#   c.ends_at = DateTime.parse("Wed, 07 Jun 2023 20:50:00.000000000 UTC +00:00")
#   c.data = {
#     "speakers_url" => "https://re-publica.com/sites/default/files/extappdata/2023/speaker.json",
#     "sessions_url" => "https://re-publica.com/sites/default/files/extappdata/2023/session.json"
#   }
#   c.import_job_class = "republica_2023_or_later"
#   c.location = "Arena Berlin"
#   c.save!
# end

# Conference.find_or_create_by(slug: "rp2024").tap do |c|
#   c.name = "re:publica 2024"
#   c.time_zone = "Berlin"
#   c.starts_at = DateTime.parse("27 May 2024 10:30 CEST")
#   c.ends_at = DateTime.parse("29 May 2024 19:45 CEST")
#   c.data = {
#     "speakers_url" => "https://re-publica.com/sites/default/files/extappdata/2024/speaker.json",
#     "sessions_url" => "https://re-publica.com/sites/default/files/extappdata/2024/session.json"
#   }
#   c.import_job_class = "republica_2023_or_later"
#   c.location = "STATION Berlin"
#   c.save!
# end

# Conference.find_or_create_by(slug: "37c3").tap do |c|
#   c.name = "37th Chaos Communication Congress"
#   c.time_zone = "Berlin"
#   c.starts_at = DateTime.parse("27 December 2023 10:30 CET")
#   c.ends_at = DateTime.parse("30 December 2023 19:00 CET")
#   c.data = {
#     "schedule_url" => "https://fahrplan.events.ccc.de/congress/2023/fahrplan/schedule.json"
#   }
#   c.import_job_class = "pretalx"
#   c.location = "Congress Center Hamburg"
#   c.save!
# end

# Conference.find_or_create_by(slug: "38c3").tap do |c|
#   c.name = "38th Chaos Communication Congress (de-en)"
#   c.time_zone = "Berlin"
#   c.starts_at = DateTime.parse("27 December 2024 10:30 CET")
#   c.ends_at = DateTime.parse("30 December 2024 19:00 CET")
#   c.data = {
#     "heartbeat_url" => fetch_credential("heartbeat_deen"),
#     "schedule_url" => "https://api.events.ccc.de/congress/2024/assembly/6840c453-af5c-413c-8127-adcbdcd98e9e/schedule.json",
#     "filedrop_url" => "https://speakers.c3lingo.org/",
#     "engelsystem_url" => "https://engel.events.ccc.de/api/v0-beta/"
#   }
#   c.import_job_class = "pretalx"
#   c.location = "Congress Center Hamburg"
#   c.save!
# end

# Conference.find_or_create_by(slug: "38c3-more").tap do |c|
#   c.name = "38th Chaos Communication Congress (more languages)"
#   c.more_languages = true
#   c.time_zone = "Berlin"
#   c.starts_at = DateTime.parse("27 December 2024 10:30 CET")
#   c.ends_at = DateTime.parse("30 December 2024 19:00 CET")
#   c.data = {
#     "heartbeat_url" => fetch_credential("heartbeat_more"),
#     "schedule_url" => "https://api.events.ccc.de/congress/2024/assembly/6840c453-af5c-413c-8127-adcbdcd98e9e/schedule.json",
#     "filedrop_url" => "https://speakers.c3lingo.org/",
#     "engelsystem_url" => "https://engel.events.ccc.de/api/v0-beta/"
#   }
#   c.import_job_class = "pretalx"
#   c.location = "Congress Center Hamburg"
#   c.save!
# end

# orga = Conference.find_or_create_by(slug: "38c3-orga").tap do |c|
#   c.name = "38th Chaos Communication Congress (internal shifts)"
#   c.time_zone = "Berlin"
#   c.starts_at = DateTime.parse("27 December 2024 10:30 CET")
#   c.ends_at = DateTime.parse("30 December 2024 19:00 CET")
#   c.location = "Congress Center Hamburg"
#   c.save!
# end
# stage_standby = Stage.find_or_create_by(conference: orga, ref_id: "c3lingo-stage-deen").tap do |stage_|
#   stage_.name = "C3Lingo Standby"
#   stage_.weight = 100
#   stage_.save!
# end
# stage_coordinator = Stage.find_or_create_by(conference: orga, ref_id: "c3lingo-stage-more").tap do |stage_|
#   stage_.name = "C3Lingo Coordinator"
#   stage_.weight = 200
#   stage_.save!
# end
# stage_service = Stage.find_or_create_by(conference: orga, ref_id: "c3lingo-stage-service").tap do |stage_|
#   stage_.name = "C3Lingo Service"
#   stage_.weight = 50
#   stage_.save!
# end
# orga.relevant_stages = orga.stages
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-01-20241227-1015").tap do |shift_|
#   shift_.stage = stage_standby
#   shift_.title = "Standby Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-27T10:15:00+01:00"
#   shift_.ends_at = "2024-12-27T14:15:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-02-20241227-1015").tap do |shift_|
#   shift_.stage = stage_coordinator
#   shift_.title = "Coordinator Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-27T10:15:00+01:00"
#   shift_.ends_at = "2024-12-27T14:15:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-01-20241228-1015").tap do |shift_|
#   shift_.stage = stage_standby
#   shift_.title = "Standby Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-28T10:15:00+01:00"
#   shift_.ends_at = "2024-12-28T14:15:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-02-20241228-1015").tap do |shift_|
#   shift_.stage = stage_coordinator
#   shift_.title = "Coordinator Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-28T10:15:00+01:00"
#   shift_.ends_at = "2024-12-28T14:15:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-01-20241229-1015").tap do |shift_|
#   shift_.stage = stage_standby
#   shift_.title = "Standby Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-29T10:15:00+01:00"
#   shift_.ends_at = "2024-12-29T14:15:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-02-20241229-1015").tap do |shift_|
#   shift_.stage = stage_coordinator
#   shift_.title = "Coordinator Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-29T10:15:00+01:00"
#   shift_.ends_at = "2024-12-29T14:15:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-01-20241230-1015").tap do |shift_|
#   shift_.stage = stage_standby
#   shift_.title = "Standby Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-30T10:15:00+01:00"
#   shift_.ends_at = "2024-12-30T14:15:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-02-20241230-1015").tap do |shift_|
#   shift_.stage = stage_coordinator
#   shift_.title = "Coordinator Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-30T10:15:00+01:00"
#   shift_.ends_at = "2024-12-30T14:15:00+01:00"
#   shift_.save!
# end


# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-01-20241227-1415").tap do |shift_|
#   shift_.stage = stage_standby
#   shift_.title = "Standby Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-27T14:15:00+01:00"
#   shift_.ends_at = "2024-12-27T18:15:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-02-20241227-1415").tap do |shift_|
#   shift_.stage = stage_coordinator
#   shift_.title = "Coordinator Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-27T14:15:00+01:00"
#   shift_.ends_at = "2024-12-27T18:15:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-01-20241228-1415").tap do |shift_|
#   shift_.stage = stage_standby
#   shift_.title = "Standby Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-28T14:15:00+01:00"
#   shift_.ends_at = "2024-12-28T18:15:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-02-20241228-1415").tap do |shift_|
#   shift_.stage = stage_coordinator
#   shift_.title = "Coordinator Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-28T14:15:00+01:00"
#   shift_.ends_at = "2024-12-28T18:15:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-01-20241229-1415").tap do |shift_|
#   shift_.stage = stage_standby
#   shift_.title = "Standby Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-29T14:15:00+01:00"
#   shift_.ends_at = "2024-12-29T18:15:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-02-20241229-1415").tap do |shift_|
#   shift_.stage = stage_coordinator
#   shift_.title = "Coordinator Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-29T14:15:00+01:00"
#   shift_.ends_at = "2024-12-29T18:15:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-01-20241230-1415").tap do |shift_|
#   shift_.stage = stage_standby
#   shift_.title = "Standby Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-30T14:15:00+01:00"
#   shift_.ends_at = "2024-12-30T18:15:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-02-20241230-1415").tap do |shift_|
#   shift_.stage = stage_coordinator
#   shift_.title = "Coordinator Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-30T14:15:00+01:00"
#   shift_.ends_at = "2024-12-30T18:15:00+01:00"
#   shift_.save!
# end


# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-01-20241227-1900").tap do |shift_|
#   shift_.stage = stage_standby
#   shift_.title = "Standby Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-27T19:00:00+01:00"
#   shift_.ends_at = "2024-12-27T22:00:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-02-20241227-1900").tap do |shift_|
#   shift_.stage = stage_coordinator
#   shift_.title = "Coordinator Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-27T18:15:00+01:00"
#   shift_.ends_at = "2024-12-27T22:00:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-01-20241228-1900").tap do |shift_|
#   shift_.stage = stage_standby
#   shift_.title = "Standby Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-28T19:00:00+01:00"
#   shift_.ends_at = "2024-12-28T22:00:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-02-20241228-1900").tap do |shift_|
#   shift_.stage = stage_coordinator
#   shift_.title = "Coordinator Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-28T18:15:00+01:00"
#   shift_.ends_at = "2024-12-28T22:00:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-01-20241229-1900").tap do |shift_|
#   shift_.stage = stage_standby
#   shift_.title = "Standby Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-29T19:00:00+01:00"
#   shift_.ends_at = "2024-12-29T22:00:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-02-20241229-1900").tap do |shift_|
#   shift_.stage = stage_coordinator
#   shift_.title = "Coordinator Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-29T18:15:00+01:00"
#   shift_.ends_at = "2024-12-29T22:00:00+01:00"
#   shift_.save!
# end

# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-01-20241227-2200").tap do |shift_|
#   shift_.stage = stage_standby
#   shift_.title = "Standby Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-27T22:00:00+01:00"
#   shift_.ends_at = "2024-12-28T01:20:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-02-20241227-2200").tap do |shift_|
#   shift_.stage = stage_coordinator
#   shift_.title = "Coordinator Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-27T22:00:00+01:00"
#   shift_.ends_at = "2024-12-28T02:40:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-01-20241228-2200").tap do |shift_|
#   shift_.stage = stage_standby
#   shift_.title = "Standby Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-28T22:00:00+01:00"
#   shift_.ends_at = "2024-12-29T01:20:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-02-20241228-2200").tap do |shift_|
#   shift_.stage = stage_coordinator
#   shift_.title = "Coordinator Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-28T22:00:00+01:00"
#   shift_.ends_at = "2024-12-29T02:25:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-01-20241229-2200").tap do |shift_|
#   shift_.stage = stage_standby
#   shift_.title = "Standby Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-29T22:00:00+01:00"
#   shift_.ends_at = "2024-12-30T01:00:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-02-20241229-2200").tap do |shift_|
#   shift_.stage = stage_coordinator
#   shift_.title = "Coordinator Shift"
#   shift_.language = "en"
#   shift_.url = "https://rescheduled.c3lingo.org/"
#   shift_.starts_at = "2024-12-29T22:00:00+01:00"
#   shift_.ends_at = "2024-12-30T02:15:00+01:00"
#   shift_.save!
# end



# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-03-20241227").tap do |shift_|
#   shift_.stage = stage_service
#   shift_.title = "General Angel Meeting"
#   shift_.language = "de"
#   shift_.description = "Time and place are placeholders, please reconfirm before the start of the shift!"
#   shift_.url = "https://engel.events.ccc.de/"
#   shift_.starts_at = "2024-12-27T18:00:00+01:00"
#   shift_.ends_at = "2024-12-27T19:00:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-03-20241228").tap do |shift_|
#   shift_.stage = stage_service
#   shift_.title = "General Angel Meeting"
#   shift_.language = "de"
#   shift_.description = "Time and place are placeholders, please reconfirm before the start of the shift!"
#   shift_.url = "https://engel.events.ccc.de/"
#   shift_.starts_at = "2024-12-28T18:00:00+01:00"
#   shift_.ends_at = "2024-12-28T19:00:00+01:00"
#   shift_.save!
# end
# Session.find_or_create_by(conference: orga, ref_id: "c3-4242-03-20241229").tap do |shift_|
#   shift_.stage = stage_service
#   shift_.title = "General Angel Meeting"
#   shift_.language = "de"
#   shift_.description = "Time and place are placeholders, please reconfirm before the start of the shift!"
#   shift_.url = "https://engel.events.ccc.de/"
#   shift_.starts_at = "2024-12-29T18:00:00+01:00"
#   shift_.ends_at = "2024-12-29T19:00:00+01:00"
#   shift_.save!
# end


# if Rails.env.development?
#   %w[coverage fog comedy adjust forge fail vigorous promise chemistry conception meat storage certain warm develop civilian cousin injection hammer health appetite conventional good snake grant suspect atmosphere linen wrong deal calf sea management silence watch nuance loan quit convert failure bracket slice sweat treaty plot still chimpanzee assume functional marsh dream mail state dorm kid formation secular agile beach guide salesperson merit goalkeeper incongruous cart pig joystick regulation apparatus myth patent glue behead flu departure spectrum parking indication delay hesitate viable lay treat cooperative sensation auction sphere stain tap pass].each do |username|
#     User.find_or_create_by(name: username) do |u|
#       u.email = "c3lingo+#{username}@x.moeffju.net"
#       u.invitation_token = "gargamel"
#       u.save!
#     end
#   end
# end

if token = fetch_credential("telegram_bot_token")
  NotificationChannel.find_or_create_by(name: "telegram_group_chat").tap do |c|
    c.data = { token: token }
    c.save!
  end
end

# Create RBAC roles and permissions
shift_coordinator_role = Role.find_or_create_by(name: 'shift_coordinator') do |role|
  role.description = 'Can manage session assignments and scheduling'
end

events_admin_role = Role.find_or_create_by(name: 'events_admin') do |role|
  role.description = 'Can manage conferences and all sub-resources'
end

admin_role = Role.find_or_create_by(name: 'admin') do |role|
  role.description = 'Can manage users and assign roles'
end

# Create permissions
manage_assignments = Permission.find_or_create_by(name: 'manage_assignments') do |perm|
  perm.description = 'Can create and delete assignments'
end

manage_conferences = Permission.find_or_create_by(name: 'manage_conferences') do |perm|
  perm.description = 'Can create, edit, and delete conferences'
end

manage_sessions = Permission.find_or_create_by(name: 'manage_sessions') do |perm|
  perm.description = 'Can create, edit, and delete sessions'
end

manage_speakers = Permission.find_or_create_by(name: 'manage_speakers') do |perm|
  perm.description = 'Can create, edit, and delete speakers'
end

manage_stages = Permission.find_or_create_by(name: 'manage_stages') do |perm|
  perm.description = 'Can create, edit, and delete stages'
end

manage_users = Permission.find_or_create_by(name: 'manage_users') do |perm|
  perm.description = 'Can create, edit, and delete users'
end

assign_roles = Permission.find_or_create_by(name: 'assign_roles') do |perm|
  perm.description = 'Can assign roles to users'
end

# Associate permissions with roles (using find_or_create to avoid duplicates)
RolePermission.find_or_create_by(role: shift_coordinator_role, permission: manage_assignments)

RolePermission.find_or_create_by(role: events_admin_role, permission: manage_conferences)
RolePermission.find_or_create_by(role: events_admin_role, permission: manage_sessions)
RolePermission.find_or_create_by(role: events_admin_role, permission: manage_speakers)
RolePermission.find_or_create_by(role: events_admin_role, permission: manage_stages)

RolePermission.find_or_create_by(role: admin_role, permission: manage_users)
RolePermission.find_or_create_by(role: admin_role, permission: assign_roles)
RolePermission.find_or_create_by(role: admin_role, permission: manage_assignments)
RolePermission.find_or_create_by(role: admin_role, permission: manage_conferences)
RolePermission.find_or_create_by(role: admin_role, permission: manage_sessions)
RolePermission.find_or_create_by(role: admin_role, permission: manage_speakers)
RolePermission.find_or_create_by(role: admin_role, permission: manage_stages)