
CREATE TABLE company (
	id INTEGER NOT NULL, 
	name VARCHAR NOT NULL, 
	commission FLOAT NOT NULL, 
	agent_commission FLOAT NOT NULL, 
	"key" VARCHAR NOT NULL, 
	create_dttm DATETIME NOT NULL, 
	update_dttm DATETIME NOT NULL, 
	auto_approve_transactions BOOLEAN NOT NULL, 
	PRIMARY KEY (id)
)

;


CREATE TABLE account (
	id INTEGER NOT NULL, 
	login VARCHAR NOT NULL, 
	password_hash VARCHAR NOT NULL, 
	"firstName" VARCHAR, 
	surname VARCHAR, 
	phone VARCHAR, 
	email VARCHAR, 
	company_id INTEGER NOT NULL, 
	create_dttm DATETIME NOT NULL, 
	update_dttm DATETIME NOT NULL, 
	PRIMARY KEY (id), 
	FOREIGN KEY(company_id) REFERENCES company (id)
)

;


CREATE TABLE company_balances (
	id INTEGER NOT NULL, 
	company_id INTEGER NOT NULL, 
	available_balance FLOAT NOT NULL, 
	pending_balance FLOAT NOT NULL, 
	updated_dttm DATETIME NOT NULL, 
	PRIMARY KEY (id), 
	UNIQUE (company_id), 
	FOREIGN KEY(company_id) REFERENCES company (id)
)

;


CREATE TABLE integrationtoken (
	id INTEGER NOT NULL, 
	description VARCHAR NOT NULL, 
	token_hash VARCHAR, 
	masked_token VARCHAR, 
	company_id INTEGER NOT NULL, 
	create_dttm DATETIME NOT NULL, 
	update_dttm DATETIME NOT NULL, 
	use_dttm DATETIME, 
	PRIMARY KEY (id), 
	FOREIGN KEY(company_id) REFERENCES company (id)
)

;


CREATE TABLE salecategory (
	id INTEGER NOT NULL, 
	category VARCHAR NOT NULL, 
	description VARCHAR, 
	perc FLOAT NOT NULL, 
	company_id INTEGER NOT NULL, 
	create_dttm DATETIME NOT NULL, 
	update_dttm DATETIME NOT NULL, 
	PRIMARY KEY (id), 
	FOREIGN KEY(company_id) REFERENCES company (id)
)

;


CREATE TABLE tgagent (
	id INTEGER NOT NULL, 
	tg_id INTEGER NOT NULL, 
	chat_id INTEGER, 
	phone VARCHAR, 
	name VARCHAR, 
	login VARCHAR, 
	hash VARCHAR, 
	company_id INTEGER NOT NULL, 
	create_dttm DATETIME NOT NULL, 
	update_dttm DATETIME NOT NULL, 
	PRIMARY KEY (id), 
	FOREIGN KEY(company_id) REFERENCES company (id)
)

;


CREATE TABLE agent_balances (
	id INTEGER NOT NULL, 
	tg_agent_id INTEGER NOT NULL, 
	available_balance FLOAT NOT NULL, 
	frozen_balance FLOAT NOT NULL, 
	updated_dttm DATETIME NOT NULL, 
	PRIMARY KEY (id), 
	UNIQUE (tg_agent_id), 
	FOREIGN KEY(tg_agent_id) REFERENCES tgagent (id)
)

;


CREATE TABLE agent_transactions (
	id INTEGER NOT NULL, 
	tg_agent_id INTEGER NOT NULL, 
	amount FLOAT NOT NULL, 
	status VARCHAR NOT NULL, 
	transaction_group CHAR(32) NOT NULL, 
	create_dttm DATETIME NOT NULL, 
	update_dttm DATETIME NOT NULL, 
	PRIMARY KEY (id), 
	FOREIGN KEY(tg_agent_id) REFERENCES tgagent (id), 
	UNIQUE (transaction_group)
)

;


CREATE TABLE ref (
	id INTEGER NOT NULL, 
	tg_agent_id INTEGER NOT NULL, 
	ref VARCHAR NOT NULL, 
	description VARCHAR, 
	promocode VARCHAR(8) NOT NULL, 
	create_dttm DATETIME NOT NULL, 
	update_dttm DATETIME NOT NULL, 
	PRIMARY KEY (id), 
	FOREIGN KEY(tg_agent_id) REFERENCES tgagent (id)
)

;


CREATE TABLE partner_transactions (
	id INTEGER NOT NULL, 
	company_id INTEGER NOT NULL, 
	type VARCHAR NOT NULL, 
	amount FLOAT NOT NULL, 
	status VARCHAR NOT NULL, 
	transaction_group CHAR(32) NOT NULL, 
	agent_transaction_id INTEGER, 
	create_dttm DATETIME NOT NULL, 
	update_dttm DATETIME NOT NULL, 
	PRIMARY KEY (id), 
	FOREIGN KEY(company_id) REFERENCES company (id), 
	FOREIGN KEY(agent_transaction_id) REFERENCES agent_transactions (id)
)

;


CREATE TABLE sale (
	id INTEGER NOT NULL, 
	cost FLOAT NOT NULL, 
	crediting FLOAT NOT NULL, 
	ref INTEGER NOT NULL, 
	sale_id VARCHAR NOT NULL, 
	company_id INTEGER NOT NULL, 
	category INTEGER NOT NULL, 
	sale_dttm DATETIME NOT NULL, 
	create_dttm DATETIME NOT NULL, 
	update_dttm DATETIME NOT NULL, 
	PRIMARY KEY (id), 
	FOREIGN KEY(ref) REFERENCES ref (id), 
	FOREIGN KEY(company_id) REFERENCES company (id), 
	FOREIGN KEY(category) REFERENCES salecategory (id)
)

;