-- Step 1: Create the stored procedure
CREATE OR REPLACE PROCEDURE manage_data IS
  new_products      INTEGER default 3;
  new_orders        INTEGER default 10;
  new_users         INTEGER default 2;
  new_transactions  INTEGER default 20;
  new_tasks         INTEGER default 5;
BEGIN
    FOR i IN 1..new_products LOOP
        INSERT INTO products (id, name, category, quantity)
        VALUES (seq_products.NEXTVAL, 
                DBMS_RANDOM.STRING('A', 10), 
                DBMS_RANDOM.STRING('A', 20), 
                TRUNC(DBMS_RANDOM.VALUE(1, 100)));
    END LOOP;

    
    FOR i IN 1..new_orders LOOP
        INSERT INTO orders (id, description, status)
        VALUES (seq_orders.NEXTVAL, 
                DBMS_RANDOM.STRING('A', 50), 
                DBMS_RANDOM.STRING('A', 20));
    END LOOP;

    
    FOR i IN 1..new_users LOOP
        INSERT INTO users (id, created_at, username, age, location)
        VALUES (seq_users.NEXTVAL, SYSDATE, 
                DBMS_RANDOM.STRING('A', 15), 
                TRUNC(DBMS_RANDOM.VALUE(18, 60)), 
                DBMS_RANDOM.STRING('A', 20));
    END LOOP;

    
    FOR i IN 1..new_transactions LOOP
        INSERT INTO transactions (id, amount, currency)
        VALUES (seq_transactions.NEXTVAL, 
                ROUND(DBMS_RANDOM.VALUE(1, 10000), 2), 
                DBMS_RANDOM.STRING('A', 3));
    END LOOP;

    
    FOR i IN 1..new_tasks LOOP
        INSERT INTO tasks (id, status, priority, type, assigned_to)
        VALUES (seq_tasks.NEXTVAL, 
                DBMS_RANDOM.STRING('A', 20), 
                TRUNC(DBMS_RANDOM.VALUE(1, 10)), 
                DBMS_RANDOM.STRING('A', 20), 
                DBMS_RANDOM.STRING('A', 15));
    END LOOP;

    -- Update 2 random rows in each table
    UPDATE products SET quantity = TRUNC(DBMS_RANDOM.VALUE(1, 200)) 
    WHERE id IN (SELECT id FROM products ORDER BY DBMS_RANDOM.VALUE FETCH FIRST 2 ROWS ONLY);

    UPDATE orders SET status = DBMS_RANDOM.STRING('A', 20) 
    WHERE id IN (SELECT id FROM orders ORDER BY DBMS_RANDOM.VALUE FETCH FIRST 2 ROWS ONLY);

    UPDATE users SET age = TRUNC(DBMS_RANDOM.VALUE(18, 75)) 
    WHERE id IN (SELECT id FROM users ORDER BY DBMS_RANDOM.VALUE FETCH FIRST 2 ROWS ONLY);

    UPDATE transactions SET amount = ROUND(DBMS_RANDOM.VALUE(1, 5000), 2) 
    WHERE id IN (SELECT id FROM transactions ORDER BY DBMS_RANDOM.VALUE FETCH FIRST 2 ROWS ONLY);

    UPDATE tasks SET priority = TRUNC(DBMS_RANDOM.VALUE(1, 10)) 
    WHERE id IN (SELECT id FROM tasks ORDER BY DBMS_RANDOM.VALUE FETCH FIRST 2 ROWS ONLY);

    -- Delete 1 random row from each table
    DELETE FROM products WHERE id = (SELECT id FROM products ORDER BY DBMS_RANDOM.VALUE FETCH FIRST 1 ROW ONLY);
    DELETE FROM orders WHERE id = (SELECT id FROM orders ORDER BY DBMS_RANDOM.VALUE FETCH FIRST 1 ROW ONLY);
    DELETE FROM users WHERE id = (SELECT id FROM users ORDER BY DBMS_RANDOM.VALUE FETCH FIRST 1 ROW ONLY);
    DELETE FROM transactions WHERE id = (SELECT id FROM transactions ORDER BY DBMS_RANDOM.VALUE FETCH FIRST 1 ROW ONLY);
    DELETE FROM tasks WHERE id = (SELECT id FROM tasks ORDER BY DBMS_RANDOM.VALUE FETCH FIRST 1 ROW ONLY);

    COMMIT;
END;
/

-- Step 2: Create a scheduled job to run every 10 seconds
BEGIN
    DBMS_SCHEDULER.create_job (
        job_name        => 'JOB_MANAGE_DATA',
        job_type        => 'PLSQL_BLOCK',
        job_action      => 'BEGIN manage_data; END;',
        start_date      => SYSTIMESTAMP,
        repeat_interval => 'FREQ=SECONDLY; INTERVAL=10',
        enabled         => TRUE
    );
END;
/

