-- Migration: Create document_versions table
-- Created: 2024-01-01

CREATE TABLE IF NOT EXISTS document_versions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    document_id UUID NOT NULL REFERENCES documents(id) ON DELETE CASCADE,
    version_number INTEGER NOT NULL,
    summary_markdown TEXT NOT NULL,
    summary_pdf_path VARCHAR(1000) NOT NULL,
    feedback TEXT,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    
    -- Ensure unique version numbers per document
    UNIQUE(document_id, version_number)
);

-- Create indexes for better query performance
CREATE INDEX IF NOT EXISTS idx_document_versions_document_id ON document_versions(document_id);
CREATE INDEX IF NOT EXISTS idx_document_versions_version_number ON document_versions(version_number);
CREATE INDEX IF NOT EXISTS idx_document_versions_created_at ON document_versions(created_at);

-- Create composite index for document versions
CREATE INDEX IF NOT EXISTS idx_document_versions_document_version ON document_versions(document_id, version_number); 