-- CREATE TYPE ReferralStatus AS ENUM ('PENDING', 'COMPLETED', 'EXPIRED', 'CANCELLED');
-- CREATE TABLE IF NOT EXISTS referral_settings (
--     id BIGSERIAL PRIMARY KEY,
--     referral_reward_amount DECIMAL(15, 2) NOT NULL DEFAULT 0.00,
--     cashback_percentage DECIMAL(5, 2) NOT NULL DEFAULT 0.00,
--     bet_referral_bonus_percentage NUMERIC DEFAULT 5.0,
--     max_referrals INTEGER NOT NULL DEFAULT 0,
--     expires_after_days INTEGER NOT NULL DEFAULT 30,
--     updated_by VARCHAR(255) NOT NULL,
--     created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
--     updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
--     version INTEGER NOT NULL DEFAULT 0,
--     CONSTRAINT referral_reward_amount_positive CHECK (referral_reward_amount >= 0),
--     CONSTRAINT cashback_percentage_range CHECK (
--         cashback_percentage >= 0
--         AND cashback_percentage <= 100
--     )
-- );
CREATE TABLE IF NOT EXISTS referral_codes (
    id BIGSERIAL PRIMARY KEY,
    referral_code VARCHAR(10) NOT NULL UNIQUE,
    referrer_id BIGINT NOT NULL UNIQUE REFERENCES users (id),
    company_id BIGINT NOT NULL REFERENCES companies (id),
    is_active BOOLEAN NOT NULL DEFAULT true,
    number_of_referrals BIGINT NOT NULL,
    reward_amount BIGINT NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT reward_amount_positive CHECK (reward_amount >= 0)
);
CREATE INDEX idx_referrals_referrer_id ON referral_codes (referrer_id);
CREATE TABLE IF NOT EXISTS user_referrals (
    id BIGSERIAL PRIMARY KEY,
    referred_id BIGINT UNIQUE NOT NULL REFERENCES users (id),
    referral_code_id BIGINT NOT NULL REFERENCES referral_codes (id),
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);