/* Precision for all money amounts assumes cents are excluded. */

CREATE TABLE comparison (
	id 				INT AUTO_INCREMENT,
	PRIMARY KEY (id)
);

CREATE TABLE branch (
	id 				INT AUTO_INCREMENT,
	type 			ENUM('NMLS', 'FSRA'),
	num 			VARCHAR(40) NOT NULL,
	PRIMARY KEY (id)
);

CREATE TABLE user (
	id 				INT AUTO_INCREMENT,
	email 			VARCHAR(40) NOT NULL,
	first_name 		VARCHAR(30) NOT NULL,
	last_name 		VARCHAR(30) NOT NULL,
	password 		CHAR(64) NOT NULL,
	branch_id 		INT,
	/* The password should be a SHA256 hash in hex format. It's length doesn't
	 * vary */
	country 		ENUM('Canada', 'USA'),
	title 			ENUM('Loan Officer',
					'Branch Manager',
					'Mortgage Broker',
					'Other'),
	status 			ENUM('Trial',
					'Free',
					'Subscribed',
					'Branch Subscribed'),
	PRIMARY KEY (`id`),
	FOREIGN KEY (branch_id) REFERENCES branch(id)
	ON DELETE SET NULL
);

CREATE TABLE license (
	id 				INT AUTO_INCREMENT,
	user_id 		INT NOT NULL,
	type 			ENUM('NMLS', 'FSRA'),
	num 			VARCHAR(40) NOT NULL,
	PRIMARY KEY (`id`),
	FOREIGN KEY (user_id) REFERENCES user(id)
	ON DELETE CASCADE
	ON UPDATE RESTRICT
);

/* Officers or managers may need to create new loan types with custom settings.
 * Types with branch_id and user_id values of null will be general cases. */
CREATE TABLE loan_type (
	id 				INT AUTO_INCREMENT,
	branch_id 		INT,
	user_id 		INT,
	name 			VARCHAR(30),
	FOREIGN KEY (branch_id) REFERENCES branch(id),
	FOREIGN KEY (user_id) REFERENCES user(id),
	PRIMARY KEY (`id`)
);

CREATE TABLE borrower (
	id 				INT AUTO_INCREMENT,
	credit_score 	SMALLINT,
	monthly_income 	INT,
	num 			TINYINT, /* Number of people borrowing. */
	PRIMARY KEY (`id`)
);

CREATE TABLE estimate (
	id 				INT AUTO_INCREMENT,
	user_id 		INT NOT NULL,
	borrower_id 	INT NOT NULL,
	comparison_id  	INT,
	transaction 	ENUM('Purchase', 'Refinance'),
	loan_type_id 	INT NOT NULL,
	loan_amount 	INT NOT NULL,
	price 			INT NOT NULL,
	property 		ENUM('Single Family Detached',
					'Single Family Attached',
					'Condominium Lo-rise',
					'Condominium Hi-rise'),
	pud 			BOOLEAN, /* Property under development */
	term 			INT, /* In months */
	interest 		INT, /* Per year, precise to 2 decimals */
	hoi 			INT, /* Hazard insurance annual payments */
	mi_name 		VARCHAR(50), /* Mortgage insurance title shown in menu */
	mi_amount 		INT, /* Mortgage insurance amount */
	lender 			VARCHAR(30),
	PRIMARY KEY (`id`),
	FOREIGN KEY (loan_type_id) REFERENCES loan_type(id)
	ON UPDATE RESTRICT,
	FOREIGN KEY (comparison_id) REFERENCES comparison(id)
	ON DELETE CASCADE
	ON UPDATE RESTRICT,
	FOREIGN KEY (borrower_id) REFERENCES borrower(id)
);

/* template = true fees are saved for users or branches. If template or default
 * are true, estimate_id should be null. */
CREATE TABLE fee (
	id 				INT AUTO_INCREMENT NOT NULL,
	estimate_id 	INT,
	amount 			INT NOT NULL,
	type 			ENUM('Goverment', 'Title', 'Other'),
	notes 			VARCHAR(255),
	name 			VARCHAR(30),
	/* Group heading shown in report */
	category 		VARCHAR(60),
	PRIMARY KEY (`id`),
	FOREIGN KEY (estimate_id) REFERENCES estimate(id)
);

/* Templates to be reused by users or branches. Either user_id or branch_id must
 * be non-null. */
CREATE TABLE fee_template (
	id 				INT AUTO_INCREMENT,
	user_id 		INT,
	branch_id 		INT,
	amount 			INT NOT NULL,
	type 			ENUM('Goverment', 'Title', 'Other'),
	notes 			VARCHAR(255),
	name 			VARCHAR(30),
	/* Group heading shown in report */
	category 		VARCHAR(60),
	auto 			BOOLEAN,
	/* If fee should be automatically applied */
	PRIMARY KEY (`id`),
	FOREIGN KEY (user_id) REFERENCES user(id),
	FOREIGN KEY (branch_id) REFERENCES branch(id)
);