-- Migration 005: Folders
-- Virtual folder hierarchy for document organization

CREATE TABLE folders (
    id uuid PRIMARY KEY DEFAULT uuid_generate_v7(),
    path text NOT NULL,
    parentId uuid,
    clientId varchar(255) NOT NULL,
    createdAt timestamp NOT NULL DEFAULT NOW(),
    createdBy varchar(255) NOT NULL,
    FOREIGN KEY (parentId) REFERENCES folders(id),
    FOREIGN KEY (clientId) REFERENCES clients(clientId),
    UNIQUE(clientId, path),
    CHECK (id != parentId)
);

-- Indexes for efficient folder queries
CREATE INDEX idx_folders_parentid ON folders(parentId);
CREATE INDEX idx_folders_clientid ON folders(clientId);
CREATE INDEX idx_folders_path ON folders(path);

-- 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.';
