import { createClientComponentClient } from '@supabase/auth-helpers-nextjs' import { PricingFeature } from '@/lib/types/pricing-benefits' import { PricingTier, PRICING_TIERS } from '@/lib/stripe' const supabase = createClientComponentClient() // Simple function to check if user can use a feature (no complex service needed) export async function canUseFeature(user_id: string, feature: PricingFeature, currentTier: PricingTier): Promise { switch (feature) { case PricingFeature.CUSTOM_TEMPLATES: return currentTier.canCreateCustomTemplates case PricingFeature.PRIORITY_SUPPORT: return currentTier.hasPrioritySupport case PricingFeature.ADVANCED_EXPORTS: return currentTier.hasAdvancedExports case PricingFeature.COLLABORATION: return currentTier.hasCollaboration case PricingFeature.API_ACCESS: return currentTier.hasAPIAccess case PricingFeature.SMART_PROMPTS: // Check usage against limit const { data: usage } = await supabase .rpc('get_or_create_current_usage', { p_user_id: user_id }) if (!usage) return true // First time user return usage.smart_prompts_used < currentTier.maxSmartPrompts case PricingFeature.FAST_PROMPTS: if (currentTier.hasUnlimitedFastPrompts) return true const { data: fastUsage } = await supabase .rpc('get_or_create_current_usage', { p_user_id: user_id }) if (!fastUsage) return true return fastUsage.fast_prompts_used < currentTier.maxFastPrompts case PricingFeature.STORAGE: const { data: storageUsage } = await supabase .rpc('get_or_create_current_usage', { p_user_id: user_id }) if (!storageUsage) return true return storageUsage.storage_used_gb < currentTier.maxStorageGB default: return false } } // Simple function to increment usage export async function incrementUsage(user_id: string, feature_type: string, amount: number = 1): Promise { try { const response = await fetch('/api/usage/increment', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ user_id, feature_type, amount }) }) return response.ok } catch (error) { console.error('Error incrementing usage:', error) return false } } // Simple function to get current usage export async function getCurrentUsage(user_id: string) { try { const { data, error } = await supabase .rpc('get_or_create_current_usage', { p_user_id: user_id }) if (error) throw error return data } catch (error) { console.error('Error getting usage:', error) return null } }