select max(week_start_date) from sjreport s 

CREATE OR REPLACE FUNCTION sjreport_files_to_process()
RETURNS TRIGGER AS $$
DECLARE
    payload JSON;
BEGIN
    -- Check if bucket_id is 'uploads' and the first element of path_tokens is 'sjreport'
    IF NEW.bucket_id = 'uploads' AND NEW.path_tokens[1] = 'sjreport' THEN
        -- Constructing the payload from NEW record data
        payload := json_build_object(
            'bucket_name', NEW.bucket_id,  -- Changed from id to bucket_id
            'file_name', NEW.name,
            'path_tokens', NEW.path_tokens
        );

        -- Log the API request details into the sjreport_uploads table
        INSERT INTO sjreport_uploads (payload)
        VALUES (payload);  -- Default processed value is false
    END IF;

    RETURN NEW;
END;
$$
 LANGUAGE plpgsql;
 
CREATE TABLE sjreport_uploads (
    id SERIAL PRIMARY KEY,
    payload JSONB,
    processed BOOLEAN DEFAULT false,
    created_at TIMESTAMP DEFAULT NOW()
);


CREATE TRIGGER after_insert_trigger
AFTER INSERT ON storage.objects 
FOR EACH ROW EXECUTE FUNCTION public.sjreport_files_to_process();