-- Level 0.
create table if not exists Applications(
	id integer not null primary key autoincrement,
	name varchar(63) not null,
	date_in datetime not null default (datetime('now')),
	date_out datetime
);

create table if not exists Users(
	id integer not null primary key autoincrement,
	date_in datetime not null default (datetime('now')),
	date_out datetime
);

create table if not exists Ips(
	id integer not null primary key autoincrement,
	address varchar(39) not null,
	date_in datetime not null default (datetime('now')),
	date_out datetime
);

create table if not exists Files(
	id integer not null primary key autoincrement,
	address varchar(39) not null,
	date_in datetime not null default (datetime('now')),
	date_out datetime
);

-- Level 1.
create table if not exists Methods(
	id integer not null primary key autoincrement,
	application integer not null,
	name varchar(63) not null,
	date_in datetime not null default (datetime('now')),
	date_out datetime,
	constraint methods_ip foreign key(application) references Applications(id)
);

create table if not exists Sessions(
	id integer not null primary key autoincrement,
	'user' integer not null,
	ip integer not null,
	date_last datetime not null default (datetime('now')),
	date_in datetime not null default (datetime('now')),
	date_out datetime,
	constraint sessions_user foreign key('user') references Users(id),
	constraint sessions_ip foreign key(ip) references Ips(id)
);

-- Level 2.
create table if not exists Logs(
	id integer not null primary key autoincrement,
	'session' integer,
	'method' integer not null,
	parameters text not null,
	error bigint not null,
	date_in datetime not null default (datetime('now')),
	date_out datetime,
	constraint logs_session foreign key('session') references Sessions(id),
	constraint logs_method foreign key('method') references Methods(id)
);

create table if not exists Traces(
	id integer not null primary key autoincrement,
	'session' integer,
	'method' integer not null,
	parameters text not null,
	error bigint not null,
	trace text not null,
	message text not null,
	date_in datetime not null default (datetime('now')),
	date_out datetime,
	constraint traces_session foreign key('session') references Sessions(id),
	constraint traces_method foreign key('method') references Methods(id)
);

create table if not exists UsersData(
	id integer not null primary key autoincrement,
	'session' integer,
	'method' integer not null,
	'user' integer not null,
	name varchar(31) not null,
	password varchar(255) not null,
	accessible boolean not null default false,
	deleted datetime,
	date_in datetime not null default (datetime('now')),
	date_out datetime,
	constraint users_session foreign key('session') references Sessions(id),
	constraint users_method foreign key('method') references Methods(id),
	constraint users_user foreign key('user') references Users(id)
);

create table if not exists IpsData(
	id integer not null primary key autoincrement,
	'session' integer,
	'method' integer not null,
	ip integer not null,
	address varchar(39) not null,
	accessible boolean not null default true,
	deleted datetime,
	date_in datetime not null default (datetime('now')),
	date_out datetime,
	constraint ips_session foreign key('session') references Sessions(id),
	constraint ips_method foreign key('method') references Methods(id),
	constraint ips_ip foreign key(ip) references Ips(id)
);

create table if not exists FilesData(
	id integer not null primary key autoincrement,
	'session' integer,
	'method' integer not null,
	file integer not null,
	'path' varchar(255) not null,
	deleted datetime,
	date_in datetime not null default (datetime('now')),
	date_out datetime,
	constraint files_session foreign key('session') references Sessions(id),
	constraint files_method foreign key('method') references Methods(id),
	constraint files_file foreign key(file) references Files(id)
);
