-- Version 0.5.5.1 Removed character length restrictions as the docs for POSTGRESQL recommend not specifying a limit.
-- Version 0.6.0.0 Changed the numeric values to integers as Golang doesn't have a native decimal type.

DROP TABLE IF EXISTS property_value;
DROP TABLE IF EXISTS property;
DROP TABLE IF EXISTS property_data_type;
DROP TABLE IF EXISTS item_component;
DROP TABLE IF EXISTS item;
DROP TABLE IF EXISTS subcategory;
DROP TABLE IF EXISTS category;

CREATE TABLE property_data_type (
	property_data_type_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
	property_data_type CHARACTER VARYING UNIQUE NOT NULL
);

INSERT INTO property_data_type (property_data_type) VALUES ('Money');
INSERT INTO property_data_type (property_data_type) VALUES ('Text');
INSERT INTO property_data_type (property_data_type) VALUES ('DateTime');
INSERT INTO property_data_type (property_data_type) VALUES ('Number');

CREATE TABLE category (
	category_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
	category_name CHARACTER VARYING UNIQUE NOT NULL
);

CREATE TABLE subcategory (
	subcategory_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
	subcategory_name CHARACTER VARYING UNIQUE NOT NULL,
	category_id INT NOT NULL REFERENCES category(category_id)
);

CREATE TABLE item (
	item_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
	item_name CHARACTER VARYING NOT NULL,
	value_cents INT DEFAULT 0,
	purchase_cost_cents INT DEFAULT 0,
	date_aquired TIMESTAMP WITH TIME ZONE,
	description CHARACTER VARYING,
	notes CHARACTER VARYING,
	sold_for_cents INT DEFAULT 0,
	sold_to CHARACTER VARYING, -- Could be a pointer TO a TABLE OF comtacts.
	category_id INT NOT NULL REFERENCES category(category_id),
	subcategory_id INT REFERENCES subcategory(subcategory_id)
);

-- Example: property_name of "Author" which maps to property_data_type of "Text".
CREATE TABLE property (
	property_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
	property_name CHARACTER varying NOT NULL,
	property_type_id INT NOT NULL REFERENCES property_data_type(property_data_type_id),
	category_id INT NOT NULL REFERENCES category(category_id),
	subcategory_id INT REFERENCES subcategory(subcategory_id)
);

-- Allows grouping items together, i.e. Alienware Desktop -> CPU, HDDs / SSDs, Motherboard, etc.
CREATE TABLE item_component (
	item_component_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
	item_id INT NOT NULL REFERENCES item(item_id),
	item_subcomponent_id INT NOT NULL REFERENCES item(item_id)
);

CREATE TABLE property_value (
	property_value_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
	item_id INT NOT NULL REFERENCES item(item_id),
	property_id INT NOT NULL REFERENCES property(property_id),
	value CHARACTER VARYING NOT NULL
);

INSERT INTO category (category_name) VALUES ('Books');
INSERT INTO category (category_name) VALUES ('Electronics');
INSERT INTO category (category_name) VALUES ('Appliances');
INSERT INTO category (category_name) VALUES ('Kitchenware');

INSERT INTO subcategory(subcategory_name, category_id) VALUES ('PCIe Cards', 2);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('Laptops', 2);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('Desktops', 2);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('CPUs', 2);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('Hard Drives', 2);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('Switches', 2);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('Routers', 2);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('CPU Coolers', 2);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('USB Devices', 2);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('Oscilloscopes', 2);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('Heaters', 3);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('Eletric Kettles', 4);