set serveroutput on
-- list markdown files in object storage
SELECT object_name, bytes, checksum, created, last_modified
FROM DBMS_CLOUD.LIST_OBJECTS(
  credential_name => 'OCI$RESOURCE_PRINCIPAL',
  location_uri    => 'https://objectstorage.us-chicago-1.oraclecloud.com/n/idi1o0a010nx/b/md-processed/o/'
);


-- create table and load markdown content into the database
DROP TABLE IF EXISTS markdown_files;

CREATE TABLE IF NOT EXISTS markdown_files (
  id             NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  source_uri     VARCHAR2(1000) NOT NULL,
  filename       VARCHAR2(500)  NOT NULL,
  file_hash      VARCHAR2(128),
  file_size      NUMBER,
  md_clob        CLOB,  
  status         VARCHAR2(30) DEFAULT 'PENDING',
  ingested_at    TIMESTAMP DEFAULT SYSTIMESTAMP,
  error_message  VARCHAR2(4000)
);

--
DECLARE
  l_clob CLOB;
  l_blob BLOB;
  l_uri      VARCHAR2(1000) := 'https://objectstorage.us-chicago-1.oraclecloud.com/n/idi1o0a010nx/b/md-processed/o/';
  l_filename VARCHAR2(500)  := 'Promocion-Reintegro-30-de-reintegro-en-comercios-adheridos-12-cuotas-sin-interese-_-DIC-2025v1.md';
BEGIN
    l_clob := blob_to_clob(DBMS_CLOUD.GET_OBJECT(credential_name => 'OCI$RESOURCE_PRINCIPAL', object_uri => l_uri || l_filename));

    INSERT INTO markdown_files (source_uri, filename, file_size, md_clob, file_hash, status)
    VALUES (l_uri || l_filename, 
            l_filename, 
            DBMS_LOB.GETLENGTH(l_clob), 
            l_clob, 
            DBMS_CRYPTO.HASH(l_clob, DBMS_CRYPTO.HASH_SH256), 
            'INGESTED'
        );

    COMMIT;    
    DBMS_OUTPUT.PUT_LINE('Markdown file loaded. Size: ' || DBMS_LOB.GETLENGTH(l_clob) || ' bytes');    
END;
/

-- test
SELECT * FROM markdown_files;








