-- White-Label Branding System Migration
-- Allows paying servers to customize bot identity and get custom handles

-- Server branding configuration
CREATE TABLE IF NOT EXISTS server_branding (
  id SERIAL PRIMARY KEY,
  guild_id VARCHAR(32) UNIQUE NOT NULL,
  
  -- Custom Identity
  custom_bot_name VARCHAR(80),
  custom_bot_avatar_url VARCHAR(512),
  custom_footer_text VARCHAR(200),
  custom_embed_color VARCHAR(10),
  
  -- Custom Handle (aethex.dev/{handle})
  custom_handle VARCHAR(50) UNIQUE,
  handle_verified BOOLEAN DEFAULT false,
  
  -- Landing Page Content
  landing_title VARCHAR(100),
  landing_description TEXT,
  landing_banner_url VARCHAR(512),
  landing_invite_url VARCHAR(200),
  landing_website_url VARCHAR(200),
  landing_social_links JSONB DEFAULT '{}',
  landing_features JSONB DEFAULT '[]',
  landing_theme VARCHAR(20) DEFAULT 'default',
  
  -- Branding Tier
  tier VARCHAR(20) DEFAULT 'free', -- free, basic, pro, enterprise
  branding_enabled BOOLEAN DEFAULT false,
  
  -- Subscription
  subscription_id VARCHAR(100),
  subscription_status VARCHAR(20),
  subscription_started_at TIMESTAMP WITH TIME ZONE,
  subscription_expires_at TIMESTAMP WITH TIME ZONE,
  
  -- Metadata
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  created_by VARCHAR(32)
);

-- Indexes for fast lookups
CREATE INDEX IF NOT EXISTS idx_branding_handle ON server_branding(custom_handle) WHERE custom_handle IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_branding_tier ON server_branding(tier);
CREATE INDEX IF NOT EXISTS idx_branding_enabled ON server_branding(branding_enabled);

-- Branding analytics (track custom landing page visits)
CREATE TABLE IF NOT EXISTS branding_analytics (
  id SERIAL PRIMARY KEY,
  guild_id VARCHAR(32) NOT NULL,
  event_type VARCHAR(50) NOT NULL, -- page_view, invite_click, social_click
  referrer VARCHAR(512),
  user_agent VARCHAR(512),
  ip_hash VARCHAR(64), -- hashed for privacy
  metadata JSONB DEFAULT '{}',
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_branding_analytics_guild ON branding_analytics(guild_id);
CREATE INDEX IF NOT EXISTS idx_branding_analytics_date ON branding_analytics(created_at);

-- Reserved handles (prevent abuse)
CREATE TABLE IF NOT EXISTS reserved_handles (
  id SERIAL PRIMARY KEY,
  handle VARCHAR(50) UNIQUE NOT NULL,
  reason VARCHAR(200),
  reserved_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Insert reserved handles
INSERT INTO reserved_handles (handle, reason) VALUES
  ('admin', 'System reserved'),
  ('api', 'System reserved'),
  ('dashboard', 'System reserved'),
  ('login', 'System reserved'),
  ('auth', 'System reserved'),
  ('oauth', 'System reserved'),
  ('federation', 'System reserved'),
  ('pricing', 'System reserved'),
  ('features', 'System reserved'),
  ('commands', 'System reserved'),
  ('support', 'System reserved'),
  ('help', 'System reserved'),
  ('aethex', 'Brand reserved'),
  ('warden', 'Brand reserved'),
  ('official', 'Brand reserved'),
  ('staff', 'Brand reserved'),
  ('mod', 'Brand reserved'),
  ('moderator', 'Brand reserved')
ON CONFLICT (handle) DO NOTHING;

-- White-label pricing tiers reference:
-- Basic ($15/mo): Custom bot name, footer, embed color
-- Pro ($35/mo): + Custom avatar, custom handle, landing page
-- Enterprise ($75/mo): + Analytics, priority support, custom domain support

COMMENT ON TABLE server_branding IS 'White-label branding configuration for servers';
COMMENT ON COLUMN server_branding.custom_handle IS 'Custom URL handle at aethex.dev/{handle}';
COMMENT ON COLUMN server_branding.tier IS 'Branding tier: free, basic, pro, enterprise';
