-- Copyright 2025-2026 Thomas Hintz

-- This file is part of Nassella.

-- Nassella is free software: you can redistribute it and/or modify it under the
-- terms of the GNU Affero General Public License as published by the Free
-- Software Foundation, either version 3 of the License, or (at your option) any
-- later version.

-- Nassella is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

-- You should have received a copy of the GNU Affero General Public License
-- along with Nassella. If not, see <https://www.gnu.org/licenses/>.

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 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');
insert into apps(app_name) values ('wordpress');
insert into apps(app_name) values ('lldap');
insert into apps(app_name) values ('authelia');

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),
       app_id integer not null references apps on delete cascade,
       installed_version varchar(100)
       );
create unique index user_selected_apps_app_id_user_id_instance_id_idx on user_selected_apps(user_id, app_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
       );

insert into migrations(migration_id) values(0);
insert into migrations(migration_id) values(1);
insert into migrations(migration_id) values(2);
insert into migrations(migration_id) values(3);
insert into migrations(migration_id) values(4);
insert into migrations(migration_id) values(5);
