CREATE EXTENSION IF NOT EXISTS "postgis";
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS "fuzzystrmatch";

DROP TYPE IF EXISTS QUANTITY CASCADE;
CREATE TYPE QUANTITY AS ENUM ('lots', 'many', 'few', 'none');

DROP TYPE IF EXISTS CATEGORY CASCADE;
CREATE TYPE CATEGORY AS ENUM ('beverages', 'desserts', 'entrees', 'other');

DROP TABLE IF EXISTS food_items CASCADE;
CREATE TABLE food_items (
	id              uuid PRIMARY KEY,
	name            VARCHAR(100),
	place_id        VARCHAR(50),
	category        CATEGORY,
	images          VARCHAR,
	loc             geography(POINT,4326)
);

DROP TABLE IF EXISTS images CASCADE;
CREATE TABLE images (
    filename        VARCHAR(200) PRIMARY KEY,
    food_item_id    uuid REFERENCES food_items (id) ON DELETE CASCADE,
    username        VARCHAR(100),
    date            timestamp (1) with time zone
);

DROP TABLE IF EXISTS quantities CASCADE;
CREATE TABLE quantities (
	food_item_id    uuid REFERENCES food_items (id) ON DELETE CASCADE,
	date            timestamp (1) with time zone,
	quantity        QUANTITY,
	PRIMARY KEY(food_item_id, date)
);


CREATE INDEX IF NOT EXISTS food_loc_index ON food_items USING GIST ( loc );

DROP VIEW IF EXISTS latest_quantities;
CREATE VIEW latest_quantities AS SELECT food_item_id, quantity, date from quantities q1 where date = (
	SELECT max(date) FROM quantities q2 WHERE q1.food_item_id=q2.food_item_id
);

--INSERT INTO food_items (id, name, place_id, category, loc)
--  VALUES (
--		uuid_generate_v4(),
--		'Big John Cookies',
--		'ChIJf5QJFBK1RIYRjjfxZz9Z0O0',
--		'desserts',
--		ST_GeogFromText('SRID=4326;POINT(-97.7532464 30.270667599999996)')
--	);
--INSERT INTO food_items (id, name, place_id, category, loc)
--  VALUES (
--		uuid_generate_v4(),
--		'Jelly Filled Donuts',
--		'ChIJ72if-Qe1RIYRCzMucGEEdBA',
--		'desserts',
--		ST_GeogFromText('SRID=4326;POINT(-97.742308 30.263963300000004)')
--	);
--INSERT INTO food_items (id, name, place_id, category, loc)
--  VALUES (
--		uuid_generate_v4(),
--		'Spelt Brownies',
--		'ChIJG44vBQi1RIYRvWGHdYUolZY',
--		'desserts',
--		ST_GeogFromText('SRID=4326;POINT(-97.7419085 30.265019100000004)')
--	);
--INSERT INTO food_items (id, name, place_id, category, loc)
--  VALUES (
--		uuid_generate_v4(),
--		'Oatmeal Raisin Cookies',
--		'ChIJf5QJFBK1RIYRjjfxZz9Z0O0',
--		'desserts',
--		ST_GeogFromText('SRID=4326;POINT(-97.7532464 30.270667599999996)')
--	);
--INSERT INTO food_items (id, name, place_id, category, loc)
--  VALUES (
--		uuid_generate_v4(),
--		'Donuts with Sprinkles',
--		'ChIJ72if-Qe1RIYRCzMucGEEdBA',
--		'desserts',
--		ST_GeogFromText('SRID=4326;POINT(-97.742308 30.263963300000004)')
--	);
--INSERT INTO food_items (id, name, place_id, category, loc)
--  VALUES (
--		uuid_generate_v4(),
--		'Powdered Donuts',
--		'ChIJ72if-Qe1RIYRCzMucGEEdBA',
--		'desserts',
--		ST_GeogFromText('SRID=4326;POINT(-97.742308 30.263963300000004)')
--	);
--INSERT INTO food_items (id, name, place_id, category, loc)
--  VALUES (
--		uuid_generate_v4(),
--		'Snickerdoodles',
--		'ChIJr3szW6a1RIYRkM7LRpnBIO0',
--		'desserts',
--		ST_GeogFromText('SRID=4326;POINT(-97.73798459999999 30.266898599999998)')
--	);
--INSERT INTO food_items (id, name, place_id, category, loc)
--  VALUES (
--		uuid_generate_v4(),
--		'Pizza',
--		'ChIJr3szW6a1RIYRkM7LRpnBIO0',
--		'desserts',
--		ST_GeogFromText('SRID=4326;POINT(-97.73798459999999 30.266898599999998)')
--	);
--INSERT INTO quantities SELECT id, current_timestamp, 'many' FROM food_items;