-- ============================================================================
-- PUBLIC SYNONYMS FOR ROA APPLICATION
-- ============================================================================
-- Creates all public synonyms required by the ROA application
-- These synonyms provide cross-schema access to CONTAFIN_ORACLE objects
--
-- Usage:
--   @synonyms-public.sql
--
-- Connect as: SYSTEM or user with CREATE PUBLIC SYNONYM privilege
-- Must run AFTER CONTAFIN_ORACLE schema objects are imported
-- ============================================================================

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

PROMPT
PROMPT ========================================
PROMPT Creating Public Synonyms
PROMPT ========================================
PROMPT

-- ============================================================================
-- SECTION 1: CLEANUP - Drop existing synonyms (optional, for idempotency)
-- ============================================================================

PROMPT Cleaning up existing synonyms...

-- Core user/program synonyms
DROP PUBLIC SYNONYM SYN_LUNILEAN;
DROP PUBLIC SYNONYM SYN_PACK_DREPTURI;
DROP PUBLIC SYNONYM SYN_VDEF_UTIL_GRUP;
DROP PUBLIC SYNONYM SYN_DEF_GRUP_DREPT;
DROP PUBLIC SYNONYM SYN_UTILIZATORI;
DROP PUBLIC SYNONYM SYN_VDEF_UTIL_PROGRAME;
DROP PUBLIC SYNONYM SYN_VDEF_UTIL_OBIECTE;
DROP PUBLIC SYNONYM SYN_VUTILIZATORI;
DROP PUBLIC SYNONYM SYN_NOM_PROGRAME;
DROP PUBLIC SYNONYM SYN_DEF_PROGRAME;
DROP PUBLIC SYNONYM SYN_VDEF_PROGRAME;
DROP PUBLIC SYNONYM SYN_DEF_GRUP;
DROP PUBLIC SYNONYM SYN_NOM_FIRME;
DROP PUBLIC SYNONYM SYN_V_NOM_FIRME;
DROP PUBLIC SYNONYM SYN_VDEF_UTIL_FIRME;
DROP PUBLIC SYNONYM SYN_HELPCONT;
DROP PUBLIC SYNONYM SYN_OPTIUNI_PROGRAME;

-- Utility types/functions
DROP PUBLIC SYNONYM STRINGAGG;
DROP PUBLIC SYNONYM CHAR_ROW;
DROP PUBLIC SYNONYM CHAR_TAB;
DROP PUBLIC SYNONYM NUM_ROW;
DROP PUBLIC SYNONYM NUM_TAB;
DROP PUBLIC SYNONYM UW_SEL_ROW;
DROP PUBLIC SYNONYM UW_SEL_TAB;
DROP PUBLIC SYNONYM GETWORDCOUNT;
DROP PUBLIC SYNONYM GETWORDNUM;
DROP PUBLIC SYNONYM CHARC2COLLECTION;
DROP PUBLIC SYNONYM CHARN2COLLECTION;
DROP PUBLIC SYNONYM VALOARETAG;

-- Nomenclature synonyms
DROP PUBLIC SYNONYM SYN_NOM_FORME_JURIDICE;
DROP PUBLIC SYNONYM SYN_NOM_FORME_ORGANIZARE;
DROP PUBLIC SYNONYM SYN_NOM_TIP_SOCIETATE;
DROP PUBLIC SYNONYM SYN_NOM_FORME_PROPRIETATE;
DROP PUBLIC SYNONYM SYN_NOM_CETATENII;
DROP PUBLIC SYNONYM SYN_NOM_TARI;
DROP PUBLIC SYNONYM SYN_NOM_JUDETE;
DROP PUBLIC SYNONYM SYN_NOM_LOCALITATI;
DROP PUBLIC SYNONYM SYN_VNOM_JUDETE;
DROP PUBLIC SYNONYM SYN_VNOM_LOCALITATI;
DROP PUBLIC SYNONYM SYN_VNOM_TARI;
DROP PUBLIC SYNONYM SYN_NOM_CODURI_CAEN;
DROP PUBLIC SYNONYM SYN_NOM_TIPAPATRID;

-- Currency/exchange synonyms
DROP PUBLIC SYNONYM SYN_VNOM_VALUTE_ISO;
DROP PUBLIC SYNONYM SYN_NOM_VALUTE_ISO;
DROP PUBLIC SYNONYM SYN_CURS_COTATII;
DROP PUBLIC SYNONYM SYN_CURS_ACTUALIZARI;

-- Salary module synonyms
DROP PUBLIC SYNONYM SYN_SAL_NOM_TEMEI;
DROP PUBLIC SYNONYM SYN_SAL_NOM_TIPAUTORIZATIE;
DROP PUBLIC SYNONYM SYN_SAL_NOM_TIP_SPOR;
DROP PUBLIC SYNONYM SYN_SAL_NOM_TIP_NORME;
DROP PUBLIC SYNONYM SYN_SAL_NOM_STARI_CTR;
DROP PUBLIC SYNONYM SYN_SAL_NOM_DURATA_MUNCA;
DROP PUBLIC SYNONYM SYN_SAL_NOM_REPARTIZARE_MUNCA;
DROP PUBLIC SYNONYM SYN_SAL_NOM_INTERVALE_MUNCA;
DROP PUBLIC SYNONYM SYN_SAL_COR;
DROP PUBLIC SYNONYM SYN_SAL_ACTUALIZARE_COR;
DROP PUBLIC SYNONYM SAL_CONTRACT_M;
DROP PUBLIC SYNONYM CONTRACT_M;
DROP PUBLIC SYNONYM SAL_RED;
DROP PUBLIC SYNONYM SAL_CAMPURI_RED;
DROP PUBLIC SYNONYM TABSTERS;
DROP PUBLIC SYNONYM SAL_TABELESTERS;

-- Attachment synonyms
DROP PUBLIC SYNONYM SYN_ATAS_ATASAMENTE;
DROP PUBLIC SYNONYM SYN_ATAS_REFERINTE;

-- Utility packages
DROP PUBLIC SYNONYM PACK_UPDATE;
DROP PUBLIC SYNONYM PACK_UTILS;
DROP PUBLIC SYNONYM PACK_UTILS_FILE;
DROP PUBLIC SYNONYM SYN_PACK_DEF_CO;
DROP PUBLIC SYNONYM PACK_ROARTVAI;

-- RTVAI module
DROP PUBLIC SYNONYM RTVAI_AGENTI;
DROP PUBLIC SYNONYM RTVAI_ISTORIC;

-- Additional types
DROP PUBLIC SYNONYM FF_SUME;
DROP PUBLIC SYNONYM FF_PERSINTRET;
DROP PUBLIC SYNONYM VANZARI_DETALII_TAB;
DROP PUBLIC SYNONYM PIVOT_TABLE;
DROP PUBLIC SYNONYM PIVOT_ROW;
DROP PUBLIC SYNONYM TABINCHIDERETVA;
DROP PUBLIC SYNONYM TABELAVALORITAGURI;
DROP PUBLIC SYNONYM RANDINCHIDERETVA;
DROP PUBLIC SYNONYM SERVER_INFO;
DROP PUBLIC SYNONYM SYN_VNOM_UM_ISO;

-- ============================================================================
-- SECTION 2: CORE USER/PROGRAM MANAGEMENT SYNONYMS
-- ============================================================================

PROMPT
PROMPT [1/10] Creating core user/program synonyms...

CREATE PUBLIC SYNONYM SYN_DEF_GRUP FOR CONTAFIN_ORACLE.DEF_GRUP;
CREATE PUBLIC SYNONYM SYN_DEF_PROGRAME FOR CONTAFIN_ORACLE.DEF_PROGRAME;
CREATE PUBLIC SYNONYM SYN_VDEF_PROGRAME FOR CONTAFIN_ORACLE.VDEF_PROGRAME;
CREATE PUBLIC SYNONYM SYN_LUNILEAN FOR CONTAFIN_ORACLE.LUNILEAN;
CREATE PUBLIC SYNONYM SYN_NOM_FIRME FOR CONTAFIN_ORACLE.NOM_FIRME;
CREATE PUBLIC SYNONYM SYN_NOM_PROGRAME FOR CONTAFIN_ORACLE.NOM_PROGRAME;
CREATE PUBLIC SYNONYM SYN_PACK_DREPTURI FOR CONTAFIN_ORACLE.PACK_DREPTURI;
CREATE PUBLIC SYNONYM SYN_UTILIZATORI FOR CONTAFIN_ORACLE.UTILIZATORI;
CREATE PUBLIC SYNONYM SYN_VDEF_UTIL_PROGRAME FOR CONTAFIN_ORACLE.VDEF_UTIL_PROGRAME;
CREATE PUBLIC SYNONYM SYN_VDEF_UTIL_FIRME FOR CONTAFIN_ORACLE.VDEF_UTIL_FIRME;
CREATE PUBLIC SYNONYM SYN_VDEF_UTIL_OBIECTE FOR CONTAFIN_ORACLE.VDEF_UTIL_OBIECTE;
CREATE PUBLIC SYNONYM SYN_VUTILIZATORI FOR CONTAFIN_ORACLE.VUTILIZATORI;
CREATE PUBLIC SYNONYM SYN_VDEF_UTIL_GRUP FOR CONTAFIN_ORACLE.VDEF_UTIL_GRUP;
CREATE PUBLIC SYNONYM SYN_DEF_GRUP_DREPT FOR CONTAFIN_ORACLE.DEF_GRUP_DREPT;
CREATE PUBLIC SYNONYM SYN_OPTIUNI_PROGRAME FOR CONTAFIN_ORACLE.OPTIUNI_PROGRAME;
CREATE PUBLIC SYNONYM SYN_HELPCONT FOR CONTAFIN_ORACLE.HELPCONT;
CREATE PUBLIC SYNONYM SYN_V_NOM_FIRME FOR CONTAFIN_ORACLE.V_NOM_FIRME;

-- ============================================================================
-- SECTION 3: UTILITY TYPES AND FUNCTIONS
-- ============================================================================

PROMPT [2/10] Creating utility type synonyms...

CREATE PUBLIC SYNONYM STRINGAGG FOR CONTAFIN_ORACLE.STRINGAGG;
CREATE PUBLIC SYNONYM CHAR_ROW FOR CONTAFIN_ORACLE.CHAR_ROW;
CREATE PUBLIC SYNONYM CHAR_TAB FOR CONTAFIN_ORACLE.CHAR_TAB;
CREATE PUBLIC SYNONYM NUM_ROW FOR CONTAFIN_ORACLE.NUM_ROW;
CREATE PUBLIC SYNONYM NUM_TAB FOR CONTAFIN_ORACLE.NUM_TAB;
CREATE PUBLIC SYNONYM UW_SEL_ROW FOR CONTAFIN_ORACLE.UW_SEL_ROW;
CREATE PUBLIC SYNONYM UW_SEL_TAB FOR CONTAFIN_ORACLE.UW_SEL_TAB;
CREATE PUBLIC SYNONYM VALOARETAG FOR CONTAFIN_ORACLE.VALOARETAG;
CREATE PUBLIC SYNONYM GETWORDCOUNT FOR CONTAFIN_ORACLE.GETWORDCOUNT;
CREATE PUBLIC SYNONYM GETWORDNUM FOR CONTAFIN_ORACLE.GETWORDNUM;
CREATE PUBLIC SYNONYM CHARC2COLLECTION FOR CONTAFIN_ORACLE.CHARC2COLLECTION;
CREATE PUBLIC SYNONYM CHARN2COLLECTION FOR CONTAFIN_ORACLE.CHARN2COLLECTION;

-- ============================================================================
-- SECTION 4: NOMENCLATURE SYNONYMS (Legal Forms, Geography)
-- ============================================================================

PROMPT [3/10] Creating nomenclature synonyms...

CREATE PUBLIC SYNONYM SYN_NOM_FORME_JURIDICE FOR CONTAFIN_ORACLE.NOM_FORME_JURIDICE;
CREATE PUBLIC SYNONYM SYN_NOM_FORME_ORGANIZARE FOR CONTAFIN_ORACLE.NOM_FORME_ORGANIZARE;
CREATE PUBLIC SYNONYM SYN_NOM_TIP_SOCIETATE FOR CONTAFIN_ORACLE.NOM_TIP_SOCIETATE;
CREATE PUBLIC SYNONYM SYN_NOM_FORME_PROPRIETATE FOR CONTAFIN_ORACLE.NOM_FORME_PROPRIETATE;
CREATE PUBLIC SYNONYM SYN_NOM_CETATENII FOR CONTAFIN_ORACLE.NOM_CETATENII;
CREATE PUBLIC SYNONYM SYN_NOM_TARI FOR CONTAFIN_ORACLE.NOM_TARI;
CREATE PUBLIC SYNONYM SYN_NOM_JUDETE FOR CONTAFIN_ORACLE.NOM_JUDETE;
CREATE PUBLIC SYNONYM SYN_NOM_LOCALITATI FOR CONTAFIN_ORACLE.NOM_LOCALITATI;
CREATE PUBLIC SYNONYM SYN_VNOM_JUDETE FOR CONTAFIN_ORACLE.VNOM_JUDETE;
CREATE PUBLIC SYNONYM SYN_VNOM_LOCALITATI FOR CONTAFIN_ORACLE.VNOM_LOCALITATI;
CREATE PUBLIC SYNONYM SYN_VNOM_TARI FOR CONTAFIN_ORACLE.VNOM_TARI;
CREATE PUBLIC SYNONYM SYN_NOM_CODURI_CAEN FOR CONTAFIN_ORACLE.NOM_CODURI_CAEN;
CREATE PUBLIC SYNONYM SYN_NOM_TIPAPATRID FOR CONTAFIN_ORACLE.NOM_TIPAPATRID;

-- ============================================================================
-- SECTION 5: CURRENCY AND EXCHANGE RATE SYNONYMS
-- ============================================================================

PROMPT [4/10] Creating currency/exchange synonyms...

CREATE PUBLIC SYNONYM SYN_VNOM_VALUTE_ISO FOR CONTAFIN_ORACLE.VNOM_VALUTE_ISO;
CREATE PUBLIC SYNONYM SYN_NOM_VALUTE_ISO FOR CONTAFIN_ORACLE.NOM_VALUTE_ISO;
CREATE PUBLIC SYNONYM SYN_CURS_COTATII FOR CONTAFIN_ORACLE.CURS_COTATII;
CREATE PUBLIC SYNONYM SYN_CURS_ACTUALIZARI FOR CONTAFIN_ORACLE.CURS_ACTUALIZARI;
CREATE PUBLIC SYNONYM SYN_VNOM_UM_ISO FOR CONTAFIN_ORACLE.VNOM_UM_ISO;

-- ============================================================================
-- SECTION 6: SALARY MODULE SYNONYMS
-- ============================================================================

PROMPT [5/10] Creating salary module synonyms...

CREATE PUBLIC SYNONYM SYN_SAL_NOM_TEMEI FOR CONTAFIN_ORACLE.SAL_NOM_TEMEI;
CREATE PUBLIC SYNONYM SYN_SAL_NOM_TIPAUTORIZATIE FOR CONTAFIN_ORACLE.SAL_NOM_TIPAUTORIZATIE;
CREATE PUBLIC SYNONYM SYN_SAL_NOM_TIP_SPOR FOR CONTAFIN_ORACLE.SAL_NOM_TIP_SPOR;
CREATE PUBLIC SYNONYM SYN_SAL_NOM_TIP_NORME FOR CONTAFIN_ORACLE.SAL_NOM_TIP_NORME;
CREATE PUBLIC SYNONYM SYN_SAL_NOM_STARI_CTR FOR CONTAFIN_ORACLE.SAL_NOM_STARI_CTR;
CREATE PUBLIC SYNONYM SYN_SAL_NOM_DURATA_MUNCA FOR CONTAFIN_ORACLE.SAL_NOM_DURATA_MUNCA;
CREATE PUBLIC SYNONYM SYN_SAL_NOM_REPARTIZARE_MUNCA FOR CONTAFIN_ORACLE.SAL_NOM_REPARTIZARE_MUNCA;
CREATE PUBLIC SYNONYM SYN_SAL_NOM_INTERVALE_MUNCA FOR CONTAFIN_ORACLE.SAL_NOM_INTERVALE_MUNCA;
CREATE PUBLIC SYNONYM SYN_SAL_COR FOR CONTAFIN_ORACLE.SAL_COR;
CREATE PUBLIC SYNONYM SYN_SAL_ACTUALIZARE_COR FOR CONTAFIN_ORACLE.SAL_ACTUALIZARE_COR;

-- Salary types (must be created after CONTAFIN_ORACLE types exist)
PROMPT [6/10] Creating salary contract types...
CREATE PUBLIC SYNONYM SAL_CONTRACT_M FOR CONTAFIN_ORACLE.SAL_CONTRACT_M;
CREATE PUBLIC SYNONYM CONTRACT_M FOR CONTAFIN_ORACLE.CONTRACT_M;
CREATE PUBLIC SYNONYM SAL_RED FOR CONTAFIN_ORACLE.SAL_RED;
CREATE PUBLIC SYNONYM SAL_CAMPURI_RED FOR CONTAFIN_ORACLE.SAL_CAMPURI_RED;
CREATE PUBLIC SYNONYM TABSTERS FOR CONTAFIN_ORACLE.TABSTERS;
CREATE PUBLIC SYNONYM SAL_TABELESTERS FOR CONTAFIN_ORACLE.SAL_TABELESTERS;

-- ============================================================================
-- SECTION 7: ATTACHMENT SYNONYMS
-- ============================================================================

PROMPT [7/10] Creating attachment synonyms...

CREATE PUBLIC SYNONYM SYN_ATAS_ATASAMENTE FOR CONTAFIN_ORACLE.ATAS_ATASAMENTE;
CREATE PUBLIC SYNONYM SYN_ATAS_REFERINTE FOR CONTAFIN_ORACLE.ATAS_REFERINTE;

-- ============================================================================
-- SECTION 8: UTILITY PACKAGES
-- ============================================================================

PROMPT [8/10] Creating utility package synonyms...

CREATE PUBLIC SYNONYM PACK_UPDATE FOR CONTAFIN_ORACLE.PACK_UPDATE;
CREATE PUBLIC SYNONYM PACK_UTILS FOR CONTAFIN_ORACLE.PACK_UTILS;
CREATE PUBLIC SYNONYM PACK_UTILS_FILE FOR CONTAFIN_ORACLE.PACK_UTILS_FILE;
CREATE PUBLIC SYNONYM SYN_PACK_DEF_CO FOR CONTAFIN_ORACLE.PACK_DEF_CO;
CREATE PUBLIC SYNONYM PACK_ROARTVAI FOR CONTAFIN_ORACLE.PACK_ROARTVAI;

-- ============================================================================
-- SECTION 9: RTVAI MODULE AND ADDITIONAL TYPES
-- ============================================================================

PROMPT [9/10] Creating RTVAI and additional type synonyms...

CREATE PUBLIC SYNONYM RTVAI_AGENTI FOR CONTAFIN_ORACLE.RTVAI_AGENTI;
CREATE PUBLIC SYNONYM RTVAI_ISTORIC FOR CONTAFIN_ORACLE.RTVAI_ISTORIC;

CREATE PUBLIC SYNONYM FF_SUME FOR CONTAFIN_ORACLE.FF_SUME;
CREATE PUBLIC SYNONYM FF_PERSINTRET FOR CONTAFIN_ORACLE.FF_PERSINTRET;
CREATE PUBLIC SYNONYM VANZARI_DETALII_TAB FOR CONTAFIN_ORACLE.VANZARI_DETALII_TAB;
CREATE PUBLIC SYNONYM PIVOT_TABLE FOR CONTAFIN_ORACLE.PIVOT_TABLE;
CREATE PUBLIC SYNONYM PIVOT_ROW FOR CONTAFIN_ORACLE.PIVOT_ROW;
CREATE PUBLIC SYNONYM TABINCHIDERETVA FOR CONTAFIN_ORACLE.TABINCHIDERETVA;
CREATE PUBLIC SYNONYM TABELAVALORITAGURI FOR CONTAFIN_ORACLE.TABELAVALORITAGURI;
CREATE PUBLIC SYNONYM RANDINCHIDERETVA FOR CONTAFIN_ORACLE.RANDINCHIDERETVA;
CREATE PUBLIC SYNONYM SERVER_INFO FOR CONTAFIN_ORACLE.SERVER_INFO;

-- ============================================================================
-- SECTION 10: APPLICATION CONTEXT AND SPECIAL SYNONYMS
-- ============================================================================

PROMPT [10/10] Creating application context and special synonyms...

-- Create application context for session variables
CREATE CONTEXT SESIUNE USING CONTAFIN_ORACLE.SET_VARIABILE;

-- Private synonym for license view (in CONTAFIN_ORACLE schema)
-- This links to the SYS.VAUTH_SERII view
BEGIN
    EXECUTE IMMEDIATE 'DROP SYNONYM CONTAFIN_ORACLE.VDEF_PROGRAME_SERII';
EXCEPTION
    WHEN OTHERS THEN NULL;
END;
/

CREATE SYNONYM CONTAFIN_ORACLE.VDEF_PROGRAME_SERII FOR SYS.VAUTH_SERII;

PROMPT
PROMPT ========================================
PROMPT Public Synonym Creation Complete
PROMPT ========================================
PROMPT

-- Verification
PROMPT Total public synonyms created:
SELECT COUNT(*) AS synonym_count
FROM dba_synonyms
WHERE owner = 'PUBLIC'
  AND table_owner = 'CONTAFIN_ORACLE';

PROMPT
PROMPT Public synonyms list:
SELECT synonym_name, table_name
FROM dba_synonyms
WHERE owner = 'PUBLIC'
  AND table_owner = 'CONTAFIN_ORACLE'
ORDER BY synonym_name;

PROMPT
