import { getAuth } from './auth'

export type UserPlan = 'free' | 'pro' | 'business' | 'enterprise'

export const PLAN_LIMITS = {
    free: {
        maxMonitors: 3,
        minFrequency: 60, // minutes
        features: ['email_alerts', 'basic_noise_filtering'],
    },
    pro: {
        maxMonitors: 20,
        minFrequency: 5,
        features: ['email_alerts', 'slack_integration', 'webhook_integration', 'keyword_alerts', 'smart_noise_filtering', 'audit_export'],
    },
    business: {
        maxMonitors: 100,
        minFrequency: 1,
        features: ['email_alerts', 'slack_integration', 'webhook_integration', 'keyword_alerts', 'smart_noise_filtering', 'audit_export', 'api_access', 'team_members'],
    },
    enterprise: {
        maxMonitors: Infinity,
        minFrequency: 1,
        features: ['email_alerts', 'slack_integration', 'webhook_integration', 'keyword_alerts', 'smart_noise_filtering', 'audit_export', 'api_access', 'team_members', 'custom_integrations', 'sla'],
    },
} as const

export function usePlan() {
    const auth = getAuth()
    const plan = (auth?.user?.plan as UserPlan) || 'free'
    const limits = PLAN_LIMITS[plan] || PLAN_LIMITS.free

    return {
        plan,
        limits,
        canUseSlack: limits.features.includes('slack_integration' as any),
        canUseWebhook: limits.features.includes('webhook_integration' as any),
        canUseKeywords: limits.features.includes('keyword_alerts' as any),
        canUseSmartNoise: limits.features.includes('smart_noise_filtering' as any),
        maxMonitors: limits.maxMonitors,
        minFrequency: limits.minFrequency,
    }
}
