-- Create campaigns table
CREATE TABLE campaigns (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    name VARCHAR(150) NOT NULL,
    description TEXT,
    type VARCHAR(50) NOT NULL,         -- REWARD, POINTS, TOKENS, MIXED
    start_date TIMESTAMP NOT NULL,
    end_date TIMESTAMP NOT NULL,
    is_active BOOLEAN DEFAULT true,
    show_on_app BOOLEAN DEFAULT true,  -- apakah campaign muncul di aplikasi
    position INT DEFAULT 0,            -- urutan tampil di aplikasi
    metadata JSONB,                    -- fleksibel: banner, theme, config lain
    created_at TIMESTAMP DEFAULT now(),
    updated_at TIMESTAMP DEFAULT now()
);

-- Create campaign_rules table
CREATE TABLE campaign_rules (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    campaign_id UUID NOT NULL REFERENCES campaigns(id) ON DELETE CASCADE,
    
    -- Rule definition
    rule_type VARCHAR(50) NOT NULL,       -- TIER, SPEND, PRODUCT, CATEGORY, DAY, LOCATION
    condition_value VARCHAR(255),         -- e.g. SILVER, 50000, uuid-product, MONDAY
    
    -- Reward definition
    reward_type VARCHAR(50) NOT NULL,     -- POINTS, TOKENS, REWARD
    reward_value BIGINT,                  -- e.g. 10 (points/tokens), 2 (multiplier)
    reward_subtype VARCHAR(50),           -- e.g. MULTIPLIER, SPIN, BONUS, PHYSICAL
    reward_ref_id UUID,                   -- FK ke rewards.id kalau reward_type = REWARD
    
    metadata JSONB,                       -- fleksibel: extra configs
    
    created_at TIMESTAMP DEFAULT now(),
    updated_at TIMESTAMP DEFAULT now()
);

-- Create indexes for better performance
CREATE INDEX idx_campaigns_type ON campaigns(type);
CREATE INDEX idx_campaigns_start_date ON campaigns(start_date);
CREATE INDEX idx_campaigns_end_date ON campaigns(end_date);
CREATE INDEX idx_campaigns_is_active ON campaigns(is_active);
CREATE INDEX idx_campaigns_show_on_app ON campaigns(show_on_app);
CREATE INDEX idx_campaigns_position ON campaigns(position);

CREATE INDEX idx_campaign_rules_campaign_id ON campaign_rules(campaign_id);
CREATE INDEX idx_campaign_rules_rule_type ON campaign_rules(rule_type);
CREATE INDEX idx_campaign_rules_reward_type ON campaign_rules(reward_type);
CREATE INDEX idx_campaign_rules_reward_ref_id ON campaign_rules(reward_ref_id);

-- Add comments
COMMENT ON TABLE campaigns IS 'Campaign definitions for gamification system';
COMMENT ON TABLE campaign_rules IS 'Rules and rewards for each campaign';

COMMENT ON COLUMN campaigns.type IS 'Type of campaign: REWARD, POINTS, TOKENS, MIXED';
COMMENT ON COLUMN campaigns.show_on_app IS 'Whether campaign appears in mobile app';
COMMENT ON COLUMN campaigns.position IS 'Display order in app (lower number = higher priority)';

COMMENT ON COLUMN campaign_rules.rule_type IS 'Rule condition type: TIER, SPEND, PRODUCT, CATEGORY, DAY, LOCATION';
COMMENT ON COLUMN campaign_rules.condition_value IS 'Value for the rule condition (tier name, amount, product ID, etc.)';
COMMENT ON COLUMN campaign_rules.reward_type IS 'Type of reward: POINTS, TOKENS, REWARD';
COMMENT ON COLUMN campaign_rules.reward_value IS 'Reward amount or multiplier';
COMMENT ON COLUMN campaign_rules.reward_subtype IS 'Subtype of reward: MULTIPLIER, SPIN, BONUS, PHYSICAL';
COMMENT ON COLUMN campaign_rules.reward_ref_id IS 'Reference to rewards table when reward_type = REWARD';
