-- Migration 011: Quick Analysis on Upload
-- Adds fields to store automatic CVR, document type, and document number detection

-- Add quick analysis fields to incoming_files
ALTER TABLE incoming_files 
ADD COLUMN IF NOT EXISTS detected_cvr VARCHAR(8),
ADD COLUMN IF NOT EXISTS detected_vendor_id INTEGER REFERENCES vendors(id),
ADD COLUMN IF NOT EXISTS detected_document_type VARCHAR(20),  -- 'invoice' or 'credit_note'
ADD COLUMN IF NOT EXISTS detected_document_number VARCHAR(100);

-- Add index for CVR lookups
CREATE INDEX IF NOT EXISTS idx_incoming_files_detected_cvr ON incoming_files(detected_cvr);
CREATE INDEX IF NOT EXISTS idx_incoming_files_detected_vendor ON incoming_files(detected_vendor_id);

-- Add comments
COMMENT ON COLUMN incoming_files.detected_cvr IS 'Automatically detected CVR number from PDF text';
COMMENT ON COLUMN incoming_files.detected_vendor_id IS 'Vendor matched by CVR on upload';
COMMENT ON COLUMN incoming_files.detected_document_type IS 'Auto-detected: invoice or credit_note';
COMMENT ON COLUMN incoming_files.detected_document_number IS 'Automatically extracted invoice/credit note number';
