-- Add folderId column to documents table for folder organization
-- Note: filename column already exists from migration 105
ALTER TABLE documents ADD COLUMN folderId uuid;

-- Add foreign key constraint to folders table
ALTER TABLE documents ADD CONSTRAINT fk_documents_folderid
    FOREIGN KEY (folderId) REFERENCES folders(id);

-- Add index for efficient folder-based document queries
CREATE INDEX idx_documents_folderid ON documents(folderId);

-- Add originalPath column to store the exact path provided during upload
-- This value is IMMUTABLE after creation - it preserves the original upload path
-- even if the document is later moved to a different virtual folder
ALTER TABLE documents ADD COLUMN originalPath text;
COMMENT ON COLUMN documents.originalPath IS
    'Original path provided during upload. IMMUTABLE after creation - never modify this value.';

-- Add comments to clarify immutability of S3 storage fields
COMMENT ON COLUMN documentEntries.key IS
    'S3 object key. IMMUTABLE after creation - never modify this value.';
COMMENT ON COLUMN documentEntries.bucket IS
    'S3 bucket name. IMMUTABLE after creation - never modify this value.';

-- Add comment to clarify that folders are virtual (database-only, no S3 relationship)
COMMENT ON TABLE folders IS
    'Virtual folder hierarchy for document organization. Database-only - has no direct relationship to S3 storage locations.';
COMMENT ON COLUMN folders.path IS
    'Virtual folder path (e.g., "/contracts/2025"). Can be renamed without affecting S3 storage.';
