-- Migration 006: Documents (Consolidated)
-- Document storage and processing tables
-- Merges: 005_documents + 103 + 105 + 106 + 107 + 108 + 111 + 116 + 118
-- NOTE: documentTextExtractions and documentTextExtractionEntries removed (no longer used)

-- Enum for document clean failure types
CREATE TYPE cleanFailType AS ENUM (
    'invalid_mimetype',
    'invalid_read',
    'invalid_read_pages',
    'zero_page_count',
    'large_file',
    'small_dimensions',
    'large_dimensions',
    'small_dpi',
    'large_dpi'
);

-- Enum for clean document mime types
CREATE TYPE cleanMimeType AS ENUM ('application/pdf');

-- Main documents table with all columns
CREATE TABLE documents (
    id uuid PRIMARY KEY DEFAULT uuid_generate_v7(),
    clientId varchar(255) NOT NULL,
    hash text NOT NULL,

    -- Batch reference (from migration 103)
    batch_id uuid,

    -- Filename (from migration 105)
    filename TEXT,

    -- Folder organization (from migration 111)
    folderId uuid,
    originalPath text,

    -- File size (from migration 118)
    file_size_bytes BIGINT,

    FOREIGN KEY (clientId) REFERENCES clients(clientId),
    FOREIGN KEY (batch_id) REFERENCES batch_uploads(id),
    FOREIGN KEY (folderId) REFERENCES folders(id),
    UNIQUE(clientId, hash)
);

-- Indexes for documents table
CREATE INDEX idx_documents_batch_id ON documents(batch_id);
CREATE INDEX idx_documents_filename ON documents(filename);
CREATE INDEX idx_documents_folderid ON documents(folderId);

-- Comments for immutable fields (from migration 111)
COMMENT ON COLUMN documents.originalPath IS
    'Original path provided during upload. IMMUTABLE after creation - never modify this value.';

-- Document uploads table (staging area for incoming documents)
CREATE TABLE documentUploads (
    id uuid PRIMARY KEY DEFAULT uuid_generate_v7(),
    bucket text NOT NULL,
    key text NOT NULL,
    clientId varchar(255) NOT NULL,
    part unsignedsmallint NOT NULL,
    createdAt timestamp NOT NULL,

    -- Filename (from migration 106)
    filename TEXT,

    -- Batch reference (from migration 107)
    batch_id uuid,

    -- Folder reference (from migration 116)
    folder_id uuid,

    FOREIGN KEY (clientId) REFERENCES clients(clientId),
    FOREIGN KEY (folder_id) REFERENCES folders(id)
);

-- Indexes for documentUploads
CREATE INDEX idx_documentuploads_filename ON documentUploads(filename);
CREATE INDEX idx_documentuploads_batch_id ON documentUploads(batch_id);
CREATE INDEX idx_documentuploads_clientid ON documentUploads(clientId);
CREATE INDEX idx_documentuploads_folder_id ON documentUploads(folder_id);

-- Document entries (S3 storage records)
CREATE TABLE documentEntries (
    id uuid PRIMARY KEY DEFAULT uuid_generate_v7(),
    documentId uuid NOT NULL,
    bucket text NOT NULL,
    key text NOT NULL,
    FOREIGN KEY (documentId) REFERENCES documents(id)
);

-- Comments for immutable fields (from migration 111)
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.';

-- Document cleans (processed/cleaned documents)
CREATE TABLE documentCleans (
    id uuid PRIMARY KEY DEFAULT uuid_generate_v7(),
    documentId uuid NOT NULL,
    bucket text,
    key text,
    hash text,
    mimetype cleanMimeType,
    fail cleanFailType,
    FOREIGN KEY (documentId) REFERENCES documents(id),
    CONSTRAINT bucket_and_key_together CHECK (
        (bucket IS NULL) = (key IS NULL) AND
        (bucket IS NULL) = (mimetype IS NULL) AND
        (bucket IS NULL) = (hash IS NULL)
    ),
    CONSTRAINT location_xor_fail CHECK (
        (bucket IS NOT NULL) != (fail IS NOT NULL)
    )
);

-- Document clean entries (version tracking for cleans)
CREATE TABLE documentCleanEntries (
    id uuid PRIMARY KEY DEFAULT uuid_generate_v7(),
    cleanId uuid NOT NULL,
    version bigint NOT NULL,
    FOREIGN KEY (cleanId) REFERENCES documentCleans(id)
);
