-- +goose Up
-- +goose StatementBegin
SELECT 'up SQL query';

-- Users as auth data
create table if not exists users (
	id uuid primary key,
	username text default null,
	login text not null unique,
	password bytea not null,
	created_at timestamptz default current_timestamp,
	updated_at timestamptz default current_timestamp
);

create index idx_users_login on users (login);

create index idx_users_username on users (username);

-- Sessions and auth data
create table sessions (
	id uuid primary key,
	session_token varchar(200) not null unique,
	csrf_token varchar(200) not null unique,
	user_id uuid references users(id),
	created_at timestamp default current_timestamp,
	expired_at timestamp not null
);

create index if not exists idx_sessions_session_token_csrf_token on sessions (session_token, csrf_token);

-- Files
create table files_metadata (
	id uuid primary key, 
	name text not null, 
	fslink text not null, 
	size bigint not null, 
	ext text not null, 
	owner_id uuid not null,
	parent_dir uuid not null, 
	created_at timestamptz default current_timestamp, 
	updated_at timestamptz default null,
	deleted_at timestamptz default null
);

create index idx_fm_owner_id on files_metadata(owner_id); 
create index idx_fm_owner_id_parent_dir on files_metadata(owner_id, parent_dir); 

create table directories (
	id uuid primary key,
	name text not null, 
	owner_id uuid not null, 
	parent_dir uuid not null,
	created_at timestamptz default current_timestamp, 
	updated_at timestamptz default null,
	deleted_at timestamptz default null
);

create index idx_directories_owner_id_parent_dir on directories(owner_id, parent_dir);

create table directory_users_access (
	id uuid primary key,
	dir_id uuid not null,
	user_id uuid not null, 
	assess_flag integer,
	created_at timestamptz default current_timestamp, 
	updated_at timestamptz default null
);

create index idx_dua_owner_id_parent_dir on directories(owner_id, parent_dir);


