-- Add document versioning and OCR fields

ALTER TABLE documents ADD COLUMN IF NOT EXISTS version INTEGER DEFAULT 1;
ALTER TABLE documents ADD COLUMN IF NOT EXISTS parent_document_id INTEGER REFERENCES documents(id);
ALTER TABLE documents ADD COLUMN IF NOT EXISTS password_protected BOOLEAN DEFAULT false;
ALTER TABLE documents ADD COLUMN IF NOT EXISTS password_hash VARCHAR(255);
ALTER TABLE documents ADD COLUMN IF NOT EXISTS ocr_text TEXT;
ALTER TABLE documents ADD COLUMN IF NOT EXISTS ocr_status VARCHAR(50) DEFAULT 'pending' CHECK (ocr_status IN ('pending', 'processing', 'completed', 'failed'));
ALTER TABLE documents ADD COLUMN IF NOT EXISTS ocr_confidence DECIMAL(5,2);
ALTER TABLE documents ADD COLUMN IF NOT EXISTS ai_verification_status VARCHAR(50) DEFAULT 'pending' CHECK (ai_verification_status IN ('pending', 'verified', 'rejected', 'needs_review'));
ALTER TABLE documents ADD COLUMN IF NOT EXISTS ai_verification_notes TEXT;
ALTER TABLE documents ADD COLUMN IF NOT EXISTS preview_url VARCHAR(500);

CREATE INDEX IF NOT EXISTS idx_documents_parent ON documents(parent_document_id);
CREATE INDEX IF NOT EXISTS idx_documents_ocr_status ON documents(ocr_status);
CREATE INDEX IF NOT EXISTS idx_documents_verification_status ON documents(ai_verification_status);
