CREATE SCHEMA  IF NOT EXISTS STRONGLOOP DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;

USE STRONGLOOP;


CREATE TABLE STRONGLOOP.PRODUCT
    (ID VARCHAR(20) NOT NULL,
	NAME VARCHAR(64), 
	AUDIBLE_RANGE DECIMAL(12,2), 
	EFFECTIVE_RANGE DECIMAL(12,2), 
	ROUNDS DECIMAL(10,0), 
	EXTRAS VARCHAR(64), 
	FIRE_MODES VARCHAR(64), 
	PRIMARY KEY (ID));

CREATE TABLE STRONGLOOP.LOCATION 
    (ID VARCHAR(20) NOT NULL,
	STREET VARCHAR(20), 
	CITY VARCHAR(20), 
	ZIPCODE VARCHAR(20), 
	NAME VARCHAR(20), 
	PRIMARY KEY (ID));

CREATE TABLE STRONGLOOP.CUSTOMER
   (ID VARCHAR(20) NOT NULL, 
	NAME VARCHAR(40), 
	MILITARY_AGENCY VARCHAR(20), 
	PRIMARY KEY (ID));
	
CREATE TABLE STRONGLOOP.INVENTORY 
   (PRODUCT_ID VARCHAR(20) NOT NULL,
	LOCATION_ID VARCHAR(20) NOT NULL,
	AVAILABLE INT,
	TOTAL INT,
	PRIMARY KEY (PRODUCT_ID, LOCATION_ID),
	 CONSTRAINT PRODUCT_FK FOREIGN KEY (PRODUCT_ID)
	  REFERENCES STRONGLOOP.PRODUCT (ID),
	 CONSTRAINT LOCATION_FK FOREIGN KEY (LOCATION_ID)
	  REFERENCES STRONGLOOP.LOCATION (ID)
   ); 
   
CREATE TABLE STRONGLOOP.RESERVATION 
    (ID VARCHAR(20) NOT NULL,
	PRODUCT_ID VARCHAR(20) NOT NULL,
	LOCATION_ID VARCHAR(20) NOT NULL,
	CUSTOMER_ID VARCHAR(20) NOT NULL,
	QTY INT,
	STATUS VARCHAR(20), 
	RESERVE_DATE DATE, 
	PICKUP_DATE DATE, 
	RETURN_DATE DATE,
	PRIMARY KEY(ID),
	 CONSTRAINT RESERVATION_PRODUCT_FK FOREIGN KEY (PRODUCT_ID)
	  REFERENCES STRONGLOOP.PRODUCT (ID),
	 CONSTRAINT RESERVATION_LOCATION_FK FOREIGN KEY (LOCATION_ID)
	  REFERENCES STRONGLOOP.LOCATION (ID),
	 CONSTRAINT RESERVATION_CUSTOMER_FK FOREIGN KEY (CUSTOMER_ID)
	  REFERENCES STRONGLOOP.CUSTOMER (ID)
   );
	

 CREATE OR REPLACE VIEW STRONGLOOP.INVENTORY_VIEW
   (ID, PRODUCT_ID, PRODUCT_NAME, AUDIBLE_RANGE, EFFECTIVE_RANGE, ROUNDS, EXTRAS, FIRE_MODES, LOCATION_ID, LOCATION, CITY, ZIPCODE, AVAILABLE) AS
   SELECT
   CONCAT(CONCAT(P.ID, ':'), L.ID) AS ID,
   P.ID AS PRODUCT_ID,
   P.NAME AS PRODUCT_NAME,
   P.AUDIBLE_RANGE,
   P.EFFECTIVE_RANGE,
   P.ROUNDS,
   P.EXTRAS,
   P.FIRE_MODES,
   L.ID AS LOCATION_ID,
   L.NAME AS LOCATION,
   L.CITY,
   L.ZIPCODE,
   I.AVAILABLE

 FROM INVENTORY I, PRODUCT P, LOCATION L

 WHERE P.ID = I.PRODUCT_ID AND L.ID = I.LOCATION_ID;
 	