create or replace procedure prc_26ai_backup_demohub( p_only_replicate in varchar2 default 'N')
authid current_user
as
  v_file_name varchar2(200) := '26ai_demohub' || to_char(sysdate,'ddmmrrrr');
  v_bucket varchar2(500) := 'https://objectstorage.sa-saopaulo-1.oraclecloud.com/n/idi1o0a010nx/b/bucket-database-26ai/o/';
  v_backup_start date;
  h1 NUMBER;
BEGIN
/*

  Criado por: fernando.leal@oracle.com
  Data: Oct/2025
  Objetivo: backup da base de dados do 26ai DemoHub

  v1 - leal

  Requer Scheduler de banco criado para executar 1x por semana (criado sob owner ADMIN)

  1- (aichat1) GRANT INHERIT PRIVILEGES ON USER AICHAT1 TO ADMIN;
     (admin) GRANT INHERIT PRIVILEGES ON USER ADMIN TO AICHAT1;
 
  2- 
  BEGIN
  DBMS_SCHEDULER.create_job (
    job_name        => 'job_26ai_backup_logico',
    job_type        => 'PLSQL_BLOCK',
    job_action      => 'begin aichat1.prc_26ai_backup_demohub; end;',
    start_date      => sysdate,
    repeat_interval => 'freq=weekly;',
    enabled         => TRUE);
END;

*/
  v_backup_start := sysdate;

  if p_only_replicate = 'N' then

          -- Open the export job
          h1 := DBMS_DATAPUMP.OPEN(
            operation => 'EXPORT',
            job_mode  => 'SCHEMA',
            job_name  => 'AICHAT_EXPORT_' ||  to_char(sysdate,'rrrrmmddhh24miss') || '_' || v_file_name,
            version   => 'LATEST'
          );

          -- Specify the dump file destination
          DBMS_DATAPUMP.ADD_FILE(
            handle   => h1,
            filename => v_file_name || '.dmp',
            directory => 'DATA_PUMP_DIR',
            filetype => DBMS_DATAPUMP.KU$_FILE_TYPE_DUMP_FILE,
            reusefile => 1
          );

          -- Specify the log file destination
          DBMS_DATAPUMP.ADD_FILE(
            handle   => h1,
            filename => v_file_name || '.log',
            directory => 'DATA_PUMP_DIR',
            filetype => DBMS_DATAPUMP.KU$_FILE_TYPE_LOG_FILE,
            reusefile => 1
          );

          -- Export only the GUEST schema
          DBMS_DATAPUMP.METADATA_FILTER(
            handle => h1,
            name   => 'SCHEMA_EXPR',
            value  => 'IN (''AICHAT1'')'
          );

          -- Start and detach
          DBMS_DATAPUMP.START_JOB(h1);
          DBMS_DATAPUMP.DETACH(h1);

         insert into aichat1.tb_26ai_backup_log
         values ( v_backup_start, sysdate, v_file_name  || '.dmp' , v_file_name  || '.log', v_bucket, 'latinoamerica');

         commit;
         
  -- replicacao do arquivo para object storage
  elsif p_only_replicate = 'Y' then

        DBMS_CLOUD.PUT_OBJECT(credential_name => 'OCI_CRED',
            object_uri => v_bucket,
            directory_name => 'DATA_PUMP_DIR',
            file_name => v_file_name || '.dmp');

        DBMS_CLOUD.PUT_OBJECT(credential_name => 'OCI_CRED',
            object_uri => v_bucket,
            directory_name => 'DATA_PUMP_DIR',
            file_name => v_file_name || '.log');    
      
        -- remove o arquivo temporario armazenado no DATA_PUMP_DIR
        DBMS_CLOUD.DELETE_FILE(
            directory_name => 'DATA_PUMP_DIR',
            file_name      => v_file_name  || '.dmp'
        );    
           -- remove o arquivo temporario armazenado no DATA_PUMP_DIR
        DBMS_CLOUD.DELETE_FILE(
            directory_name => 'DATA_PUMP_DIR',
            file_name      => v_file_name  || '.log'
        );    

   end if; 


   
END;
/