create table users(
       user_id bigserial primary key,
       email varchar(255) not null,
       username varchar(255) not null unique,
       key_key varchar(255),
       key_iv varchar(255)
);
create unique index users_username_idx on users (username);

create table instances(
       instance_id bigserial primary key,
       user_id integer not null references users on delete cascade,
       ssh_key_priv_enc text,
       ssh_key_pub_enc text,
       restic_password_enc varchar(255)
);
create unique index instances_user_id_instance_id_idx on instances (instance_id, user_id);

create table user_service_configs(
       id bigserial primary key,
       user_id integer not null references users on delete cascade,
       instance_id integer not null references instances on delete cascade,
       cloudflare_api_token_enc varchar(255),
       cloudflare_account_id_enc varchar(255),
       cloudflare_zone_id_enc varchar(255),
       digitalocean_api_token_enc varchar(255),
       digitalocean_region varchar(255),
       digitalocean_size varchar(255),
       digitalocean_volume_size integer,
       backblaze_application_key_enc varchar(255),
       backblaze_key_id_enc varchar(255),
       backblaze_bucket_url_enc varchar(255)
       );
create unique index user_service_configs_user_id_instance_id_idx on user_service_configs (user_id, instance_id);

create table user_selected_apps(
       id bigserial primary key,
       user_id integer not null references users on delete cascade,
       instance_id integer not null references instances on delete cascade,
       wg_easy_version varchar(100),
       nextcloud_version varchar(100),
       nassella_version varchar(100),
       log_viewer_version varchar(100),
       ghost_version varchar(100),
       instance_control_version varchar(100)
       );
create unique index user_selected_apps_user_id_instance_id_idx on user_selected_apps (user_id, instance_id);

create table user_app_configs(
       id bigserial primary key,
       user_id integer not null references users on delete cascade,
       instance_id integer not null references instances on delete cascade,
       root_domain varchar(100),
       config_enc text
       );

create unique index user_app_configs_user_id_instance_id_idx on user_app_configs (user_id, instance_id);


create type deployment_status as enum ('queued', 'in-progress', 'complete', 'failed');

create table deployments(
       id bigserial primary key,
       user_id integer not null references users on delete cascade,
       instance_id integer not null references instances on delete cascade,
       started timestamptz,
       finished timestamptz,
       status deployment_status not null default 'queued',
       pid integer,
       generate_configs deployment_status not null default 'queued',
       terraform_custom_image deployment_status not null default 'queued',
       terraform_dns deployment_status not null default 'queued',
       terraform_volume_create deployment_status not null default 'queued',
       terraform_volume_destroy deployment_status not null default 'queued',
       terraform_machine_create deployment_status not null default 'queued',
       terraform_machine_destroy deployment_status not null default 'queued',
       terraform_ip_create deployment_status not null default 'queued',
       terraform_ip_destroy deployment_status not null default 'queued',
       instance_backup deployment_status not null default 'queued',
       log_enc text
       );

create index deployments_user_id_instance_id_idx on deployments (user_id, instance_id);

create table user_terraform_state(
       id bigserial primary key,
       user_id integer not null references users on delete cascade,
       instance_id integer not null references instances on delete cascade,
       state_enc text,
       state_backup_enc text
       );

create unique index user_terraform_state_user_id_instance_id_idx on user_terraform_state (user_id, instance_id);

create table migrations(
       id bigserial primary key,
       migration_id integer not null unique,
       );
