INSERT INTO public.organization ("name",created_at,"domain",enabled,theme,branding_colors,branding_image_login,branding_logo, email_from) VALUES 
  ('NextGen LMS','2019-11-23 11:52:03.000','education.holotrain.ro',true,'paper',NULL,'/assets/images/login/sparrow_bg_1.jpg', NULL, '')
;

INSERT INTO public.organization (id, "name",created_at,"domain",enabled,theme,branding_colors,branding_image_login,branding_logo, email_from) VALUES 
  (99, 'Backend',CURRENT_TIMESTAMP,'backend.holotrain.ro',true,'paper',NULL,'/assets/images/login/sparrow_bg_1.jpg',NULL, '')
;

INSERT INTO public.feature ("name",description,enabled,auto_assign) VALUES
	 ('Access.Reports','Can access reports',true,'Administrator,Organizator'),
	 ('Gestiune.Roluri','Adaugare Modificare Stergere Roluri',true,'Administrator'),
	 ('Gestiune.Permisiuni','Adaugare Modificare Stergere Permisiuni',true,'Administrator'),
	 ('Gestiune.Entitati','Adaugare Modificare Stergere Entitati',true,'Administrator'),
	 ('Inregistrare.Utilizatori','Inregistrare Utilizatori',true,'Administrator'),
	 ('Completare.Profil','Completare Profil Utilizator ',true,'Administrator,Organizator,Utilizator'),
	 ('General.Connect','Conectare la sistem',true,'Administrator,Organizator,Utilizator'),
	 ('Access.Administration','Gestiune setări și nomenclatoare de bază',true,'Administrator'),
	 ('Manage.Users','Gestiune utilizatori',true,'Administrator'),
	 ('Manage.Groups','Gestiune grupuri',true,'Administrator'),
	 ('Manage.Roles','Gestiune roluri',true,'Administrator'),
	 ('Manage.UserAttributes','Gestiune atribute utilizatori',true,'Administrator'),
	 ('Manage.PasswordPolicy','Gestiune politică parolă',true,'Administrator'),
	 ('Import.Users','Import utilizatori',true,'Administrator'),
	 ('Branding.Colors','Gestiune culori',true,'Administrator'),
	 ('Branding.Images','Gestiune imagini',true,'Administrator'),
	 ('Branding.Messages','Gestiune mesage branding',false,'Administrator'),
	 ('View.Administration','Vizualizare meniu administrare',true,'Administrator'),
	 ('Completare.Mesaje','Comunicare prin mesaje',true,'Administrator,Organizator,Utilizator'),
	 ('Gestionare.FireDiscutie','Adaugare/Modificare fire discutie',true,'Administrator,Organizator'),
	 ('Comunicare.Mesaje','Comunicare prin mesaje',true,'Administrator,Organizator,Utilizator'),
	 ('Marcare.Subiect','Marcare subiect public',true,'Administrator,Organizator'),
	 ('Vizualizare.Mesaje','Vizualizare mesaje publice',true,'Administrator,Organizator,Utilizator'),
	 ('Moderare.Mesaje','Moderare mesaje',true,'Administrator,Organizator'),
	 ('Marcare.SubiectInteres','Marcare subiecte interes',true,'Administrator,Organizator,Utilizator'),
	 ('Inchidere.FireDiscutie','Inchidere+arhivare fire discutie',true,'Administrator,Organizator'),
	 ('Access.Forum','Acces modul forum',true,'Administrator,Organizator,Utilizator'),
	 ('Access.LMS','Acces LMS',true,'Administrator,Organizator,Utilizator'),
	 ('Manage.LMS','Gestiune LMS',true,'Administrator,Organizator'),
	 ('Access.Backend','Acces the backend module',true,NULL)
;

INSERT INTO public."role" (name,organization_id,can_autoregister,needs_approval) 
  SELECT 'Administrator',id,false,false from organization where id < 99
;

INSERT INTO public."role" (name,organization_id,can_autoregister,needs_approval) 
  SELECT 'Organizator',id,false,false from organization where id < 99
;

INSERT INTO public."role" (name,organization_id,can_autoregister,needs_approval) 
  SELECT 'Utilizator',id,true,true from organization where id < 99
;

INSERT INTO public."role" (name,organization_id,can_autoregister,needs_approval) VALUES
  ('SuperAdmin',99,false,false)
;

INSERT INTO public.thread_category(name, organization_id) VALUES
  ('General', 1)
;

-- Features FOR SuperAdmin (Backend Access)
INSERT INTO public.role_feature (role_id,feature_id,organization_id)
  SELECT r.id, ft.id, r.organization_id FROM public.feature ft
  JOIN ROLE r ON 1=1
  WHERE r.name='SuperAdmin' AND ft.name IN ('General.Connect','Access.Backend')
;

-- Features for Administrator (Organization Access)
INSERT INTO public.role_feature (role_id,feature_id,organization_id)
  SELECT r.id, ft.id, r.organization_id FROM public.feature ft
  JOIN ROLE r ON 1=1
  WHERE ft.name <> 'Access.Backend' AND r.name='Administrator'
;

-- Features for Organizator (Organization Access)
INSERT INTO public.role_feature (role_id,feature_id,organization_id)
  SELECT r.id, ft.id, r.organization_id FROM public.feature ft
  JOIN ROLE r ON 1=1
  WHERE r.name='Organizator' and ft.name IN (
    'Access.Reports',
    'Completare.Profil',
    'General.Connect',
    'Completare.Mesaje',
    'Gestionare.FireDiscutie',
    'Comunicare.Mesaje',
    'Marcare.Subiect',
    'Vizualizare.Mesaje',
    'Moderare.Mesaje',
    'Marcare.SubiectInteres',
    'Access.Forum',
    'Access.LMS',
    'Manage.LMS'
  )
;

-- Features for User (Organization Access)
INSERT INTO public.role_feature (role_id,feature_id,organization_id)
  SELECT r.id, ft.id, r.organization_id FROM public.feature ft
  JOIN ROLE r ON 1=1
  WHERE r.name='Utilizator' and ft.name IN (
    'Access.Reports',
    'Completare.Profil',
    'General.Connect',
    'Completare.Mesaje',
    'Gestionare.FireDiscutie',
    'Comunicare.Mesaje',
    'Marcare.Subiect',
    'Vizualizare.Mesaje',
    'Marcare.SubiectInteres',
    'Access.Forum',
    'Access.LMS'
  )
;

INSERT INTO public."user" (first_name,last_name,email,"password",auth_token,auth_series,created_at,enabled,role_id,organization_id,password_expired,last_password_change,approved,approved_at) 
  SELECT 'Administrator','Holotrain','admin@education.holotrain.ro','$2y$10$7FBAZmv3A7.9XPkRby0/w.jEONVE6azySgDoeP8OPWgN4YBbcthDm',NULL,NULL,CURRENT_TIMESTAMP,true,r.id,r.organization_id,false,NULL,true,null from role r
  WHERE name='Administrator'
;

INSERT INTO public."user" (first_name,last_name,email,"password",auth_token,auth_series,created_at,enabled,role_id,organization_id,password_expired,last_password_change,approved,approved_at) 
  SELECT 'Organizator','Holotrain','organizator@education.holotrain.ro','$2y$10$7FBAZmv3A7.9XPkRby0/w.jEONVE6azySgDoeP8OPWgN4YBbcthDm',NULL,NULL,CURRENT_TIMESTAMP,true,r.id,r.organization_id,false,NULL,true,null from role r
  WHERE name='Organizator'
;

INSERT INTO public."user" (first_name,last_name,email,"password",auth_token,auth_series,created_at,enabled,role_id,organization_id,password_expired,last_password_change,approved,approved_at) 
  SELECT 'Utilizator','Holotrain','utilizator@education.holotrain.ro','$2y$10$7FBAZmv3A7.9XPkRby0/w.jEONVE6azySgDoeP8OPWgN4YBbcthDm',NULL,NULL,CURRENT_TIMESTAMP,true,r.id,r.organization_id,false,NULL,true,null from role r
  WHERE name='Utilizator'
;

INSERT INTO public."user" (first_name,last_name,email,"password",auth_token,auth_series,created_at,enabled,role_id,organization_id,password_expired,last_password_change,approved,approved_at) 
  SELECT 'SuperAdmin','Holotrain','superadmin@holotrain.ro','$2y$10$7FBAZmv3A7.9XPkRby0/w.jEONVE6azySgDoeP8OPWgN4YBbcthDm',NULL,NULL,CURRENT_TIMESTAMP,true,r.id,r.organization_id,false,NULL,true,null 
  FROM role r WHERE name ='SuperAdmin'
;

INSERT INTO public.course_category ("name",description,enabled,parent_id,"order",organization_id) VALUES
     ('General','Cursuri generale',1,NULL,1,1);

INSERT INTO public.customer_type ("name",created_at) VALUES
	 ('Master',CURRENT_TIMESTAMP),
	 ('Sucursală',CURRENT_TIMESTAMP)
;

-- SETUP FOR LOCAL ACCESS
UPDATE organization SET DOMAIN=REPLACE(DOMAIN, '.ro', '.local:8080')