-- CREATE SCHEMA
/*
DROP USER "C##ART" CASCADE;

CREATE USER "C##ART" IDENTIFIED BY Welcome123;

GRANT connect, resource, DBA to "C##ART";
*/

-- ORACLE SQL SCHEMA DDL:

-- DROP TABLES FIRST; IF EXIST
SET ECHO ON;
SET SERVEROUTPUT ON;

DECLARE
      l_tab DBMS_UTILITY.uncl_array;
      l_tablen number;
    BEGIN
      dbms_utility.comma_to_table('CONTACT,PROFILE,ROLE,USERROLE,STAFF,ZIP,ADDRESS,DOCUMENT,PLAN,EXHIBIT,SUBSCRIPTION,AUTHORIZATION,INVENTORY,ORDER_ITEMS,INVOICE,TASK_HISTORY'
      , l_tablen, l_tab);
      for i in 1..l_tablen
      LOOP
	   BEGIN
	   EXECUTE IMMEDIATE 'DROP TABLE ' || l_tab(i) || ' CASCADE CONSTRAINTS';
	   DBMS_OUTPUT.put_line('DROPPED '  || l_tab(i) );
	   EXCEPTION
            WHEN OTHERS THEN NULL;
       END;
     END LOOP;

     DBMS_UTILITY.comma_to_table('ISEQ_CONTACT,ISEQ_ROLE,ISEQ_PROFILE,ISEQ_DOCUMENT', l_tablen, l_tab);
     for i in 1..l_tablen
     LOOP
	   BEGIN
       EXECUTE IMMEDIATE 'DROP SEQUENCE '|| l_tab(i);
       DBMS_OUTPUT.put_line('DROPPED '  || l_tab(i) );
	   EXCEPTION
            WHEN OTHERS THEN NULL;
       END;
     END LOOP;

     COMMIT;
   END;
/

CREATE SEQUENCE ISEQ_CONTACT START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE;

CREATE TABLE CONTACT (
	CONTACT_ID NUMBER DEFAULT ISEQ_CONTACT.nextval NOT NULL,
	FIRST_NAME VARCHAR2(16) NOT NULL,
	LAST_NAME VARCHAR2(16) NOT NULL,
	PHONE_NO VARCHAR2(12),
	EMAIL VARCHAR2(64) NOT NULL,
	MODIFIED_BY NUMBER,
	CREATED_DT TIMESTAMP DEFAULT SYSDATE,
	MODIFIED_DT TIMESTAMP DEFAULT SYSDATE,
	CONSTRAINT PK_CONTACT_ID PRIMARY KEY (CONTACT_ID),
	FOREIGN KEY (MODIFIED_BY) REFERENCES CONTACT(CONTACT_ID)
);

CREATE TABLE ZIP (
	ZIP VARCHAR2(5),
	CITY VARCHAR2(16),
	STATE VARCHAR2(2),
	CONSTRAINT PK_ZIP PRIMARY KEY (ZIP),
	CONSTRAINT ZIP_CHECK CHECK ( REGEXP_LIKE(ZIP, '\d{5}') )
);

CREATE TABLE ADDRESS (
	ZIP VARCHAR2(5) NOT NULL,
	FK_CONTACT_ID NUMBER NOT NULL,
	STREET VARCHAR2(32) NOT NULL,
	CONSTRAINT PK_ADDRESS PRIMARY KEY (ZIP, FK_CONTACT_ID),
	FOREIGN KEY (FK_CONTACT_ID) REFERENCES CONTACT(CONTACT_ID) ON DELETE CASCADE,
	FOREIGN KEY (ZIP) REFERENCES ZIP(ZIP)
);

CREATE SEQUENCE "ISEQ_ROLE" START WITH 100 INCREMENT BY 100 NOCACHE NOCYCLE;

CREATE TABLE ROLE (
	ROLE_ID NUMBER DEFAULT ISEQ_ROLE.nextval NOT NULL,
	ROLENAME VARCHAR2(16),
	CONSTRAINT PK_ROLE_ID PRIMARY KEY (ROLE_ID),
	CONSTRAINT check_role CHECK (ROLENAME IN ('SYSTEM', 'CONTACT', 'CUSTOMER', 'STAFF', 'SUPPLIER'))
);

CREATE SEQUENCE "ISEQ_PROFILE" START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE;

CREATE TABLE PROFILE (
	PROFILE_ID NUMBER DEFAULT ISEQ_PROFILE.nextval NOT NULL,
	FK_CONTACT_ID NUMBER NOT NULL,
	ACTIVE BOOLEAN DEFAULT FALSE,
	MODIFIED_BY NUMBER NOT NULL,
	CREATED_DT TIMESTAMP DEFAULT SYSDATE,
	MODIFIED_DT TIMESTAMP DEFAULT SYSDATE,
	CONSTRAINT PK_PROFILE_ID PRIMARY KEY(PROFILE_ID),
	FOREIGN KEY (FK_CONTACT_ID) REFERENCES CONTACT(CONTACT_ID) ON DELETE CASCADE,
	FOREIGN KEY(MODIFIED_BY) REFERENCES PROFILE(PROFILE_ID)
);

CREATE TABLE USERROLE (
	FK_PROFILE_ID NUMBER NOT NULL,
	FK_ROLE_ID NUMBER NOT NULL,
	PASSWORDHASH RAW(256) DEFAULT NULL,
	CONSTRAINT PK_USERROLE PRIMARY KEY (FK_ROLE_ID,FK_PROFILE_ID),
	FOREIGN KEY(FK_PROFILE_ID) REFERENCES PROFILE(PROFILE_ID) ON DELETE CASCADE,
	FOREIGN KEY(FK_ROLE_ID) REFERENCES ROLE(ROLE_ID) ON DELETE CASCADE
);

CREATE TABLE STAFF(
	FK_PROFILE_ID NUMBER NOT NULL,
	POSITION VARCHAR2(14) NOT NULL,
	CONSTRAINT check_position CHECK (POSITION IN ('Director', 'Curator',
	'Art Handler', 'Registrar', 'Associate')),
	CONSTRAINT PK_STAFF PRIMARY KEY (FK_PROFILE_ID),
    FOREIGN KEY(FK_PROFILE_ID) REFERENCES PROFILE(PROFILE_ID)
);

CREATE SEQUENCE ISEQ_DOCUMENT START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE;
CREATE TABLE DOCUMENT (
	DOCUMENT_ID NUMBER DEFAULT ISEQ_DOCUMENT.nextval NOT NULL,
	DOCUMENT_URL VARCHAR2(254) NOT NULL,
    CONSTRAINT PK_DOCUMENT PRIMARY KEY (DOCUMENT_ID)
);

CREATE TABLE PLAN (
	PLAN_ID NUMBER GENERATED ALWAYS AS IDENTITY,
	DESCRIPTION VARCHAR2(32),
	FK_DOCUMENT_ID NUMBER NOT NULL,
	Duration NUMBER, --in hours
	price NUMBER,
	MODIFIED_BY NUMBER,
	CREATED_DT TIMESTAMP DEFAULT SYSDATE,
	MODIFIED_DT TIMESTAMP DEFAULT SYSDATE,
	CONSTRAINT PK_PLAN PRIMARY KEY(PLAN_ID),
	FOREIGN KEY(FK_DOCUMENT_ID) REFERENCES DOCUMENT(DOCUMENT_ID)
);

CREATE TABLE SUBSCRIPTION (
	FK_PLAN_ID NUMBER,
	FK_CONTACT_ID NUMBER,
	MODIFIED_BY NUMBER,
	CREATED_DT TIMESTAMP DEFAULT SYSDATE,
	MODIFIED_DT TIMESTAMP DEFAULT SYSDATE,
	CONSTRAINT PK_SUBSCRIPTION PRIMARY KEY (FK_PLAN_ID, FK_CONTACT_ID),
	FOREIGN KEY(FK_CONTACT_ID) REFERENCES CONTACT(CONTACT_ID),
	FOREIGN KEY(FK_PLAN_ID) REFERENCES PLAN(PLAN_ID)
);

/* Once invoice is paid, external system inserts invoice uid. Insert/Update.*/
CREATE TABLE INVOICE (
    INVOICE_ID RAW(16) DEFAULT SYS_GUID() NOT NULL PRIMARY KEY,
    FK_PLAN_ID NUMBER,
    FK_CONTACT_ID NUMBER,
    InvoiceDate DATE,
    AmountPaid DECIMAL(10, 2) DEFAULT 0,
    FOREIGN KEY(FK_CONTACT_ID) REFERENCES CONTACT(CONTACT_ID),
    FOREIGN KEY(FK_PLAN_ID) REFERENCES PLAN(PLAN_ID)
);

CREATE TABLE AUTHORIZATION (
	AUTHORIZATION_ID NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
	FK_CONTACT_ID NUMBER,
	AUTHORIZATION_STATUS VARCHAR2(16) NOT NULL,
	AUTHORIZATION_DATE TIMESTAMP DEFAULT SYSDATE,
	EXPIRATION_DATE TIMESTAMP DEFAULT SYSDATE,
	FK_DOCUMENT_ID NUMBER,
	MODIFIED_BY NUMBER,
	CREATED_DT TIMESTAMP DEFAULT SYSDATE,
	MODIFIED_DT TIMESTAMP DEFAULT SYSDATE,
	FOREIGN KEY(FK_CONTACT_ID) REFERENCES CONTACT(CONTACT_ID),
	FOREIGN KEY(FK_DOCUMENT_ID) REFERENCES DOCUMENT(DOCUMENT_ID),
	CONSTRAINT check_authorization_status CHECK (AUTHORIZATION_STATUS IN ('pending', 'approved', 'rejected', 'expired'))
);

CREATE TABLE INVENTORY (
	INVENTORY_ID NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
	FK_CONTACT_ID NUMBER NOT NULL,
	AUTHORIZATION_ID NUMBER,
	SKU VARCHAR2(100) NOT NULL,
	PRODUCT_DESCRIPTION VARCHAR2(100) NOT NULL,
	UNIT_COST DECIMAL(10,2) NOT NULL,
	LEAD_TIME_DAYS INTEGER NOT NULL,
	PRODUCT_NAME VARCHAR2(100) NOT NULL,
	UNIT_OF_MEASURE VARCHAR2(50) NOT NULL,
	BASE_PRICE DECIMAL(10,2) NOT NULL,
	QUANTITY_AVAILABLE INTEGER DEFAULT 0 NOT NULL,
	CREATED_AT TIMESTAMP DEFAULT SYSDATE,
	UPDATED_AT TIMESTAMP DEFAULT SYSDATE,
	FK_DOCUMENT_ID NUMBER,
	FOREIGN KEY(FK_CONTACT_ID) REFERENCES CONTACT(CONTACT_ID),
	FOREIGN KEY(FK_DOCUMENT_ID) REFERENCES DOCUMENT(DOCUMENT_ID),
	FOREIGN KEY(AUTHORIZATION_ID) REFERENCES AUTHORIZATION(AUTHORIZATION_ID),
	CONSTRAINT SUPPLIER_UNIQUE_SKU UNIQUE(FK_CONTACT_ID, SKU)
);

CREATE TABLE EXHIBIT (
	EXHIBIT_ID NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
	FK_INVENTORY_ID NUMBER NOT NULL,
	FK_PLAN_ID NUMBER,
	FOREIGN KEY(FK_PLAN_ID) REFERENCES PLAN(PLAN_ID),
	FOREIGN KEY(FK_INVENTORY_ID) REFERENCES INVENTORY(INVENTORY_ID)
);

CREATE TABLE ORDER_ITEMS ( -- 'product line items customers placed in their carts.'
    ORDER_ITEM_ID NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    FK_PROFILE_ID NUMBER NOT NULL,
	TRANSACTION_ID VARCHAR2(32) NULL,
	CREATED_AT TIMESTAMP NOT NULL,
	QUANTITY NUMBER DEFAULT(1) NOT NULL,
	UNIT_PRICE DECIMAL(10,2) NOT NULL,
	LINE_TOTAL DECIMAL(10,2) NOT NULL,
    FK_INVENTORY_ID NUMBER NOT NULL,
    FOREIGN KEY(FK_PROFILE_ID) REFERENCES PROFILE(PROFILE_ID),
    FOREIGN KEY(FK_INVENTORY_ID) REFERENCES INVENTORY(INVENTORY_ID)
);

CREATE TABLE TASK_HISTORY(
	TASK_ID NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
	ASSIGNED_TO_PROFILE_ID NUMBER NOT NULL,
	CREATED_BY_PROFILE_ID  NUMBER NOT NULL,
	RELATED_ENTITY_TYPE VARCHAR2(32), -- 'order', 'supplier', etc.
	RELATED_ENTITY_ID NUMBER,
	TASK_TYPE VARCHAR2(32) NOT NULL,
	TASK_TITLE VARCHAR2(64) NOT NULL,
	DESCRIPTION VARCHAR2(128),
	FK_DOCUMENT_ID NUMBER,
	STATUS VARCHAR2(16) DEFAULT 'pending',
	DUE_DATE TIMESTAMP,
	COMPLETED_AT TIMESTAMP,
	CREATED_AT TIMESTAMP DEFAULT SYSDATE,
	UPDATED_AT TIMESTAMP DEFAULT SYSDATE,
	FOREIGN KEY(FK_DOCUMENT_ID) REFERENCES DOCUMENT(DOCUMENT_ID),
	CONSTRAINT chk_status CHECK (STATUS IN ('pending', 'in_progress', 'completed', 'cancelled'))
);

/* VIEW of available exhibit inventory */
CREATE OR REPLACE VIEW VIEW_AVAILABLE_INVENTORY AS
    SELECT
    INVENTORY_ID,FK_CONTACT_ID,AUTHORIZATION_ID,SKU,PRODUCT_DESCRIPTION,UNIT_COST,LEAD_TIME_DAYS,
    PRODUCT_NAME,UNIT_OF_MEASURE,BASE_PRICE,QUANTITY_AVAILABLE,CREATED_AT,UPDATED_AT,FK_DOCUMENT_ID
	FROM
	INVENTORY WHERE QUANTITY_AVAILABLE > 0
INTERSECT
    SELECT
    FK_INVENTORY_ID, NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
    FROM
    EXHIBIT;

CREATE OR REPLACE VIEW VIEW_SYSTEMROLE AS
	SELECT u.FK_PROFILE_ID, p.FK_CONTACT_ID,  r.ROLENAME, p.ACTIVE  FROM
	USERROLE u
	JOIN PROFILE p ON p.PROFILE_ID=u.FK_PROFILE_ID
	JOIN ROLE r ON r.ROLE_ID = u.FK_ROLE_ID AND r.ROLE_ID = 100;

CREATE OR REPLACE VIEW VIEW_STAFF AS
	SELECT u.FK_PROFILE_ID, p.FK_CONTACT_ID,  r.ROLENAME, p.ACTIVE, c.FIRST_NAME, c.LAST_NAME, s.POSITION FROM
	USERROLE u
	JOIN PROFILE p ON p.PROFILE_ID=u.FK_PROFILE_ID
	JOIN CONTACT c ON c.CONTACT_ID = p.FK_CONTACT_ID
	JOIN ROLE r ON r.ROLE_ID = u.FK_ROLE_ID AND r.ROLE_ID = 300
    JOIN STAFF s on s.FK_PROFILE_ID = p.PROFILE_ID;

CREATE OR REPLACE VIEW VIEW_SUPPLIER AS
	SELECT u.FK_PROFILE_ID, p.FK_CONTACT_ID,  r.ROLENAME, p.ACTIVE, c.FIRST_NAME, c.LAST_NAME FROM
	USERROLE u
	JOIN PROFILE p ON p.PROFILE_ID=u.FK_PROFILE_ID
	JOIN CONTACT c ON c.CONTACT_ID = p.FK_CONTACT_ID
	JOIN ROLE r ON r.ROLE_ID = u.FK_ROLE_ID AND r.ROLE_ID = 200;

CREATE OR REPLACE VIEW VIEW_CUSTOMER AS
	SELECT u.FK_PROFILE_ID, p.FK_CONTACT_ID,  r.ROLENAME, p.ACTIVE, c.FIRST_NAME, c.LAST_NAME FROM
	USERROLE u
	JOIN PROFILE p ON p.PROFILE_ID=u.FK_PROFILE_ID
	JOIN CONTACT c ON c.CONTACT_ID = p.FK_CONTACT_ID
	JOIN ROLE r ON r.ROLE_ID = u.FK_ROLE_ID AND r.ROLE_ID = 400;

-- Trigger customer ordered items reservation
CREATE OR REPLACE TRIGGER ORDER_ITEMS_reserved
    AFTER INSERT ON ORDER_ITEMS FOR EACH ROW
    BEGIN
        UPDATE INVENTORY SET QUANTITY_AVAILABLE = QUANTITY_AVAILABLE - :NEW.QUANTITY WHERE INVENTORY_ID= :NEW.FK_INVENTORY_ID;
        DBMS_OUTPUT.put_line(' ORDER transaction. ' );
    END;
/

-- Trigger Paid Invoice, generates subscription
CREATE OR REPLACE TRIGGER INVOICE_paid_subscription
    AFTER INSERT OR UPDATE ON INVOICE FOR EACH ROW
    BEGIN
        DBMS_OUTPUT.put_line(' SUBSCRIPTION transaction. ' );
        INSERT INTO SUBSCRIPTION (FK_PLAN_ID, FK_CONTACT_ID) VALUES (:NEW.FK_PLAN_ID, :NEW.FK_CONTACT_ID);
		EXCEPTION
    	WHEN DUP_VAL_ON_INDEX THEN
	  		DBMS_OUTPUT.put_line('Subscription exists for plan and contact. No new subscription created.');
			UPDATE SUBSCRIPTION SET FK_PLAN_ID = :NEW.FK_PLAN_ID, FK_CONTACT_ID = :NEW.FK_CONTACT_ID, MODIFIED_BY = 1, MODIFIED_DT = SYSDATE
			WHERE FK_PLAN_ID = :NEW.FK_PLAN_ID AND FK_CONTACT_ID = :NEW.FK_CONTACT_ID;
		WHEN OTHERS THEN
	  		DBMS_OUTPUT.put_line('Error in INVOICE_paid_subscription creating/updating subscription: ' || SQLERRM);
    END;
/

CREATE OR REPLACE PROCEDURE sp_auth_supplier(
    p_status IN VARCHAR,
    p_supplier IN NUMBER,
    p_document_url IN VARCHAR
) IS
    l_id NUMBER;
BEGIN
    BEGIN
      INSERT INTO DOCUMENT(DOCUMENT_ID, DOCUMENT_URL) VALUES (ISEQ_DOCUMENT.nextval, p_document_url)
      RETURNING DOCUMENT_ID INTO l_id; -- Captures the generated ID
    END;
    BEGIN
      INSERT INTO AUTHORIZATION (AUTHORIZATION_STATUS, FK_CONTACT_ID, MODIFIED_BY, FK_DOCUMENT_ID) VALUES(p_status, p_supplier, 1, l_id);
      DBMS_OUTPUT.put_line('Reviewed Document ' || p_status );
    EXCEPTION
      WHEN OTHERS
      THEN
         DBMS_OUTPUT.put_line (SQLERRM);
         raise_application_error (-20500, 'failed while setting auth_supplier_status. ');
    END;
END;

/

INSERT INTO ROLE (ROLE_ID, ROLENAME) VALUES (100, 'SYSTEM');
INSERT INTO ROLE (ROLE_ID, ROLENAME) VALUES (200, 'SUPPLIER');
INSERT INTO ROLE (ROLE_ID, ROLENAME) VALUES (300, 'STAFF');
INSERT INTO ROLE (ROLE_ID, ROLENAME) VALUES (400, 'CUSTOMER');

-- SYSTEM ROLE
INSERT INTO CONTACT ( FIRST_NAME, LAST_NAME, PHONE_NO, EMAIL, MODIFIED_BY) VALUES ('ART', 'SYSTEM', '000-1234', 'arts@art.com', 1);
-- STAFF ROLE
INSERT INTO CONTACT ( FIRST_NAME, LAST_NAME, PHONE_NO, EMAIL, MODIFIED_BY) VALUES ('Alice', 'Johnson', '650-555-9012', 'alice.johnson@art.com', 1);
INSERT INTO CONTACT ( FIRST_NAME, LAST_NAME, PHONE_NO, EMAIL, MODIFIED_BY) VALUES ( 'Karen',  'Park',     '206-555-0110', 'karen.park@art.com',    1);
INSERT INTO CONTACT ( FIRST_NAME, LAST_NAME, PHONE_NO, EMAIL, MODIFIED_BY) VALUES ( 'Jeese',  'Taylor',   '713-555-0106', 'grace.taylor@art.com',  1);
INSERT INTO CONTACT ( FIRST_NAME, LAST_NAME, PHONE_NO, EMAIL, MODIFIED_BY) VALUES ( 'James',  'Lee',      '206-555-0109', 'james.lee@art.com',     1);
-- SUPPLIER ROLE
INSERT INTO CONTACT ( FIRST_NAME, LAST_NAME, PHONE_NO, EMAIL, MODIFIED_BY) VALUES ('Stephen', 'Alec', '424-546-9312', 'salec@eizel.com', 1);
INSERT INTO CONTACT ( FIRST_NAME, LAST_NAME, PHONE_NO, EMAIL, MODIFIED_BY) VALUES ('Henry',  'Martinez', '602-555-0107', 'henry@artsupply.com',   1);
-- CUSTOMER ROLE
INSERT INTO CONTACT ( FIRST_NAME, LAST_NAME, PHONE_NO, EMAIL, MODIFIED_BY) VALUES ('Carol',  'White',    '212-555-0102', 'carol.white@mail.com',  1);
INSERT INTO CONTACT ( FIRST_NAME, LAST_NAME, PHONE_NO, EMAIL, MODIFIED_BY) VALUES ('Bob',    'Smith',    '212-555-0101', 'bob.smith@mail.com',    1);
INSERT INTO CONTACT ( FIRST_NAME, LAST_NAME, PHONE_NO, EMAIL, MODIFIED_BY) VALUES ('David',  'Brown',    '312-555-0103', 'david.brown@mail.com',  1);
INSERT INTO CONTACT ( FIRST_NAME, LAST_NAME, PHONE_NO, EMAIL, MODIFIED_BY) VALUES ('Emma',   'Davis',    '312-555-0104', 'emma.davis@mail.com',   1);
INSERT INTO CONTACT ( FIRST_NAME, LAST_NAME, PHONE_NO, EMAIL, MODIFIED_BY) VALUES ('Frank',  'Wilson',   '713-555-0105', 'frank.wilson@mail.com', 1);

INSERT INTO ZIP (ZIP, CITY, STATE) VALUES ('10001', 'New York',      'NY');
INSERT INTO ZIP (ZIP, CITY, STATE) VALUES ('90210', 'Beverly Hills', 'CA');
INSERT INTO ZIP (ZIP, CITY, STATE) VALUES ('94027', 'Atherton',      'CA');
INSERT INTO ZIP (ZIP, CITY, STATE) VALUES ('90211', 'Los Angeles',   'CA');
INSERT INTO ZIP (ZIP, CITY, STATE) VALUES ('60601', 'Chicago',       'IL');
INSERT INTO ZIP (ZIP, CITY, STATE) VALUES ('77001', 'Houston',       'TX');
INSERT INTO ZIP (ZIP, CITY, STATE) VALUES ('85001', 'Phoenix',       'AZ');
INSERT INTO ZIP (ZIP, CITY, STATE) VALUES ('98101', 'Seattle',       'WA');
INSERT INTO ZIP (ZIP, CITY, STATE) VALUES ('02101', 'Boston',        'MA');
INSERT INTO ZIP (ZIP, CITY, STATE) VALUES ('33101', 'Miami',         'FL');

INSERT INTO ADDRESS (FK_CONTACT_ID, STREET, ZIP) VALUES (1, '789 Oak St, suite 10', '94027');
INSERT INTO ADDRESS (FK_CONTACT_ID, STREET, ZIP) VALUES (2, '789 Oak St, suite 15', '94027');
INSERT INTO ADDRESS (FK_CONTACT_ID, STREET, ZIP) VALUES (3, '789 Oak St, suite 1', '94027');
INSERT INTO ADDRESS (FK_CONTACT_ID, STREET, ZIP) VALUES (4, '789 Oak St, suite 5', '94027');
INSERT INTO ADDRESS (FK_CONTACT_ID, STREET, ZIP) VALUES (5, '789 Oak St, suite 15', '94027');
INSERT INTO ADDRESS (FK_CONTACT_ID, STREET, ZIP) VALUES (6, '890 Venue Rd', '90210');
INSERT INTO ADDRESS (FK_CONTACT_ID, STREET, ZIP) VALUES (7,  '200 Sunset Blvd',  '90210');
INSERT INTO ADDRESS (FK_CONTACT_ID, STREET, ZIP) VALUES (8,  '100 Broadway',     '10001');
INSERT INTO ADDRESS (FK_CONTACT_ID, STREET, ZIP) VALUES (9,  '300 Michigan Ave',  '60601');
INSERT INTO ADDRESS (FK_CONTACT_ID, STREET, ZIP) VALUES (10,  '400 Main St',       '77001');
INSERT INTO ADDRESS (FK_CONTACT_ID, STREET, ZIP ) VALUES (11,  '500 Central Ave',   '85001');
INSERT INTO ADDRESS (FK_CONTACT_ID, STREET, ZIP ) VALUES (12,  '600 Pike St',       '98101');

-- SYSTEMPROFILE
INSERT INTO PROFILE (FK_CONTACT_ID, ACTIVE, MODIFIED_BY) VALUES (1, TRUE, 1);
-- STAFF PROFILE
INSERT INTO PROFILE (FK_CONTACT_ID, ACTIVE, MODIFIED_BY) VALUES (2, TRUE, 2);
-- SUPPLIER
INSERT INTO PROFILE (FK_CONTACT_ID, ACTIVE, MODIFIED_BY) VALUES (3, TRUE, 3);

INSERT INTO PROFILE (PROFILE_ID, FK_CONTACT_ID, ACTIVE, MODIFIED_BY) VALUES (4,  4,  TRUE,  1);
INSERT INTO PROFILE (PROFILE_ID, FK_CONTACT_ID, ACTIVE, MODIFIED_BY) VALUES (5,  5,  TRUE,  1);
INSERT INTO PROFILE (PROFILE_ID, FK_CONTACT_ID, ACTIVE, MODIFIED_BY) VALUES (6,  6,  TRUE,  1);
INSERT INTO PROFILE (PROFILE_ID, FK_CONTACT_ID, ACTIVE, MODIFIED_BY) VALUES (7,  7,  TRUE,  1);
INSERT INTO PROFILE (PROFILE_ID, FK_CONTACT_ID, ACTIVE, MODIFIED_BY) VALUES (8,  8,  FALSE, 1);  -- inactive
INSERT INTO PROFILE (PROFILE_ID, FK_CONTACT_ID, ACTIVE, MODIFIED_BY) VALUES (9,  9,  TRUE,  1);
INSERT INTO PROFILE (PROFILE_ID, FK_CONTACT_ID, ACTIVE, MODIFIED_BY) VALUES (10, 10, TRUE,  1);
INSERT INTO PROFILE (PROFILE_ID, FK_CONTACT_ID, ACTIVE, MODIFIED_BY) VALUES (11, 11, TRUE,  1);
INSERT INTO PROFILE (PROFILE_ID, FK_CONTACT_ID, ACTIVE, MODIFIED_BY) VALUES (12, 12, TRUE,  1);


INSERT INTO USERROLE (FK_PROFILE_ID, FK_ROLE_ID, PASSWORDHASH) VALUES (1, 100, NULL);
INSERT INTO USERROLE (FK_PROFILE_ID, FK_ROLE_ID) VALUES (2, 300);
INSERT INTO USERROLE (FK_PROFILE_ID, FK_ROLE_ID) VALUES (3, 300);
INSERT INTO USERROLE (FK_PROFILE_ID, FK_ROLE_ID) VALUES (4,  300);
INSERT INTO USERROLE (FK_PROFILE_ID, FK_ROLE_ID) VALUES (5,  300);
INSERT INTO USERROLE (FK_PROFILE_ID, FK_ROLE_ID) VALUES (6,  200);
INSERT INTO USERROLE (FK_PROFILE_ID, FK_ROLE_ID) VALUES (7,  200);
INSERT INTO USERROLE (FK_PROFILE_ID, FK_ROLE_ID) VALUES (8,  400);  -- inactive customer
INSERT INTO USERROLE (FK_PROFILE_ID, FK_ROLE_ID) VALUES (9,  400);
INSERT INTO USERROLE (FK_PROFILE_ID, FK_ROLE_ID) VALUES (10, 400);
INSERT INTO USERROLE (FK_PROFILE_ID, FK_ROLE_ID) VALUES (11, 400);
INSERT INTO USERROLE (FK_PROFILE_ID, FK_ROLE_ID) VALUES (12, 400);

INSERT INTO STAFF (FK_PROFILE_ID, POSITION) VALUES (2,  'Director');
INSERT INTO STAFF (FK_PROFILE_ID, POSITION) VALUES (3, 'Registrar');
INSERT INTO STAFF (FK_PROFILE_ID, POSITION) VALUES (4,  'Curator');
INSERT INTO STAFF (FK_PROFILE_ID, POSITION) VALUES (5, 'Art Handler');

INSERT INTO DOCUMENT (DOCUMENT_ID, DOCUMENT_URL) VALUES (2,  'https://art.com/docs/consignment-stephen-crimson-horizon.pdf');
INSERT INTO DOCUMENT (DOCUMENT_ID, DOCUMENT_URL) VALUES (3,  'https://art.com/docs/consignment-stephen-misty-valley.pdf');
INSERT INTO DOCUMENT (DOCUMENT_ID, DOCUMENT_URL) VALUES (4,  'https://art.com/docs/consignment-stephen-twisted-form.pdf');
INSERT INTO DOCUMENT (DOCUMENT_ID, DOCUMENT_URL) VALUES (5,  'https://art.com/docs/consignment-stephen-blue-symmetry.pdf');
INSERT INTO DOCUMENT (DOCUMENT_ID, DOCUMENT_URL) VALUES (6,  'https://art.com/docs/consignment-henry-urban-solitude.pdf');
INSERT INTO DOCUMENT (DOCUMENT_ID, DOCUMENT_URL) VALUES (7,  'https://art.com/docs/consignment-henry-earth-vessel.pdf');
INSERT INTO DOCUMENT (DOCUMENT_ID, DOCUMENT_URL) VALUES (8,  'https://art.com/docs/consignment-henry-quiet-reflection.pdf');

-- Stephen — approved
INSERT INTO AUTHORIZATION (AUTHORIZATION_STATUS, FK_CONTACT_ID, FK_DOCUMENT_ID, MODIFIED_BY, AUTHORIZATION_DATE, EXPIRATION_DATE)
VALUES ('approved', 6, 2, 1, SYSDATE, SYSDATE + 365);
-- Stephen — approved
INSERT INTO AUTHORIZATION (AUTHORIZATION_STATUS, FK_CONTACT_ID, FK_DOCUMENT_ID, MODIFIED_BY, AUTHORIZATION_DATE, EXPIRATION_DATE)
VALUES ('approved', 6, 3, 1, SYSDATE, SYSDATE + 365);
-- Stephen — pending ('pending' status path)
INSERT INTO AUTHORIZATION (AUTHORIZATION_STATUS, FK_CONTACT_ID, FK_DOCUMENT_ID, MODIFIED_BY, AUTHORIZATION_DATE, EXPIRATION_DATE)
VALUES ('pending',  6, 4, 1, SYSDATE, SYSDATE + 365);
-- Stephen — rejected ('rejected' status path)
INSERT INTO AUTHORIZATION (AUTHORIZATION_STATUS, FK_CONTACT_ID, FK_DOCUMENT_ID, MODIFIED_BY, AUTHORIZATION_DATE, EXPIRATION_DATE)
VALUES ('rejected', 6, 5, 1, SYSDATE, SYSDATE + 30);
-- Henry — approved
INSERT INTO AUTHORIZATION (AUTHORIZATION_STATUS, FK_CONTACT_ID, FK_DOCUMENT_ID, MODIFIED_BY, AUTHORIZATION_DATE, EXPIRATION_DATE)
VALUES ('approved', 7, 6, 1, SYSDATE, SYSDATE + 365);
-- Henry — approved
INSERT INTO AUTHORIZATION (AUTHORIZATION_STATUS, FK_CONTACT_ID, FK_DOCUMENT_ID, MODIFIED_BY, AUTHORIZATION_DATE, EXPIRATION_DATE)
VALUES ('approved', 7, 7, 1, SYSDATE, SYSDATE + 365);
-- Henry — approved
INSERT INTO AUTHORIZATION (AUTHORIZATION_STATUS, FK_CONTACT_ID, FK_DOCUMENT_ID, MODIFIED_BY, AUTHORIZATION_DATE, EXPIRATION_DATE)
VALUES ('approved', 7, 8, 1, SYSDATE, SYSDATE + 365);
-- Henry — pending
INSERT INTO AUTHORIZATION (AUTHORIZATION_STATUS, FK_CONTACT_ID, FK_DOCUMENT_ID, MODIFIED_BY, AUTHORIZATION_DATE, EXPIRATION_DATE)
VALUES ('pending',  7, 7, 1, SYSDATE, SYSDATE + 180);
-- Henry — expired (EXPIRATION_DATE in the past)
INSERT INTO AUTHORIZATION (AUTHORIZATION_STATUS, FK_CONTACT_ID, FK_DOCUMENT_ID, MODIFIED_BY, AUTHORIZATION_DATE, EXPIRATION_DATE)
VALUES ('expired',  7, 8, 1, SYSDATE - 365, SYSDATE - 30);

-- PLAN Documents for exhibition
INSERT INTO DOCUMENT (DOCUMENT_ID, DOCUMENT_URL) VALUES (9,  'https://art.com/docs/plan-spring-gala-2025.pdf');
INSERT INTO DOCUMENT (DOCUMENT_ID, DOCUMENT_URL) VALUES (10, 'https://art.com/docs/plan-summer-exhibit-2025.pdf');
INSERT INTO DOCUMENT (DOCUMENT_ID, DOCUMENT_URL) VALUES (11, 'https://art.com/docs/plan-fall-exhibition-2025.pdf');

-- PLANS
INSERT INTO PLAN (DESCRIPTION, FK_DOCUMENT_ID, DURATION, PRICE,  MODIFIED_BY) VALUES ('Spring Gala Preview',      9,   4,   75.00,  1);   -- PLAN_ID 1
INSERT INTO PLAN (DESCRIPTION, FK_DOCUMENT_ID, DURATION, PRICE,  MODIFIED_BY) VALUES ('Spring Gala Standard',     9,   8,  120.00,  1);   -- PLAN_ID 2
INSERT INTO PLAN (DESCRIPTION, FK_DOCUMENT_ID, DURATION, PRICE,  MODIFIED_BY) VALUES ('Spring Gala VIP',          9,  12,  250.00,  1);   -- PLAN_ID 3
INSERT INTO PLAN (DESCRIPTION, FK_DOCUMENT_ID, DURATION, PRICE,  MODIFIED_BY) VALUES ('Summer Exhibit Basic',    10,   4,   50.00,  1);   -- PLAN_ID 4
INSERT INTO PLAN (DESCRIPTION, FK_DOCUMENT_ID, DURATION, PRICE,  MODIFIED_BY) VALUES ('Summer Exhibit Plus',     10,   8,  100.00,  1);   -- PLAN_ID 5
INSERT INTO PLAN (DESCRIPTION, FK_DOCUMENT_ID, DURATION, PRICE,  MODIFIED_BY) VALUES ('Summer Exhibit Premium',  10,  16,  200.00,  1);   -- PLAN_ID 6
INSERT INTO PLAN (DESCRIPTION, FK_DOCUMENT_ID, DURATION, PRICE,  MODIFIED_BY) VALUES ('Fall Exhibition Access',  11,   4,   60.00,  1);   -- PLAN_ID 7
INSERT INTO PLAN (DESCRIPTION, FK_DOCUMENT_ID, DURATION, PRICE,  MODIFIED_BY) VALUES ('Fall Exhibition Full Day',11,   8,  110.00,  1);   -- PLAN_ID 8
INSERT INTO PLAN (DESCRIPTION, FK_DOCUMENT_ID, DURATION, PRICE,  MODIFIED_BY) VALUES ('Fall Exhibition Weekend', 11,  24,  180.00,  1);   -- PLAN_ID 9
INSERT INTO PLAN (DESCRIPTION, FK_DOCUMENT_ID, DURATION, PRICE,  MODIFIED_BY) VALUES ('Annual Patron Pass',      11, 200,  500.00,  1);   -- PLAN_ID 10

-- IV 1 — Crimson Horizon
INSERT INTO INVENTORY (FK_CONTACT_ID, AUTHORIZATION_ID, SKU, PRODUCT_DESCRIPTION, UNIT_COST, LEAD_TIME_DAYS,
    PRODUCT_NAME,     UNIT_OF_MEASURE, BASE_PRICE, QUANTITY_AVAILABLE, FK_DOCUMENT_ID)
VALUES (6, 1, 'ART-S-001',
    'Oil on canvas, abstract expressionism, 24x36',   800.00, 7,
    'Crimson Horizon', 'piece',        1200.00,  5,  2);

-- IV 2 — Misty Valley
INSERT INTO INVENTORY (FK_CONTACT_ID, AUTHORIZATION_ID, SKU,
    PRODUCT_DESCRIPTION,                            UNIT_COST, LEAD_TIME_DAYS,
    PRODUCT_NAME,     UNIT_OF_MEASURE, BASE_PRICE, QUANTITY_AVAILABLE, FK_DOCUMENT_ID)
VALUES (6, 2, 'ART-S-002',
    'Watercolor on paper, landscape, 18x24',          450.00, 5,
    'Misty Valley',    'piece',         700.00,  5,  3);

-- IV 3 — Twisted Form
INSERT INTO INVENTORY (FK_CONTACT_ID, AUTHORIZATION_ID, SKU,
    PRODUCT_DESCRIPTION,                            UNIT_COST, LEAD_TIME_DAYS,
    PRODUCT_NAME,     UNIT_OF_MEASURE, BASE_PRICE, QUANTITY_AVAILABLE, FK_DOCUMENT_ID)
VALUES (6, 3, 'ART-S-003',
    'Bronze sculpture, abstract, 12 in height',      1500.00, 14,
    'Twisted Form',    'piece',        2200.00,  5,  4);

-- IV 4 — Blue Symmetry
INSERT INTO INVENTORY (FK_CONTACT_ID, AUTHORIZATION_ID, SKU,
    PRODUCT_DESCRIPTION,                            UNIT_COST, LEAD_TIME_DAYS,
    PRODUCT_NAME,     UNIT_OF_MEASURE, BASE_PRICE, QUANTITY_AVAILABLE, FK_DOCUMENT_ID)
VALUES (7, 1, 'ART-S-004',
    'Acrylic on canvas, geometric, 30x30',            600.00, 7,
    'Blue Symmetry',   'piece',         950.00,  5,  2);

-- IV 5 — Urban Collage
INSERT INTO INVENTORY (FK_CONTACT_ID, AUTHORIZATION_ID, SKU,
    PRODUCT_DESCRIPTION,                            UNIT_COST, LEAD_TIME_DAYS,
    PRODUCT_NAME,     UNIT_OF_MEASURE, BASE_PRICE, QUANTITY_AVAILABLE, FK_DOCUMENT_ID)
VALUES (7, 2, 'ART-S-005',
    'Mixed media on board, 20x20',                    350.00, 5,
    'Urban Collage',   'piece',         550.00,  6,  3);

-- IV 6 — Urban Solitude
INSERT INTO INVENTORY (FK_CONTACT_ID, AUTHORIZATION_ID, SKU,
    PRODUCT_DESCRIPTION,                            UNIT_COST, LEAD_TIME_DAYS,
    PRODUCT_NAME,     UNIT_OF_MEASURE, BASE_PRICE, QUANTITY_AVAILABLE, FK_DOCUMENT_ID)
VALUES (6, 6, 'ART-H-001',
    'Photography print, archival, limited ed 1/10',   900.00, 3,
    'Urban Solitude',  'piece',        1500.00, 10,  6);

-- IV 7 — Earth Vessel
INSERT INTO INVENTORY (FK_CONTACT_ID, AUTHORIZATION_ID, SKU,
    PRODUCT_DESCRIPTION,                            UNIT_COST, LEAD_TIME_DAYS,
    PRODUCT_NAME,     UNIT_OF_MEASURE, BASE_PRICE, QUANTITY_AVAILABLE, FK_DOCUMENT_ID)
VALUES (7, 7, 'ART-H-002',
    'Ceramic vessel, hand-thrown and glazed',          250.00, 10,
    'Earth Vessel',    'piece',         420.00,  8,  7);

-- IV 8 — Quiet Reflection
INSERT INTO INVENTORY (FK_CONTACT_ID, AUTHORIZATION_ID, SKU,
    PRODUCT_DESCRIPTION,                            UNIT_COST, LEAD_TIME_DAYS,
    PRODUCT_NAME,     UNIT_OF_MEASURE, BASE_PRICE, QUANTITY_AVAILABLE, FK_DOCUMENT_ID)
VALUES (7, 8, 'ART-H-003',
    'Pastel drawing, portrait, 16x20',                300.00, 7,
    'Quiet Reflection','piece',         500.00,  4,  8);

-- IV 9 — Neon Dreams
INSERT INTO INVENTORY (FK_CONTACT_ID, AUTHORIZATION_ID, SKU,
    PRODUCT_DESCRIPTION,                            UNIT_COST, LEAD_TIME_DAYS,
    PRODUCT_NAME,     UNIT_OF_MEASURE, BASE_PRICE, QUANTITY_AVAILABLE, FK_DOCUMENT_ID)
VALUES (6, 6, 'ART-H-004',
    'Digital print on aluminum, 24x36',               700.00, 5,
    'Neon Dreams',     'piece',        1100.00,  6,  6);

-- IV 10 — Woven Stories
INSERT INTO INVENTORY (FK_CONTACT_ID, AUTHORIZATION_ID, SKU,
    PRODUCT_DESCRIPTION,                            UNIT_COST, LEAD_TIME_DAYS,
    PRODUCT_NAME,     UNIT_OF_MEASURE, BASE_PRICE, QUANTITY_AVAILABLE, FK_DOCUMENT_ID)
VALUES (6, 7, 'ART-H-005',
    'Textile wall hanging, woven, 36x48',             550.00, 14,
    'Woven Stories',   'piece',         850.00,  5,  7);

INSERT INTO EXHIBIT (FK_INVENTORY_ID, FK_PLAN_ID) VALUES (1,  1);  -- Crimson Horizon   → Spring Preview
INSERT INTO EXHIBIT (FK_INVENTORY_ID, FK_PLAN_ID) VALUES (2,  2);  -- Misty Valley      → Spring Standard
INSERT INTO EXHIBIT (FK_INVENTORY_ID, FK_PLAN_ID) VALUES (3,  3);  -- Twisted Form      → Spring VIP
INSERT INTO EXHIBIT (FK_INVENTORY_ID, FK_PLAN_ID) VALUES (4,  4);  -- Blue Symmetry     → Summer Basic
INSERT INTO EXHIBIT (FK_INVENTORY_ID, FK_PLAN_ID) VALUES (5,  5);  -- Urban Collage     → Summer Plus
INSERT INTO EXHIBIT (FK_INVENTORY_ID, FK_PLAN_ID) VALUES (6,  6);  -- Urban Solitude    → Summer Premium
INSERT INTO EXHIBIT (FK_INVENTORY_ID, FK_PLAN_ID) VALUES (7,  7);  -- Earth Vessel      → Fall Access
INSERT INTO EXHIBIT (FK_INVENTORY_ID, FK_PLAN_ID) VALUES (8,  8);  -- Quiet Reflection  → Fall Full Day
INSERT INTO EXHIBIT (FK_INVENTORY_ID, FK_PLAN_ID) VALUES (9,  9);  -- Neon Dreams       → Fall Weekend
INSERT INTO EXHIBIT (FK_INVENTORY_ID, FK_PLAN_ID) VALUES (10, 10); -- Woven Stories     → Annual Pass


INSERT INTO INVOICE (FK_CONTACT_ID, FK_PLAN_ID, InvoiceDate, AmountPaid) VALUES (9, 1, DATE'2025-01-01', 75.00);   -- Bob — Spring Preview
INSERT INTO INVOICE (FK_CONTACT_ID, FK_PLAN_ID, InvoiceDate, AmountPaid) VALUES (10, 2, DATE'2025-01-02', 120.00); -- Carol — Spring Standard
INSERT INTO INVOICE (FK_CONTACT_ID, FK_PLAN_ID, InvoiceDate, AmountPaid) VALUES (11, 3, DATE'2025-01-03', 250.00); -- David — Spring VIP
INSERT INTO INVOICE (FK_CONTACT_ID, FK_PLAN_ID, InvoiceDate, AmountPaid) VALUES (12, 4, DATE'2025-01-04', 50.00);  -- Emma — Summer Basic
INSERT INTO INVOICE (FK_CONTACT_ID, FK_PLAN_ID, InvoiceDate, AmountPaid) VALUES (9, 5, DATE'2025-01-05', 100.00);  -- Bob — Summer Plus
INSERT INTO INVOICE (FK_CONTACT_ID, FK_PLAN_ID, InvoiceDate, AmountPaid) VALUES (10, 6, DATE'2025-01-06', 150.00); -- Carol — Summer Premium
INSERT INTO INVOICE (FK_CONTACT_ID, FK_PLAN_ID, InvoiceDate, AmountPaid) VALUES (11, 7, DATE'2025-01-07', 60.00);  -- David — Fall Access
INSERT INTO INVOICE (FK_CONTACT_ID, FK_PLAN_ID, InvoiceDate, AmountPaid) VALUES (12, 8, DATE'2025-01-08', 110.00); -- Emma — Fall Full Day
INSERT INTO INVOICE (FK_CONTACT_ID, FK_PLAN_ID, InvoiceDate, AmountPaid) VALUES (9, 9, DATE'2025-01-09', 180.00);  -- Bob — Fall Weekend
INSERT INTO INVOICE (FK_CONTACT_ID, FK_PLAN_ID, InvoiceDate, AmountPaid) VALUES (10, 10, DATE'2025-01-10', 500.00); -- Carol — Annual Pass

/
