-- enable foreign key support in sqlite
--PRAGMA foreign_keys = ON;

-- part manufacturer
CREATE TABLE IF NOT EXISTS manufacturer (
	id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
	name TEXT NOT NULL UNIQUE, -- manufacturer (expanded version, without legal form)
	nick TEXT, -- manufacturer nickname
	partof INTEGER, -- if the manufacturer has been acquired or is part of another
	homepage TEXT, -- URL to home page
	search TEXT, -- URL to search page
	FOREIGN KEY (partof) REFERENCES manufacturer (id)
);

-- part distributor
CREATE TABLE IF NOT EXISTS distributor (
	id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
	name TEXT NOT NULL UNIQUE,
	homepage TEXT, -- URL to home page
	product_page TEXT -- URL to product page (%s is replace by sku)
);

-- part package
CREATE TABLE IF NOT EXISTS package (
	id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
	name TEXT NOT NULL UNIQUE,
	family TEXT -- if this package it part of a more general package family
);

-- the part itself
CREATE TABLE IF NOT EXISTS part (
	id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
	name TEXT NOT NULL UNIQUE,
	description TEXT, -- a short (searchable) description
	details TEXT, -- even more part details than just in the description
	manufacturer INTEGER,
	family INTEGER, -- if this part is part of a part family
	datasheet TEXT, -- URL to datasheet
	package INTEGER,
	pincount INTEGER,
	page TEXT, -- URL to product page
	FOREIGN KEY (manufacturer) REFERENCES manufacturer (id),
	FOREIGN KEY (family) REFERENCES part (id),
	FOREIGN KEY (package) REFERENCES package (id)
);

-- CAD drawing type
CREATE TABLE IF NOT EXISTS cad (
	id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
	name TEXT NOT NULL UNIQUE,
	extension TEXT
);

-- part drawing (symbol, footprint, ...)
CREATE TABLE IF NOT EXISTS drawing (
	id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
	cad INTEGER NOT NULL,
	path TEXT NOT NULL,
	option TEXT,
	FOREIGN KEY (cad) REFERENCES cad (id)
);

-- a part at a distributor
CREATE TABLE IF NOT EXISTS distribution (
	id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
	part INTEGER NOT NULL, -- the part
	distributor INTEGER NOT NULL, -- the part distributor
	sku TEXT NOT NULL, -- distributor part number
	FOREIGN KEY (part) REFERENCES part (id),
	FOREIGN KEY (distributor) REFERENCES distributor (id)
);

-- part stock at a distributor
CREATE TABLE IF NOT EXISTS stock (
	id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
	distribution INTEGER NOT NULL, -- the part
	stock INTEGER NOT NULL,
	FOREIGN KEY (distribution) REFERENCES distribution (id)
);

CREATE TABLE IF NOT EXISTS document (
	id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
	type TEXT -- document type
);

-- part attachments
CREATE TABLE IF NOT EXISTS attachement (
	id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
	part INTEGER NOT NULL, -- the part
	type INTEGER, -- attachment document type
	path TEXT NOT NULL, -- the path to the attachment
	priority INTEGER NOT NULL, -- in which place the attachment should be displayed
	FOREIGN KEY (part) REFERENCES part (id),
	FOREIGN KEY (type) REFERENCES document (id),
	UNIQUE (part, priority)
);

-- property unit
CREATE TABLE IF NOT EXISTS unit (
	id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
	name TEXT NOT NULL UNIQUE,
	symbol TEXT NOT NULL UNIQUE
);

-- part property
CREATE TABLE IF NOT EXISTS property (
	id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
	name TEXT NOT NULL, -- property name (min V, ...)
	unit INTEGER,
	FOREIGN KEY (unit) REFERENCES unit (id)
);

-- property value
CREATE TABLE IF NOT EXISTS poperty_value (
	id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
	part INTEGER NOT NULL, -- the part
	property INTEGER NOT NULL,
	value TEXT,
	FOREIGN KEY (part) REFERENCES part (id),
	FOREIGN KEY (property) REFERENCES property (id)
);

-- part location
CREATE TABLE IF NOT EXISTS location (
	id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
	name TEXT NOT NULL UNIQUE,
	container TEXT -- container type
);

-- local stock
CREATE TABLE IF NOT EXISTS inventory (
	id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
	part INTEGER NOT NULL,
	location INTEGER,
	quantity INTEGER NOT NULL,
	FOREIGN KEY (part) REFERENCES part (id),
	FOREIGN KEY (location) REFERENCES location (id),
	UNIQUE (part, location)
);
