DROP TABLE IF EXISTS ADDRESS CASCADE;


DROP TABLE IF EXISTS BUSINESS CASCADE;


DROP TABLE IF EXISTS BUSINESS_SERVICE CASCADE;


DROP TABLE IF EXISTS REVIEW CASCADE;


DROP TABLE IF EXISTS SERVICE CASCADE;


DROP TABLE IF EXISTS REVIEWER CASCADE;


DROP TABLE IF EXISTS CATEGORY CASCADE;


CREATE TABLE CATEGORY(CATEGORY_ID serial PRIMARY KEY,
	CATEGORY_NAME VARCHAR(150) NOT NULL);


CREATE TABLE REVIEWER(REVIEWER_ID serial PRIMARY KEY,
	REVIEWER_NAME VARCHAR(150),
	REVIEWER_VERIFIED boolean NOT NULL,
	REVIEWER_EMAIL VARCHAR(150) NOT NULL,
	REVIEWER_PASSWORD VARCHAR(150) NOT NULL);


CREATE TABLE BUSINESS(BUSINESS_ID serial PRIMARY KEY,
	BUSINESS_NAME VARCHAR(150) NOT NULL,
	BUSINESS_PHONE VARCHAR(150),
	BUSINESS_DESCR VARCHAR(500),
	BUSINESS_HOURS VARCHAR(1000),
	CATEGORY_ID integer NOT NULL,
	CONSTRAINT FK_BUSINESS_CATEGORY_ID
	FOREIGN KEY (CATEGORY_ID) REFERENCES CATEGORY(CATEGORY_ID));


CREATE TABLE ADDRESS(ADDRESS_ID serial, ADDRESS_STREET VARCHAR(150) NOT NULL,
	ADDRESS_POSTAL_CODE VARCHAR(150) NOT NULL,
	ADDRESS_CITY VARCHAR(150) NOT NULL,
	BUSINESS_ID integer NOT NULL,
	CONSTRAINT PK_ADDRESS_BUSINESS PRIMARY KEY(BUSINESS_ID,
		ADDRESS_ID),
	CONSTRAINT FK_BUSINESS_ID
	FOREIGN KEY (BUSINESS_ID) REFERENCES BUSINESS(BUSINESS_ID));


CREATE TABLE SERVICE(SERVICE_ID serial PRIMARY KEY,
	SERVICE_NAME VARCHAR(500) NOT NULL,
	CATEGORY_ID integer NOT NULL,
	CONSTRAINT FK_SERVICE_CATEGORY
	FOREIGN KEY (CATEGORY_ID) REFERENCES CATEGORY(CATEGORY_ID));


CREATE TABLE REVIEW(REVIEW_ID serial PRIMARY KEY,
	REVIEW_TITLE VARCHAR(150),
	REVIEW_TEXT VARCHAR(1000),
	REVIEW_STARS integer, BUSINESS_ID integer NOT NULL,
	ADDRESS_ID integer NOT NULL,
	REVIEWER_ID integer NOT NULL,
	review_timestamp timestamp default current_timestamp,
	CONSTRAINT FK_BUSINESS_ID
	FOREIGN KEY (BUSINESS_ID) REFERENCES BUSINESS(BUSINESS_ID),
		CONSTRAINT FK_ADDRESS_ID
	FOREIGN KEY (BUSINESS_ID,
		ADDRESS_ID) REFERENCES ADDRESS(BUSINESS_ID,
			ADDRESS_ID),
	CONSTRAINT FK_REVIEWER_ID
	FOREIGN KEY (REVIEWER_ID) REFERENCES REVIEWER(REVIEWER_ID));


CREATE TABLE BUSINESS_SERVICE(BUSINESS_ID integer NOT NULL,
	SERVICE_ID integer NOT NULL,
	CONSTRAINT PK_BUSINESS_SERVICE PRIMARY KEY (BUSINESS_ID,
	SERVICE_ID), CONSTRAINT FK_BS_BUSINESS_ID
	FOREIGN KEY (BUSINESS_ID) REFERENCES BUSINESS(BUSINESS_ID),
	CONSTRAINT FK_BS_SERVICE_ID
	FOREIGN KEY (SERVICE_ID) REFERENCES SERVICE(SERVICE_ID));