CREATE TABLE batch_folder_candidates (
    batch_id uuid NOT NULL REFERENCES batch_uploads(id) ON DELETE CASCADE,
    folder_id uuid NOT NULL REFERENCES folders(id) ON DELETE CASCADE,
    path text NOT NULL,
    existed_before boolean NOT NULL,
    created_at timestamp NOT NULL DEFAULT NOW(),
    PRIMARY KEY (batch_id, folder_id)
);

CREATE INDEX idx_batch_folder_candidates_batch_id ON batch_folder_candidates(batch_id);
CREATE INDEX idx_batch_folder_candidates_folder_id ON batch_folder_candidates(folder_id);

ALTER TABLE batch_uploads
ADD COLUMN folder_cleanup_completed_at timestamp;

CREATE INDEX idx_batch_uploads_folder_cleanup_pending
ON batch_uploads(created_at)
WHERE folder_cleanup_completed_at IS NULL;
