-- Discount coupons for Tracecoin and package purchases CREATE TABLE IF NOT EXISTS coupons ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), code VARCHAR(50) NOT NULL UNIQUE, description TEXT, discount_type VARCHAR(20) NOT NULL, -- PERCENT, FLAT discount_value INTEGER NOT NULL, -- percent (0-100) or paise applies_to VARCHAR(50) NOT NULL DEFAULT 'ALL', -- ALL, TRACECOIN_BUNDLE, JOB_POSTING, CONTACT_VIEWS min_order_amount INTEGER NOT NULL DEFAULT 0, -- paise max_uses INTEGER, -- NULL = unlimited uses_count INTEGER NOT NULL DEFAULT 0, per_user_limit INTEGER NOT NULL DEFAULT 1, valid_from TIMESTAMPTZ NOT NULL DEFAULT NOW(), valid_until TIMESTAMPTZ, is_active BOOLEAN NOT NULL DEFAULT true, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); -- Track which users used which coupons CREATE TABLE IF NOT EXISTS coupon_uses ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), coupon_id UUID NOT NULL REFERENCES coupons(id) ON DELETE CASCADE, user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, payment_id UUID REFERENCES payments(id), used_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), UNIQUE (coupon_id, user_id) ); CREATE INDEX IF NOT EXISTS idx_coupons_code ON coupons(code); CREATE INDEX IF NOT EXISTS idx_coupon_uses_user_id ON coupon_uses(user_id);