-- ============================================================
-- 31_dds_standalone_demo_setup.sql
-- Independent local Deep Data Security demo.
--
-- This script deliberately does not use VPD views, application
-- permission tables, or external database links. It creates a
-- local PG/MY-shaped matrix so the DDS demo can run independently
-- when the VPD demo's RDS links are unavailable.
-- ============================================================
WHENEVER SQLERROR EXIT SQL.SQLCODE
SET ECHO OFF
SET FEEDBACK ON
SET DEFINE ON
SET VERIFY OFF

PROMPT === 1. Creating DDS-only local source tables ===
BEGIN
  EXECUTE IMMEDIATE 'DROP VIEW v_dds_customers_pg';
EXCEPTION WHEN OTHERS THEN NULL;
END;
/
BEGIN
  EXECUTE IMMEDIATE 'DROP VIEW v_dds_customers_my';
EXCEPTION WHEN OTHERS THEN NULL;
END;
/
BEGIN
  EXECUTE IMMEDIATE 'DROP TABLE cb_dds_demo_customers_pg PURGE';
EXCEPTION WHEN OTHERS THEN NULL;
END;
/
BEGIN
  EXECUTE IMMEDIATE 'DROP TABLE cb_dds_demo_customers_my PURGE';
EXCEPTION WHEN OTHERS THEN NULL;
END;
/

CREATE TABLE cb_dds_demo_customers_pg (
  customer_id NUMBER PRIMARY KEY,
  full_name   VARCHAR2(100) NOT NULL,
  email       VARCHAR2(200),
  signup_date DATE DEFAULT SYSDATE NOT NULL,
  region      VARCHAR2(30) NOT NULL
);

CREATE TABLE cb_dds_demo_customers_my (
  customer_id NUMBER PRIMARY KEY,
  full_name   VARCHAR2(100) NOT NULL,
  email       VARCHAR2(200),
  signup_date DATE DEFAULT SYSDATE NOT NULL,
  region      VARCHAR2(30) NOT NULL
);

INSERT INTO cb_dds_demo_customers_pg VALUES (101, 'PG Alice', 'pg.alice@example.test', DATE '2026-01-10', 'APAC');
INSERT INTO cb_dds_demo_customers_pg VALUES (102, 'PG Bob',   'pg.bob@example.test',   DATE '2026-01-11', 'EMEA');
INSERT INTO cb_dds_demo_customers_pg VALUES (103, 'PG Carol', 'pg.carol@example.test', DATE '2026-01-12', 'AMER');

INSERT INTO cb_dds_demo_customers_my VALUES (201, 'MY Alice', 'my.alice@example.test', DATE '2026-02-10', 'APAC');
INSERT INTO cb_dds_demo_customers_my VALUES (202, 'MY Bob',   'my.bob@example.test',   DATE '2026-02-11', 'EMEA');
INSERT INTO cb_dds_demo_customers_my VALUES (203, 'MY Carol', 'my.carol@example.test', DATE '2026-02-12', 'AMER');

CREATE OR REPLACE VIEW v_dds_customers_pg AS
SELECT customer_id, full_name, email, signup_date, region
FROM cb_dds_demo_customers_pg;

CREATE OR REPLACE VIEW v_dds_customers_my AS
SELECT customer_id, full_name, email, signup_date, region
FROM cb_dds_demo_customers_my;

COMMIT;

PROMPT === 2. Creating independent DDS END USERs ===
CREATE END USER "dds_demo_my"   IDENTIFIED BY "&DDSUSER_MY_PASSWORD";
CREATE END USER "dds_demo_pg"   IDENTIFIED BY "&DDSUSER_PG_PASSWORD";
CREATE END USER "dds_demo_both" IDENTIFIED BY "&DDSUSER_BOTH_PASSWORD";
CREATE END USER "dds_demo_none" IDENTIFIED BY "&DDSUSER_NONE_PASSWORD";

PROMPT === 3. Creating independent DATA ROLEs ===
CREATE ROLE dds_demo_connect_role;
GRANT CREATE SESSION TO dds_demo_connect_role;

CREATE DATA ROLE dds_demo_my_role;
CREATE DATA ROLE dds_demo_pg_role;
CREATE DATA ROLE dds_demo_both_role;
CREATE DATA ROLE dds_demo_none_role;

GRANT dds_demo_connect_role TO dds_demo_my_role;
GRANT dds_demo_connect_role TO dds_demo_pg_role;
GRANT dds_demo_connect_role TO dds_demo_both_role;
GRANT dds_demo_connect_role TO dds_demo_none_role;

GRANT DATA ROLE dds_demo_my_role   TO "dds_demo_my";
GRANT DATA ROLE dds_demo_pg_role   TO "dds_demo_pg";
GRANT DATA ROLE dds_demo_both_role TO "dds_demo_both";
GRANT DATA ROLE dds_demo_none_role TO "dds_demo_none";

PROMPT === 4. Creating independent DATA GRANTs ===
CREATE DATA GRANT admin.dds_demo_my_grant
  AS SELECT
  ON admin.v_dds_customers_my
  TO dds_demo_my_role;

CREATE DATA GRANT admin.dds_demo_pg_grant
  AS SELECT
  ON admin.v_dds_customers_pg
  TO dds_demo_pg_role;

CREATE DATA GRANT admin.dds_demo_both_pg_grant
  AS SELECT
  ON admin.v_dds_customers_pg
  TO dds_demo_both_role;

CREATE DATA GRANT admin.dds_demo_both_my_grant
  AS SELECT
  ON admin.v_dds_customers_my
  TO dds_demo_both_role;

PROMPT === Independent DDS demo setup complete ===
EXIT;
