-- Migration 006: Premium .AETHEX Monetization
-- Adds subscription tiers, blockchain domains, marketplace, and analytics

-- NOTE: premium_tier is stored in user_profiles table, not auth.users

-- Premium subscriptions table
CREATE TABLE IF NOT EXISTS premium_subscriptions (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id VARCHAR NOT NULL ,
  tier VARCHAR(20) NOT NULL, -- free, premium, enterprise
  status VARCHAR(20) DEFAULT 'active', -- active, cancelled, expired, suspended
  stripe_subscription_id VARCHAR(100),
  stripe_customer_id VARCHAR(100),
  current_period_start TIMESTAMP DEFAULT NOW(),
  current_period_end TIMESTAMP,
  cancel_at_period_end BOOLEAN DEFAULT false,
  cancelled_at TIMESTAMP,
  created_at TIMESTAMP DEFAULT NOW(),
  updated_at TIMESTAMP DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_premium_subscriptions_user ON premium_subscriptions(user_id);
CREATE INDEX IF NOT EXISTS idx_premium_subscriptions_stripe ON premium_subscriptions(stripe_subscription_id);
CREATE INDEX IF NOT EXISTS idx_premium_subscriptions_status ON premium_subscriptions(user_id, status);

-- Blockchain domains table
CREATE TABLE IF NOT EXISTS blockchain_domains (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  domain VARCHAR(100) NOT NULL UNIQUE, -- e.g., "anderson.aethex"
  owner_user_id VARCHAR NOT NULL ,
  nft_token_id VARCHAR(100), -- Token ID from Freename contract
  wallet_address VARCHAR(100), -- Owner's wallet address
  verified BOOLEAN DEFAULT false,
  verification_signature TEXT,
  expires_at TIMESTAMP,
  auto_renew BOOLEAN DEFAULT true,
  renewal_price_usd DECIMAL(10, 2) DEFAULT 100.00,
  marketplace_listed BOOLEAN DEFAULT false,
  marketplace_price_usd DECIMAL(10, 2),
  created_at TIMESTAMP DEFAULT NOW(),
  updated_at TIMESTAMP DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_blockchain_domains_owner ON blockchain_domains(owner_user_id);
CREATE INDEX IF NOT EXISTS idx_blockchain_domains_marketplace ON blockchain_domains(marketplace_listed, marketplace_price_usd);
CREATE INDEX IF NOT EXISTS idx_blockchain_domains_domain ON blockchain_domains(domain);

-- Domain transfers table
CREATE TABLE IF NOT EXISTS domain_transfers (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  domain_id UUID NOT NULL REFERENCES blockchain_domains(id),
  from_user_id VARCHAR ,
  to_user_id VARCHAR ,
  transfer_type VARCHAR(20), -- sale, gift, transfer
  price_usd DECIMAL(10, 2),
  transaction_hash VARCHAR(100), -- Blockchain tx hash
  status VARCHAR(20) DEFAULT 'pending', -- pending, completed, failed
  created_at TIMESTAMP DEFAULT NOW(),
  completed_at TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_domain_transfers_domain ON domain_transfers(domain_id);
CREATE INDEX IF NOT EXISTS idx_domain_transfers_status ON domain_transfers(status, created_at);

-- Enterprise accounts table
CREATE TABLE IF NOT EXISTS enterprise_accounts (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  organization_name VARCHAR(200) NOT NULL,
  owner_user_id VARCHAR NOT NULL ,
  custom_domain VARCHAR(200), -- e.g., chat.yourgame.com
  custom_domain_verified BOOLEAN DEFAULT false,
  dns_txt_record VARCHAR(100), -- For domain verification
  white_label_enabled BOOLEAN DEFAULT true,
  custom_branding JSONB, -- {logo, primaryColor, secondaryColor, etc.}
  max_users INTEGER DEFAULT 100,
  current_users INTEGER DEFAULT 0,
  sla_tier VARCHAR(20) DEFAULT 'standard', -- standard, premium, enterprise
  dedicated_infrastructure BOOLEAN DEFAULT false,
  subscription_id UUID REFERENCES premium_subscriptions(id),
  created_at TIMESTAMP DEFAULT NOW(),
  updated_at TIMESTAMP DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_enterprise_accounts_owner ON enterprise_accounts(owner_user_id);
CREATE INDEX IF NOT EXISTS idx_enterprise_accounts_subscription ON enterprise_accounts(subscription_id);

-- Enterprise team members table
CREATE TABLE IF NOT EXISTS enterprise_team_members (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  enterprise_id UUID NOT NULL REFERENCES enterprise_accounts(id) ON DELETE CASCADE,
  user_id VARCHAR NOT NULL ,
  role VARCHAR(20) DEFAULT 'member', -- admin, member
  joined_at TIMESTAMP DEFAULT NOW(),
  UNIQUE(enterprise_id, user_id)
);

CREATE INDEX IF NOT EXISTS idx_enterprise_team_members_enterprise ON enterprise_team_members(enterprise_id);
CREATE INDEX IF NOT EXISTS idx_enterprise_team_members_user ON enterprise_team_members(user_id);

-- Usage analytics table
CREATE TABLE IF NOT EXISTS usage_analytics (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id VARCHAR NOT NULL ,
  date DATE NOT NULL,
  messages_sent INTEGER DEFAULT 0,
  messages_received INTEGER DEFAULT 0,
  voice_minutes INTEGER DEFAULT 0,
  video_minutes INTEGER DEFAULT 0,
  storage_used_mb INTEGER DEFAULT 0,
  active_friends INTEGER DEFAULT 0,
  games_played INTEGER DEFAULT 0,
  created_at TIMESTAMP DEFAULT NOW(),
  UNIQUE(user_id, date)
);

CREATE INDEX IF NOT EXISTS idx_usage_analytics_user_date ON usage_analytics(user_id, date DESC);

-- Feature limits table (for tier-based restrictions)
CREATE TABLE IF NOT EXISTS feature_limits (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  tier VARCHAR(20) NOT NULL UNIQUE, -- free, premium, enterprise
  max_friends INTEGER DEFAULT -1, -- -1 = unlimited
  max_storage_gb INTEGER DEFAULT 1,
  voice_calls_enabled BOOLEAN DEFAULT true,
  video_calls_enabled BOOLEAN DEFAULT false,
  max_video_quality VARCHAR(20) DEFAULT '480p', -- 480p, 720p, 1080p, 4k
  custom_branding BOOLEAN DEFAULT false,
  analytics_enabled BOOLEAN DEFAULT false,
  priority_support BOOLEAN DEFAULT false,
  white_label BOOLEAN DEFAULT false,
  dedicated_infrastructure BOOLEAN DEFAULT false,
  created_at TIMESTAMP DEFAULT NOW()
);

-- Insert default feature limits
INSERT INTO feature_limits (tier, max_friends, max_storage_gb, voice_calls_enabled, video_calls_enabled, max_video_quality, custom_branding, analytics_enabled, priority_support, white_label, dedicated_infrastructure)
VALUES 
  ('free', 5, 0, false, false, null, false, false, false, false, false),
  ('premium', -1, 10, true, true, '1080p', true, true, true, false, false),
  ('enterprise', -1, -1, true, true, '4k', true, true, true, true, true)
ON CONFLICT (tier) DO NOTHING;

-- Payment transactions table (for audit trail)
CREATE TABLE IF NOT EXISTS payment_transactions (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id VARCHAR NOT NULL ,
  transaction_type VARCHAR(50), -- subscription, domain_purchase, domain_sale, etc.
  amount_usd DECIMAL(10, 2) NOT NULL,
  currency VARCHAR(3) DEFAULT 'usd',
  stripe_payment_intent_id VARCHAR(100),
  stripe_invoice_id VARCHAR(100),
  status VARCHAR(20) DEFAULT 'pending', -- pending, succeeded, failed, refunded
  metadata JSONB,
  created_at TIMESTAMP DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_payment_transactions_user ON payment_transactions(user_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_payment_transactions_stripe ON payment_transactions(stripe_payment_intent_id);
