-- Add refund and cancellation tracking fields
-- This migration adds support for refund processing and cancellation reasons

-- Add cancellation reason to orders
ALTER TABLE orders
ADD COLUMN IF NOT EXISTS cancellation_reason TEXT;

-- Add refund tracking to orders
ALTER TABLE orders
ADD COLUMN IF NOT EXISTS refunded_amount INTEGER DEFAULT 0,
ADD COLUMN IF NOT EXISTS refund_reason TEXT,
ADD COLUMN IF NOT EXISTS refunded_at TIMESTAMPTZ,
ADD COLUMN IF NOT EXISTS refunded_by UUID REFERENCES auth.users(id);

-- Add cancellation reason to consulting_slots
ALTER TABLE consulting_slots
ADD COLUMN IF NOT EXISTS cancellation_reason TEXT;

-- Add index for refund queries
CREATE INDEX IF NOT EXISTS idx_orders_refunded_at ON orders(refunded_at) WHERE refunded_at IS NOT NULL;

-- Add comment for documentation
COMMENT ON COLUMN orders.cancellation_reason IS 'Reason why order was cancelled (by user or admin)';
COMMENT ON COLUMN orders.refunded_amount IS 'Amount refunded to customer (in cents/minor units)';
COMMENT ON COLUMN orders.refund_reason IS 'Reason for refund';
COMMENT ON COLUMN orders.refunded_at IS 'Timestamp when refund was processed';
COMMENT ON COLUMN orders.refunded_by IS 'Admin user ID who processed the refund';
COMMENT ON COLUMN consulting_slots.cancellation_reason IS 'Reason why consulting slot was cancelled';
