-- ============================================================================
-- SCHEDULER JOBS FOR ROA UPDATE SYSTEM
-- ============================================================================
-- Creates Oracle Scheduler jobs for automatic daily updates:
--   - UPDATEROA_ZILNIC: Daily ROA application update (04:00)
--   - UPDATERTVAI_ZILNIC: Daily RTVAI module update (04:30)
--
-- Jobs are created DISABLED by default - enable manually after verification
--
-- Usage:
--   sqlplus sys/password@service as sysdba @scheduler-jobs.sql
--
-- Prerequisites:
--   - CONTAFIN_ORACLE schema with PACK_UPDATE and PACK_ROARTVAI packages
--   - SERVER_INFO configured (server-info-init.sql)
--   - Directory objects created (directories-roaupdate.sql)
-- ============================================================================

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

PROMPT
PROMPT ========================================
PROMPT Creating ROA Scheduler Jobs
PROMPT ========================================
PROMPT

-- ============================================================================
-- SECTION 1: DROP EXISTING JOBS (if any)
-- ============================================================================

PROMPT [1/4] Removing existing jobs (if any)...

BEGIN
    BEGIN
        DBMS_SCHEDULER.DROP_JOB(job_name => 'CONTAFIN_ORACLE.UPDATEROA_ZILNIC', force => TRUE);
        DBMS_OUTPUT.PUT_LINE('  Dropped existing UPDATEROA_ZILNIC job');
    EXCEPTION
        WHEN OTHERS THEN
            DBMS_OUTPUT.PUT_LINE('  UPDATEROA_ZILNIC job did not exist (OK)');
    END;

    BEGIN
        DBMS_SCHEDULER.DROP_JOB(job_name => 'CONTAFIN_ORACLE.UPDATERTVAI_ZILNIC', force => TRUE);
        DBMS_OUTPUT.PUT_LINE('  Dropped existing UPDATERTVAI_ZILNIC job');
    EXCEPTION
        WHEN OTHERS THEN
            DBMS_OUTPUT.PUT_LINE('  UPDATERTVAI_ZILNIC job did not exist (OK)');
    END;
END;
/

-- ============================================================================
-- SECTION 2: CREATE UPDATEROA_ZILNIC JOB
-- ============================================================================

PROMPT [2/4] Creating UPDATEROA_ZILNIC job...

BEGIN
    DBMS_SCHEDULER.CREATE_JOB(
        job_name            => 'CONTAFIN_ORACLE.UPDATEROA_ZILNIC',
        job_type            => 'STORED_PROCEDURE',
        job_action          => 'CONTAFIN_ORACLE.PACK_UPDATE.UPDATEROA',
        number_of_arguments => 6,
        start_date          => TRUNC(SYSDATE) + 1 + 4/24,  -- Tomorrow at 04:00
        repeat_interval     => 'FREQ=DAILY;INTERVAL=1',
        end_date            => NULL,
        job_class           => 'DEFAULT_JOB_CLASS',
        enabled             => FALSE,  -- DISABLED by default
        auto_drop           => FALSE,
        comments            => 'Daily ROA application update - runs at 04:00'
    );

    -- Set job arguments for PACK_UPDATE.UPDATEROA procedure
    -- Arguments: p_update_baza, p_update_aplicatii, p_update_rapoarte, p_module, p_versiune, p_nivel
    DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE(
        job_name          => 'CONTAFIN_ORACLE.UPDATEROA_ZILNIC',
        argument_position => 1,
        argument_value    => '1'  -- p_update_baza: Update database objects
    );

    DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE(
        job_name          => 'CONTAFIN_ORACLE.UPDATEROA_ZILNIC',
        argument_position => 2,
        argument_value    => '1'  -- p_update_aplicatii: Update applications
    );

    DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE(
        job_name          => 'CONTAFIN_ORACLE.UPDATEROA_ZILNIC',
        argument_position => 3,
        argument_value    => '1'  -- p_update_rapoarte: Update reports
    );

    DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE(
        job_name          => 'CONTAFIN_ORACLE.UPDATEROA_ZILNIC',
        argument_position => 4,
        argument_value    => ''   -- p_module: All modules (empty = all)
    );

    DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE(
        job_name          => 'CONTAFIN_ORACLE.UPDATEROA_ZILNIC',
        argument_position => 5,
        argument_value    => ''   -- p_versiune: Latest version (empty = latest)
    );

    DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE(
        job_name          => 'CONTAFIN_ORACLE.UPDATEROA_ZILNIC',
        argument_position => 6,
        argument_value    => '-3' -- p_nivel: Update level (-3 = production)
    );

    DBMS_OUTPUT.PUT_LINE('  UPDATEROA_ZILNIC job created (DISABLED)');
    DBMS_OUTPUT.PUT_LINE('  Schedule: Daily at 04:00');
    DBMS_OUTPUT.PUT_LINE('  Action: PACK_UPDATE.UPDATEROA(1, 1, 1, '''', '''', -3)');
END;
/

-- ============================================================================
-- SECTION 3: CREATE UPDATERTVAI_ZILNIC JOB
-- ============================================================================

PROMPT [3/4] Creating UPDATERTVAI_ZILNIC job...

BEGIN
    DBMS_SCHEDULER.CREATE_JOB(
        job_name            => 'CONTAFIN_ORACLE.UPDATERTVAI_ZILNIC',
        job_type            => 'STORED_PROCEDURE',
        job_action          => 'CONTAFIN_ORACLE.PACK_ROARTVAI.UPDATERTVAI',
        number_of_arguments => 0,
        start_date          => TRUNC(SYSDATE) + 1 + 4.5/24,  -- Tomorrow at 04:30
        repeat_interval     => 'FREQ=DAILY;INTERVAL=1',
        end_date            => NULL,
        job_class           => 'DEFAULT_JOB_CLASS',
        enabled             => FALSE,  -- DISABLED by default
        auto_drop           => FALSE,
        comments            => 'Daily RTVAI module update - runs at 04:30'
    );

    DBMS_OUTPUT.PUT_LINE('  UPDATERTVAI_ZILNIC job created (DISABLED)');
    DBMS_OUTPUT.PUT_LINE('  Schedule: Daily at 04:30');
    DBMS_OUTPUT.PUT_LINE('  Action: PACK_ROARTVAI.UPDATERTVAI()');
END;
/

-- ============================================================================
-- SECTION 4: GRANT SCHEDULER PRIVILEGES TO CONTAFIN_ORACLE
-- ============================================================================

PROMPT [4/4] Granting scheduler privileges to CONTAFIN_ORACLE...

-- Grant CREATE JOB privilege (may already exist)
BEGIN
    EXECUTE IMMEDIATE 'GRANT CREATE JOB TO CONTAFIN_ORACLE';
    DBMS_OUTPUT.PUT_LINE('  Granted CREATE JOB to CONTAFIN_ORACLE');
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('  CREATE JOB privilege already granted or error: ' || SQLERRM);
END;
/

-- Grant MANAGE SCHEDULER privilege for job management
BEGIN
    EXECUTE IMMEDIATE 'GRANT MANAGE SCHEDULER TO CONTAFIN_ORACLE';
    DBMS_OUTPUT.PUT_LINE('  Granted MANAGE SCHEDULER to CONTAFIN_ORACLE');
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('  MANAGE SCHEDULER privilege already granted or error: ' || SQLERRM);
END;
/

-- Grant CREATE EXTERNAL JOB for ExecuteScriptOS functionality
BEGIN
    EXECUTE IMMEDIATE 'GRANT CREATE EXTERNAL JOB TO CONTAFIN_ORACLE';
    DBMS_OUTPUT.PUT_LINE('  Granted CREATE EXTERNAL JOB to CONTAFIN_ORACLE');
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('  CREATE EXTERNAL JOB privilege already granted or error: ' || SQLERRM);
END;
/

COMMIT;

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

PROMPT
PROMPT ========================================
PROMPT Scheduler Jobs Verification
PROMPT ========================================
PROMPT

PROMPT CONTAFIN_ORACLE scheduled jobs:
SELECT job_name,
       job_type,
       enabled,
       state,
       TO_CHAR(next_run_date, 'YYYY-MM-DD HH24:MI:SS') AS next_run,
       repeat_interval
FROM dba_scheduler_jobs
WHERE owner = 'CONTAFIN_ORACLE'
  AND job_name IN ('UPDATEROA_ZILNIC', 'UPDATERTVAI_ZILNIC')
ORDER BY job_name;

PROMPT
PROMPT Job arguments for UPDATEROA_ZILNIC:
SELECT argument_position, argument_name, value
FROM dba_scheduler_job_args
WHERE owner = 'CONTAFIN_ORACLE'
  AND job_name = 'UPDATEROA_ZILNIC'
ORDER BY argument_position;

PROMPT
PROMPT ========================================
PROMPT Scheduler Jobs Creation Complete
PROMPT ========================================
PROMPT
PROMPT IMPORTANT: Jobs are created DISABLED by default.
PROMPT To enable a job after verifying configuration:
PROMPT
PROMPT   -- Enable UPDATEROA_ZILNIC
PROMPT   EXEC DBMS_SCHEDULER.ENABLE('CONTAFIN_ORACLE.UPDATEROA_ZILNIC');
PROMPT
PROMPT   -- Enable UPDATERTVAI_ZILNIC
PROMPT   EXEC DBMS_SCHEDULER.ENABLE('CONTAFIN_ORACLE.UPDATERTVAI_ZILNIC');
PROMPT
PROMPT To run a job immediately for testing:
PROMPT   EXEC DBMS_SCHEDULER.RUN_JOB('CONTAFIN_ORACLE.UPDATEROA_ZILNIC');
PROMPT
