-- ============================================================================
-- ORACLE DIRECTORY OBJECTS FOR ROA UPDATE SYSTEM
-- ============================================================================
-- Creates 54 Oracle DIRECTORY objects for PACK_UPDATE module
-- Each directory corresponds to a ROAUPDATE subfolder
--
-- Usage:
--   sqlplus sys/password@service as sysdba @directories-roaupdate.sql
--
-- Prerequisites:
--   - Physical folders must exist (created by 08-post-install-config.ps1)
--   - Run as SYS with SYSDBA privileges
--
-- Substitution Variables:
--   &roaupdate_path - Base path for ROAUPDATE folder (e.g., D:\ROAUPDATE)
-- ============================================================================

SET ECHO OFF
SET FEEDBACK ON
SET SERVEROUTPUT ON
SET VERIFY OFF
WHENEVER SQLERROR CONTINUE

PROMPT
PROMPT ========================================
PROMPT Creating ROAUPDATE Directory Objects
PROMPT ========================================
PROMPT

-- Define base path (will prompt if not defined)
DEFINE roaupdate_path = '&roaupdate_path'

PROMPT Using ROAUPDATE path: &roaupdate_path
PROMPT

-- ============================================================================
-- SECTION 1: CREATE MAIN ROAUPDATE DIRECTORY
-- ============================================================================

PROMPT [1/3] Creating main ROAUPDATE directory...

CREATE OR REPLACE DIRECTORY UPD_ROAUPDATE AS '&roaupdate_path';

-- ============================================================================
-- SECTION 2: CREATE MODULE DIRECTORIES (54 directories)
-- ============================================================================

PROMPT [2/3] Creating module directories...

-- Common/Shared
CREATE OR REPLACE DIRECTORY UPD_COMUNROA AS '&roaupdate_path\_ARHIVE\COMUNROA';
CREATE OR REPLACE DIRECTORY UPD_USERREPORTS AS '&roaupdate_path\_ARHIVE\USERREPORTS';

-- Core Applications
CREATE OR REPLACE DIRECTORY UPD_ROAAPROV AS '&roaupdate_path\_ARHIVE\ROAAPROV';
CREATE OR REPLACE DIRECTORY UPD_ROAAUTO AS '&roaupdate_path\_ARHIVE\ROAAUTO';
CREATE OR REPLACE DIRECTORY UPD_ROABAVERT AS '&roaupdate_path\_ARHIVE\ROABAVERT';
CREATE OR REPLACE DIRECTORY UPD_ROACASA AS '&roaupdate_path\_ARHIVE\ROACASA';
CREATE OR REPLACE DIRECTORY UPD_ROACOMENZI AS '&roaupdate_path\_ARHIVE\ROACOMENZI';
CREATE OR REPLACE DIRECTORY UPD_ROACONIMPORT AS '&roaupdate_path\_ARHIVE\ROACONIMPORT';
CREATE OR REPLACE DIRECTORY UPD_ROACONSTRUCTII AS '&roaupdate_path\_ARHIVE\ROACONSTRUCTII';
CREATE OR REPLACE DIRECTORY UPD_ROACONT AS '&roaupdate_path\_ARHIVE\ROACONT';
CREATE OR REPLACE DIRECTORY UPD_ROACONTRACTE AS '&roaupdate_path\_ARHIVE\ROACONTRACTE';

-- Declarations & Definitions
CREATE OR REPLACE DIRECTORY UPD_ROADECL AS '&roaupdate_path\_ARHIVE\ROADECL';
CREATE OR REPLACE DIRECTORY UPD_ROADEF AS '&roaupdate_path\_ARHIVE\ROADEF';
CREATE OR REPLACE DIRECTORY UPD_ROADEFSALARII AS '&roaupdate_path\_ARHIVE\ROADEFSALARII';
CREATE OR REPLACE DIRECTORY UPD_ROADEPOZIT AS '&roaupdate_path\_ARHIVE\ROADEPOZIT';
CREATE OR REPLACE DIRECTORY UPD_ROADEVIZE AS '&roaupdate_path\_ARHIVE\ROADEVIZE';

-- Invoicing & Suppliers
CREATE OR REPLACE DIRECTORY UPD_ROAFACTURARE AS '&roaupdate_path\_ARHIVE\ROAFACTURARE';
CREATE OR REPLACE DIRECTORY UPD_ROAFURNIZORI AS '&roaupdate_path\_ARHIVE\ROAFURNIZORI';

-- General & Inventory
CREATE OR REPLACE DIRECTORY UPD_ROAGEN AS '&roaupdate_path\_ARHIVE\ROAGEN';
CREATE OR REPLACE DIRECTORY UPD_ROAGEST AS '&roaupdate_path\_ARHIVE\ROAGEST';

-- Restaurant & Hotel
CREATE OR REPLACE DIRECTORY UPD_ROAGRESTAURANT AS '&roaupdate_path\_ARHIVE\ROAGRESTAURANT';
CREATE OR REPLACE DIRECTORY UPD_ROAHOTEL AS '&roaupdate_path\_ARHIVE\ROAHOTEL';
CREATE OR REPLACE DIRECTORY UPD_ROAHOTELCONFIG AS '&roaupdate_path\_ARHIVE\ROAHOTELCONFIG';

-- Real Estate
CREATE OR REPLACE DIRECTORY UPD_ROAIMOB AS '&roaupdate_path\_ARHIVE\ROAIMOB';

-- Payroll
CREATE OR REPLACE DIRECTORY UPD_ROAINCHIDSAL AS '&roaupdate_path\_ARHIVE\ROAINCHIDSAL';

-- Services & Operations
CREATE OR REPLACE DIRECTORY UPD_ROALUCRARI AS '&roaupdate_path\_ARHIVE\ROALUCRARI';
CREATE OR REPLACE DIRECTORY UPD_ROAMANAGER AS '&roaupdate_path\_ARHIVE\ROAMANAGER';
CREATE OR REPLACE DIRECTORY UPD_ROAMASINI AS '&roaupdate_path\_ARHIVE\ROAMASINI';

-- Nomenclatures
CREATE OR REPLACE DIRECTORY UPD_ROANOR AS '&roaupdate_path\_ARHIVE\ROANOR';
CREATE OR REPLACE DIRECTORY UPD_ROANORFRUVIMED AS '&roaupdate_path\_ARHIVE\ROANORFRUVIMED';
CREATE OR REPLACE DIRECTORY UPD_ROANORRENAV AS '&roaupdate_path\_ARHIVE\ROANORRENAV';
CREATE OR REPLACE DIRECTORY UPD_ROANORUTMIDIA AS '&roaupdate_path\_ARHIVE\ROANORUTMIDIA';

-- Fixed Assets
CREATE OR REPLACE DIRECTORY UPD_ROAOBINV AS '&roaupdate_path\_ARHIVE\ROAOBINV';

-- Offers & Prices
CREATE OR REPLACE DIRECTORY UPD_ROAOFERTARE AS '&roaupdate_path\_ARHIVE\ROAOFERTARE';
CREATE OR REPLACE DIRECTORY UPD_ROAPRETURI AS '&roaupdate_path\_ARHIVE\ROAPRETURI';

-- Printing
CREATE OR REPLACE DIRECTORY UPD_ROAPRINT AS '&roaupdate_path\_ARHIVE\ROAPRINT';
CREATE OR REPLACE DIRECTORY UPD_ROAPRINT_INITIALIZARI AS '&roaupdate_path\_ARHIVE\ROAPRINT_INITIALIZARI';

-- Agriculture & Production
CREATE OR REPLACE DIRECTORY UPD_ROAPRODAGR AS '&roaupdate_path\_ARHIVE\ROAPRODAGR';
CREATE OR REPLACE DIRECTORY UPD_ROAPRODUCTIE AS '&roaupdate_path\_ARHIVE\ROAPRODUCTIE';

-- Documents
CREATE OR REPLACE DIRECTORY UPD_ROAREGISTRATURA AS '&roaupdate_path\_ARHIVE\ROAREGISTRATURA';

-- Salary Distribution
CREATE OR REPLACE DIRECTORY UPD_ROAREPARTIZSAL AS '&roaupdate_path\_ARHIVE\ROAREPARTIZSAL';

-- Reports & Resources
CREATE OR REPLACE DIRECTORY UPD_ROARES AS '&roaupdate_path\_ARHIVE\ROARES';

-- Retail & Restaurant
CREATE OR REPLACE DIRECTORY UPD_ROARESTAURANT AS '&roaupdate_path\_ARHIVE\ROARESTAURANT';
CREATE OR REPLACE DIRECTORY UPD_ROARETAIL AS '&roaupdate_path\_ARHIVE\ROARETAIL';
CREATE OR REPLACE DIRECTORY UPD_ROARETAILMON AS '&roaupdate_path\_ARHIVE\ROARETAILMON';

-- Payroll & HR
CREATE OR REPLACE DIRECTORY UPD_ROASAL AS '&roaupdate_path\_ARHIVE\ROASAL';
CREATE OR REPLACE DIRECTORY UPD_ROASALSPEC AS '&roaupdate_path\_ARHIVE\ROASALSPEC';

-- Financial Statements
CREATE OR REPLACE DIRECTORY UPD_ROASITFIN AS '&roaupdate_path\_ARHIVE\ROASITFIN';
CREATE OR REPLACE DIRECTORY UPD_ROASITOP AS '&roaupdate_path\_ARHIVE\ROASITOP';

-- Startup & Support
CREATE OR REPLACE DIRECTORY UPD_ROASTART AS '&roaupdate_path\_ARHIVE\ROASTART';
CREATE OR REPLACE DIRECTORY UPD_ROASUPORT AS '&roaupdate_path\_ARHIVE\ROASUPORT';

-- Telecom & Monitoring
CREATE OR REPLACE DIRECTORY UPD_ROATELCOMM AS '&roaupdate_path\_ARHIVE\ROATELCOMM';
CREATE OR REPLACE DIRECTORY UPD_ROATELMON AS '&roaupdate_path\_ARHIVE\ROATELMON';

-- Wine Industry
CREATE OR REPLACE DIRECTORY UPD_ROAVIN AS '&roaupdate_path\_ARHIVE\ROAVIN';

-- ============================================================================
-- SECTION 3: GRANT PERMISSIONS TO CONTAFIN_ORACLE
-- ============================================================================

PROMPT [3/3] Granting READ,WRITE permissions to CONTAFIN_ORACLE...

-- Main directory
GRANT READ, WRITE ON DIRECTORY UPD_ROAUPDATE TO CONTAFIN_ORACLE;

-- All module directories
GRANT READ, WRITE ON DIRECTORY UPD_COMUNROA TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_USERREPORTS TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAAPROV TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAAUTO TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROABAVERT TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROACASA TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROACOMENZI TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROACONIMPORT TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROACONSTRUCTII TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROACONT TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROACONTRACTE TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROADECL TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROADEF TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROADEFSALARII TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROADEPOZIT TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROADEVIZE TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAFACTURARE TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAFURNIZORI TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAGEN TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAGEST TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAGRESTAURANT TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAHOTEL TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAHOTELCONFIG TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAIMOB TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAINCHIDSAL TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROALUCRARI TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAMANAGER TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAMASINI TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROANOR TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROANORFRUVIMED TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROANORRENAV TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROANORUTMIDIA TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAOBINV TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAOFERTARE TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAPRETURI TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAPRINT TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAPRINT_INITIALIZARI TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAPRODAGR TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAPRODUCTIE TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAREGISTRATURA TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAREPARTIZSAL TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROARES TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROARESTAURANT TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROARETAIL TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROARETAILMON TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROASAL TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROASALSPEC TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROASITFIN TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROASITOP TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROASTART TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROASUPORT TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROATELCOMM TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROATELMON TO CONTAFIN_ORACLE;
GRANT READ, WRITE ON DIRECTORY UPD_ROAVIN TO CONTAFIN_ORACLE;

COMMIT;

-- ============================================================================
-- VERIFICATION
-- ============================================================================

PROMPT
PROMPT ========================================
PROMPT Directory Objects Verification
PROMPT ========================================
PROMPT

PROMPT UPD_* directories created:
SELECT directory_name, directory_path
FROM dba_directories
WHERE directory_name LIKE 'UPD_%'
ORDER BY directory_name;

PROMPT
PROMPT Directory grants to CONTAFIN_ORACLE:
SELECT table_name AS directory_name, privilege
FROM dba_tab_privs
WHERE grantee = 'CONTAFIN_ORACLE'
  AND table_name LIKE 'UPD_%'
ORDER BY table_name;

PROMPT
PROMPT ========================================
PROMPT Directory Objects Creation Complete
PROMPT ========================================
PROMPT
