-- Migration: 004_receipt_items_enhancements.sql
-- Description: Add additional columns to receipt_items for better receipt processing
-- Created: 2026-01-12

-- Add line_number column for ordering items on receipt
ALTER TABLE public.receipt_items
    ADD COLUMN IF NOT EXISTS line_number INTEGER;
COMMENT ON COLUMN public.receipt_items.line_number IS 'Line number on the receipt for ordering items.';

-- Add match_confidence column for tracking matching confidence scores
ALTER TABLE public.receipt_items
    ADD COLUMN IF NOT EXISTS match_confidence NUMERIC(5,4);
ALTER TABLE public.receipt_items
    ADD CONSTRAINT receipt_items_match_confidence_check
    CHECK (match_confidence IS NULL OR (match_confidence >= 0 AND match_confidence <= 1));
COMMENT ON COLUMN public.receipt_items.match_confidence IS 'Confidence score (0.0-1.0) when matching to master_item or product.';

-- Add is_discount column to identify discount/coupon line items
ALTER TABLE public.receipt_items
    ADD COLUMN IF NOT EXISTS is_discount BOOLEAN DEFAULT FALSE NOT NULL;
COMMENT ON COLUMN public.receipt_items.is_discount IS 'Whether this line item represents a discount or coupon.';

-- Add unit_price_cents column for items sold by weight/volume
ALTER TABLE public.receipt_items
    ADD COLUMN IF NOT EXISTS unit_price_cents INTEGER;
ALTER TABLE public.receipt_items
    ADD CONSTRAINT receipt_items_unit_price_cents_check
    CHECK (unit_price_cents IS NULL OR unit_price_cents >= 0);
COMMENT ON COLUMN public.receipt_items.unit_price_cents IS 'Price per unit in cents (for items sold by weight/volume).';

-- Add unit_type column for unit of measurement
ALTER TABLE public.receipt_items
    ADD COLUMN IF NOT EXISTS unit_type TEXT;
COMMENT ON COLUMN public.receipt_items.unit_type IS 'Unit of measurement (e.g., lb, kg, each) for unit-priced items.';

-- Add added_to_pantry column to track pantry additions
ALTER TABLE public.receipt_items
    ADD COLUMN IF NOT EXISTS added_to_pantry BOOLEAN DEFAULT FALSE NOT NULL;
COMMENT ON COLUMN public.receipt_items.added_to_pantry IS 'Whether this item has been added to the user pantry inventory.';
