begin;
create table apps(
       id bigserial primary key,
       app_name varchar(255)
       );
create unique index apps_app_name_idx on apps (app_name);

insert into apps(app_name) values ('wg-easy');
insert into apps(app_name) values ('nextcloud');
insert into apps(app_name) values ('nassella');
insert into apps(app_name) values ('log-viewer');
insert into apps(app_name) values ('ghost');
insert into apps(app_name) values ('instance-control');

alter table user_selected_apps add app_id integer references apps on delete cascade;
alter table user_selected_apps add installed_version varchar(100);

create index user_selected_apps_app_id_user_id_idx on user_selected_apps(user_id, app_id);

insert into user_selected_apps (user_id, instance_id, app_id)
select usa.user_id, usa.instance_id, apps.id from user_selected_apps usa
join apps on apps.app_name='wg-easy'
where usa.wg_easy_version is not null;

insert into user_selected_apps (user_id, instance_id, app_id)
select usa.user_id, usa.instance_id, apps.id from user_selected_apps usa
join apps on apps.app_name='nextcloud'
where usa.nextcloud_version is not null;

insert into user_selected_apps (user_id, instance_id, app_id)
select usa.user_id, usa.instance_id, apps.id from user_selected_apps usa
join apps on apps.app_name='nassella'
where usa.nassella_version is not null;

insert into user_selected_apps (user_id, instance_id, app_id)
select usa.user_id, usa.instance_id, apps.id from user_selected_apps usa
join apps on apps.app_name='log-viewer'
where usa.log_viewer_version is not null;

insert into user_selected_apps (user_id, instance_id, app_id)
select usa.user_id, usa.instance_id, apps.id from user_selected_apps usa
join apps on apps.app_name='ghost'
where usa.ghost_version is not null;

insert into user_selected_apps (user_id, instance_id, app_id)
select usa.user_id, usa.instance_id, apps.id from user_selected_apps usa
join apps on apps.app_name='instance-control'
where usa.instance_control_version is not null;

update user_selected_apps set installed_version='15' from apps
where user_selected_apps.app_id=apps.id
and apps.app_name='wg-easy';

update user_selected_apps set installed_version='34' from apps
where user_selected_apps.app_id=apps.id
and apps.app_name='nextcloud';

update user_selected_apps set installed_version='6' from apps
where user_selected_apps.app_id=apps.id
and apps.app_name='ghost';

update user_selected_apps set installed_version='b0.0.1' from apps
where user_selected_apps.app_id=apps.id
and apps.app_name='nassella';

update user_selected_apps set installed_version='b0.0.1' from apps
where user_selected_apps.app_id=apps.id
and apps.app_name='instance-control';

update user_selected_apps set installed_version='10.6.6' from apps
where user_selected_apps.app_id=apps.id
and apps.app_name='log-viewer';

delete from user_selected_apps where wg_easy_version is not null;
delete from user_selected_apps where nextcloud_version is not null;
delete from user_selected_apps where nassella_version is not null;
delete from user_selected_apps where log_viewer_version is not null;
delete from user_selected_apps where ghost_version is not null;
delete from user_selected_apps where instance_control_version is not null;

commit;
