-- Create rewards table
CREATE TABLE rewards (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    name VARCHAR(150) NOT NULL,
    reward_type VARCHAR(50) NOT NULL,     -- VOUCHER, PHYSICAL, DIGITAL, BALANCE
    cost_points BIGINT NOT NULL,
    stock INT,                            -- nullable if unlimited (digital rewards)
    max_per_customer INT DEFAULT 1,
    tnc JSONB,                            -- sections + rules + expiry_days
    metadata JSONB,                       -- brand, model, etc.
    images JSONB,                         -- array of images
    created_at TIMESTAMP DEFAULT now(),
    updated_at TIMESTAMP DEFAULT now()
);

-- Create indexes for better performance
CREATE INDEX idx_rewards_reward_type ON rewards(reward_type);
CREATE INDEX idx_rewards_cost_points ON rewards(cost_points);
CREATE INDEX idx_rewards_created_at ON rewards(created_at);
CREATE INDEX idx_rewards_stock ON rewards(stock) WHERE stock IS NOT NULL;

-- Add comments
COMMENT ON TABLE rewards IS 'Rewards catalog for gamification system';
COMMENT ON COLUMN rewards.reward_type IS 'Type of reward: VOUCHER, PHYSICAL, DIGITAL, BALANCE';
COMMENT ON COLUMN rewards.cost_points IS 'Points required to redeem this reward';
COMMENT ON COLUMN rewards.stock IS 'Available quantity (NULL for unlimited digital rewards)';
COMMENT ON COLUMN rewards.max_per_customer IS 'Maximum times a customer can redeem this reward';
COMMENT ON COLUMN rewards.tnc IS 'Terms and conditions in JSON format';
COMMENT ON COLUMN rewards.metadata IS 'Additional reward metadata (brand, model, etc.)';
COMMENT ON COLUMN rewards.images IS 'Array of image URLs and types';
