-- ====================================================================
-- MRDS_LOADER User Creation Script
-- ====================================================================
-- Purpose: Create MRDS_LOADER user for cloud operations and data loading
-- Based on: MARS-757 install_package_MarsDEVOreplicator_MARS757_create_user.sql
-- ====================================================================

-- Note: User creation typically done by ADMIN, this script assumes user exists
-- CREATE USER MRDS_LOADER IDENTIFIED BY password
-- DEFAULT TABLESPACE USERS TEMPORARY TABLESPACE TEMP;
ALTER USER MRDS_LOADER QUOTA UNLIMITED ON DATA;

-- Basic session and system privileges
GRANT CREATE SESSION TO MRDS_LOADER;
GRANT SELECT ANY DICTIONARY TO MRDS_LOADER;
GRANT SELECT_CATALOG_ROLE TO MRDS_LOADER;
GRANT READ ANY TABLE TO MRDS_LOADER;

-- Create and configure MRDS_LOADER_ROLE (inherited by other schemas)
CREATE ROLE MRDS_LOADER_ROLE;
GRANT MRDS_LOADER_ROLE TO MRDS_LOADER;
GRANT READ, WRITE ON DIRECTORY DATA_PUMP_DIR TO MRDS_LOADER_ROLE;
GRANT EXECUTE ON DBMS_CLOUD TO MRDS_LOADER_ROLE;

-- Job execution privileges
GRANT CREATE JOB TO MRDS_LOADER;
GRANT EXECUTE ON DBMS_PARALLEL_EXECUTE TO MRDS_LOADER;

-- Cloud operations privileges
EXEC DBMS_CLOUD_ADMIN.ENABLE_RESOURCE_PRINCIPAL(username => 'MRDS_LOADER');
GRANT EXECUTE ON C##CLOUD$SERVICE.DBMS_CLOUD TO MRDS_LOADER;

-- Cross-schema privileges required for data loading operations
GRANT SELECT ANY TABLE TO CT_MRDS;

-- Comprehensive ANY privileges for data loading and replication operations
GRANT CREATE ANY TABLE TO MRDS_LOADER;
GRANT DROP ANY TABLE TO MRDS_LOADER;
GRANT CREATE ANY VIEW TO MRDS_LOADER;
GRANT DROP ANY VIEW TO MRDS_LOADER;
GRANT SELECT ANY TABLE TO MRDS_LOADER;
GRANT INSERT ANY TABLE TO MRDS_LOADER;
GRANT UPDATE ANY TABLE TO MRDS_LOADER;
GRANT DELETE ANY TABLE TO MRDS_LOADER;
GRANT ALTER ANY TABLE TO MRDS_LOADER;
GRANT EXECUTE ANY PROCEDURE TO MRDS_LOADER;
GRANT SELECT ANY SEQUENCE TO MRDS_LOADER;