/**
 * Seed Pricing Configuration
 *
 * Populates the pricing configuration MongoDB collections with default categories,
 * items, rules, discounts, taxes, and addons. Uses $setOnInsert via bulkWrite
 * upsert so existing entries are never overwritten.
 *
 * Called from src/backend/src/index.ts after database connection.
 */

import { getModels } from '../models';

// ============================================
// DEFAULT CATEGORIES
// ============================================

const DEFAULT_CATEGORIES = [
  { slug: 'modules', name: 'Modules', description: 'Platform modules included in the package', icon: 'Package', sortOrder: 1, selectionType: 'multiple', isRequired: false, isActive: true },
  { slug: 'ai_providers', name: 'AI Providers', description: 'AI service providers (OpenAI, Google, Anthropic, etc.)', icon: 'Cpu', sortOrder: 2, selectionType: 'multiple', isRequired: false, isActive: true },
  { slug: 'ai_models', name: 'AI Models', description: 'Specific AI models (nested under providers)', icon: 'Brain', sortOrder: 3, selectionType: 'multiple', isRequired: false, isActive: true },
  { slug: 'languages', name: 'Languages', description: 'Supported content languages', icon: 'Globe', sortOrder: 4, selectionType: 'multiple', isRequired: false, isActive: true },
  { slug: 'ai_features', name: 'AI Features', description: 'AI-powered features (image gen, voice, etc.)', icon: 'Sparkles', sortOrder: 5, selectionType: 'multiple', isRequired: false, isActive: true },
  { slug: 'usage_limits', name: 'Usage Limits', description: 'Quantitative limits for various platform resources', icon: 'Sliders', sortOrder: 6, selectionType: 'quantity', isRequired: false, isActive: true },
  { slug: 'storage', name: 'Storage & Infrastructure', description: 'Storage, bandwidth, and infrastructure', icon: 'HardDrive', sortOrder: 7, selectionType: 'single', isRequired: false, isActive: true },
  { slug: 'users', name: 'User Seats', description: 'Number of user seats', icon: 'Users', sortOrder: 8, selectionType: 'single', isRequired: true, isActive: true },
  { slug: 'projects', name: 'Projects', description: 'Number of projects/workspaces', icon: 'FolderOpen', sortOrder: 9, selectionType: 'single', isRequired: false, isActive: true },
  { slug: 'workflow_automation', name: 'Workflow Automation', description: 'Automation features and integrations', icon: 'Workflow', sortOrder: 10, selectionType: 'multiple', isRequired: false, isActive: true },
  { slug: 'image_generation', name: 'Image Generation', description: 'AI image generation capabilities', icon: 'ImageIcon', sortOrder: 11, selectionType: 'multiple', isRequired: false, isActive: true },
  { slug: 'token_limits', name: 'Token Limits', description: 'Monthly AI token usage limits', icon: 'Coins', sortOrder: 12, selectionType: 'single', isRequired: false, isActive: true },
  { slug: 'api_limits', name: 'API Limits', description: 'API rate limits and access tiers', icon: 'Code', sortOrder: 13, selectionType: 'single', isRequired: false, isActive: true },
];

// ============================================
// DEFAULT ITEMS (mapped from existing module/limit structure)
// ============================================

const DEFAULT_ITEMS = [
  // ── Modules ──
  { categorySlug: 'modules', slug: 'blog_module', name: 'Blog Content OS', description: 'AI-powered blog content creation and management', icon: 'FileText', pricingMode: 'flat', basePrice: 500, monthlyPrice: 500, quarterlyPrice: 1400, halfYearlyPrice: 2700, yearlyPrice: 5000, lifetimePrice: 25000, usageLimitKey: 'blogs', defaultLimit: 50, sortOrder: 1, isActive: true },
  { categorySlug: 'modules', slug: 'case_studies_module', name: 'Case Studies', description: 'Create and manage compelling case studies', icon: 'BookOpen', pricingMode: 'flat', basePrice: 300, monthlyPrice: 300, quarterlyPrice: 850, halfYearlyPrice: 1600, yearlyPrice: 3000, lifetimePrice: 15000, usageLimitKey: 'caseStudies', defaultLimit: 20, sortOrder: 2, isActive: true },
  { categorySlug: 'modules', slug: 'testimonials_module', name: 'Testimonials', description: 'Collect and showcase customer testimonials', icon: 'MessageSquare', pricingMode: 'flat', basePrice: 200, monthlyPrice: 200, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 2000, lifetimePrice: 10000, usageLimitKey: 'testimonials', defaultLimit: 30, sortOrder: 3, isActive: true },
  { categorySlug: 'modules', slug: 'seo_module', name: 'SEO Optimizer', description: 'AI-driven SEO analysis and optimization', icon: 'Search', pricingMode: 'flat', basePrice: 400, monthlyPrice: 400, quarterlyPrice: 1100, halfYearlyPrice: 2200, yearlyPrice: 4000, lifetimePrice: 20000, usageLimitKey: 'seoPages', defaultLimit: 100, sortOrder: 4, isActive: true },
  { categorySlug: 'modules', slug: 'landing_pages_module', name: 'Landing Pages', description: 'Create high-converting landing pages', icon: 'Layout', pricingMode: 'flat', basePrice: 600, monthlyPrice: 600, quarterlyPrice: 1700, halfYearlyPrice: 3300, yearlyPrice: 6000, lifetimePrice: 30000, usageLimitKey: 'landingPages', defaultLimit: 25, sortOrder: 5, isActive: true },
  { categorySlug: 'modules', slug: 'email_templates_module', name: 'Email Templates', description: 'AI-powered email template generation', icon: 'Mail', pricingMode: 'flat', basePrice: 200, monthlyPrice: 200, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 2000, lifetimePrice: 10000, usageLimitKey: 'emailTemplates', defaultLimit: 50, sortOrder: 6, isActive: true },
  { categorySlug: 'modules', slug: 'sales_scripts_module', name: 'Sales Scripts', description: 'Generate persuasive sales scripts', icon: 'ScrollText', pricingMode: 'flat', basePrice: 300, monthlyPrice: 300, quarterlyPrice: 850, halfYearlyPrice: 1600, yearlyPrice: 3000, lifetimePrice: 15000, usageLimitKey: 'salesScripts', defaultLimit: 30, sortOrder: 7, isActive: true },
  { categorySlug: 'modules', slug: 'sales_collateral_module', name: 'Sales Collateral', description: 'Professional sales collateral creation', icon: 'Presentation', pricingMode: 'flat', basePrice: 250, monthlyPrice: 250, quarterlyPrice: 700, halfYearlyPrice: 1400, yearlyPrice: 2500, lifetimePrice: 12500, usageLimitKey: 'salesCollateral', defaultLimit: 20, sortOrder: 8, isActive: true },
  { categorySlug: 'modules', slug: 'video_content_module', name: 'Video Content', description: 'Script and storyboard video content', icon: 'Video', pricingMode: 'flat', basePrice: 400, monthlyPrice: 400, quarterlyPrice: 1100, halfYearlyPrice: 2200, yearlyPrice: 4000, lifetimePrice: 20000, usageLimitKey: 'videoContent', defaultLimit: 10, sortOrder: 9, isActive: true },
  { categorySlug: 'modules', slug: 'website_planner_module', name: 'Website Planner', description: 'Plan and organize website structure', icon: 'Globe', pricingMode: 'flat', basePrice: 300, monthlyPrice: 300, quarterlyPrice: 850, halfYearlyPrice: 1600, yearlyPrice: 3000, lifetimePrice: 15000, usageLimitKey: 'websitePlanners', defaultLimit: 5, sortOrder: 10, isActive: true },
  { categorySlug: 'modules', slug: 'books_module', name: 'Books', description: 'AI-assisted book writing and publishing', icon: 'BookOpen', pricingMode: 'flat', basePrice: 500, monthlyPrice: 500, quarterlyPrice: 1400, halfYearlyPrice: 2700, yearlyPrice: 5000, lifetimePrice: 25000, usageLimitKey: 'books', defaultLimit: 2, sortOrder: 11, isActive: true },
  { categorySlug: 'modules', slug: 'courses_module', name: 'Courses', description: 'Create and sell online courses', icon: 'GraduationCap', pricingMode: 'flat', basePrice: 400, monthlyPrice: 400, quarterlyPrice: 1100, halfYearlyPrice: 2200, yearlyPrice: 4000, lifetimePrice: 20000, usageLimitKey: 'courses', defaultLimit: 5, sortOrder: 12, isActive: true },
  { categorySlug: 'modules', slug: 'loyalty_programme_module', name: 'Loyalty Programme', description: 'Design and manage loyalty programmes', icon: 'Award', pricingMode: 'flat', basePrice: 300, monthlyPrice: 300, quarterlyPrice: 850, halfYearlyPrice: 1600, yearlyPrice: 3000, lifetimePrice: 15000, usageLimitKey: 'loyaltyProgrammes', defaultLimit: 3, sortOrder: 13, isActive: true },
  { categorySlug: 'modules', slug: 'membership_plans_module', name: 'Membership Plans', description: 'Create membership tiers and plans', icon: 'CreditCard', pricingMode: 'flat', basePrice: 350, monthlyPrice: 350, quarterlyPrice: 1000, halfYearlyPrice: 1900, yearlyPrice: 3500, lifetimePrice: 17500, usageLimitKey: 'membershipPlans', defaultLimit: 5, sortOrder: 14, isActive: true },
  { categorySlug: 'modules', slug: 'referral_programme_module', name: 'Referral Programme', description: 'Build referral programmes for growth', icon: 'UserPlus', pricingMode: 'flat', basePrice: 200, monthlyPrice: 200, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 2000, lifetimePrice: 10000, usageLimitKey: 'referralProgrammes', defaultLimit: 3, sortOrder: 15, isActive: true },
  { categorySlug: 'modules', slug: 'campaigns_module', name: 'Ad Campaigns', description: 'Plan and execute advertising campaigns', icon: 'Megaphone', pricingMode: 'flat', basePrice: 500, monthlyPrice: 500, quarterlyPrice: 1400, halfYearlyPrice: 2700, yearlyPrice: 5000, lifetimePrice: 25000, usageLimitKey: 'campaigns', defaultLimit: 20, sortOrder: 16, isActive: true },
  { categorySlug: 'modules', slug: 'pr_campaigns_module', name: 'PR Campaigns', description: 'Manage public relations campaigns', icon: 'Newspaper', pricingMode: 'flat', basePrice: 400, monthlyPrice: 400, quarterlyPrice: 1100, halfYearlyPrice: 2200, yearlyPrice: 4000, lifetimePrice: 20000, usageLimitKey: 'prCampaigns', defaultLimit: 10, sortOrder: 17, isActive: true },
  { categorySlug: 'modules', slug: 'gmb_module', name: 'GMB Locations', description: 'Google My Business management', icon: 'MapPin', pricingMode: 'flat', basePrice: 300, monthlyPrice: 300, quarterlyPrice: 850, halfYearlyPrice: 1600, yearlyPrice: 3000, lifetimePrice: 15000, usageLimitKey: 'gmbLocations', defaultLimit: 10, sortOrder: 18, isActive: true },
  { categorySlug: 'modules', slug: 'influencer_module', name: 'Influencer Marketing', description: 'Manage influencer partnerships', icon: 'Star', pricingMode: 'flat', basePrice: 350, monthlyPrice: 350, quarterlyPrice: 1000, halfYearlyPrice: 1900, yearlyPrice: 3500, lifetimePrice: 17500, usageLimitKey: 'influencers', defaultLimit: 15, sortOrder: 19, isActive: true },
  { categorySlug: 'modules', slug: 'events_module', name: 'Events', description: 'Plan and manage events', icon: 'CalendarDays', pricingMode: 'flat', basePrice: 300, monthlyPrice: 300, quarterlyPrice: 850, halfYearlyPrice: 1600, yearlyPrice: 3000, lifetimePrice: 15000, usageLimitKey: 'events', defaultLimit: 10, sortOrder: 20, isActive: true },
  { categorySlug: 'modules', slug: 'pitch_decks_module', name: 'Pitch Decks', description: 'Create investor pitch decks', icon: 'Presentation', pricingMode: 'flat', basePrice: 400, monthlyPrice: 400, quarterlyPrice: 1100, halfYearlyPrice: 2200, yearlyPrice: 4000, lifetimePrice: 20000, usageLimitKey: 'pitchDecks', defaultLimit: 5, sortOrder: 21, isActive: true },
  { categorySlug: 'modules', slug: 'presentations_module', name: 'Presentations & Pitches', description: 'AI-powered presentations for company profiles, products, investor pitches, and events with export to PPTX/PDF', icon: 'Presentation', pricingMode: 'flat', basePrice: 400, monthlyPrice: 400, quarterlyPrice: 1100, halfYearlyPrice: 2200, yearlyPrice: 4000, lifetimePrice: 20000, usageLimitKey: 'pitchDecks', defaultLimit: 5, sortOrder: 21, isActive: true },
  { categorySlug: 'modules', slug: 'financial_models_module', name: 'Financial Models', description: 'Build financial models and projections', icon: 'TrendingUp', pricingMode: 'flat', basePrice: 500, monthlyPrice: 500, quarterlyPrice: 1400, halfYearlyPrice: 2700, yearlyPrice: 5000, lifetimePrice: 25000, usageLimitKey: 'financialModels', defaultLimit: 3, sortOrder: 22, isActive: false },
  { categorySlug: 'modules', slug: 'sops_module', name: 'SOPs', description: 'Standard Operating Procedures builder', icon: 'ClipboardList', pricingMode: 'flat', basePrice: 200, monthlyPrice: 200, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 2000, lifetimePrice: 10000, usageLimitKey: 'sops', defaultLimit: 20, sortOrder: 23, isActive: true },
  { categorySlug: 'modules', slug: 'job_postings_module', name: 'Job Postings', description: 'Create and manage job postings', icon: 'Briefcase', pricingMode: 'flat', basePrice: 150, monthlyPrice: 150, quarterlyPrice: 430, halfYearlyPrice: 800, yearlyPrice: 1500, lifetimePrice: 7500, usageLimitKey: 'jobPostings', defaultLimit: 10, sortOrder: 24, isActive: false },
  { categorySlug: 'modules', slug: 'legal_documents_module', name: 'Legal Documents', description: 'Generate legal documents and contracts', icon: 'Scale', pricingMode: 'flat', basePrice: 250, monthlyPrice: 250, quarterlyPrice: 700, halfYearlyPrice: 1400, yearlyPrice: 2500, lifetimePrice: 12500, usageLimitKey: 'legalDocuments', defaultLimit: 10, sortOrder: 25, isActive: true },
  { categorySlug: 'modules', slug: 'business_profile_module', name: 'Business Profile', description: 'Manage company profile, branding, and business identity', icon: 'Building2', pricingMode: 'flat', basePrice: 100, monthlyPrice: 100, quarterlyPrice: 280, halfYearlyPrice: 550, yearlyPrice: 1000, lifetimePrice: 5000, usageLimitKey: 'businessProfiles', defaultLimit: 1, sortOrder: 26, isActive: true },
  { categorySlug: 'modules', slug: 'newsletter_module', name: 'Newsletter', description: 'Create and send newsletters to subscribers', icon: 'MailOpen', pricingMode: 'flat', basePrice: 200, monthlyPrice: 200, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 2000, lifetimePrice: 10000, usageLimitKey: 'newsletters', defaultLimit: 50, sortOrder: 27, isActive: true },
  { categorySlug: 'modules', slug: 'faq_module', name: 'FAQ', description: 'Create and manage frequently asked questions', icon: 'HelpCircle', pricingMode: 'flat', basePrice: 100, monthlyPrice: 100, quarterlyPrice: 280, halfYearlyPrice: 550, yearlyPrice: 1000, lifetimePrice: 5000, usageLimitKey: 'faqs', defaultLimit: 50, sortOrder: 28, isActive: true },
  { categorySlug: 'modules', slug: 'hr_assets_module', name: 'HR Assets', description: 'HR documents, onboarding kits, and employee resources', icon: 'Users', pricingMode: 'flat', basePrice: 250, monthlyPrice: 250, quarterlyPrice: 700, halfYearlyPrice: 1400, yearlyPrice: 2500, lifetimePrice: 12500, usageLimitKey: 'hrAssets', defaultLimit: 20, sortOrder: 29, isActive: true },
  { categorySlug: 'modules', slug: 'stationery_module', name: 'Stationery', description: 'Design business cards, letterheads, and branded stationery', icon: 'PenTool', pricingMode: 'flat', basePrice: 150, monthlyPrice: 150, quarterlyPrice: 430, halfYearlyPrice: 800, yearlyPrice: 1500, lifetimePrice: 7500, usageLimitKey: 'stationery', defaultLimit: 20, sortOrder: 30, isActive: true },
  { categorySlug: 'modules', slug: 'intro_scripts_module', name: 'Intro Scripts', description: 'AI-powered introduction and elevator scripts', icon: 'Mic', pricingMode: 'flat', basePrice: 200, monthlyPrice: 200, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 2000, lifetimePrice: 10000, usageLimitKey: 'introScripts', defaultLimit: 30, sortOrder: 31, isActive: true },
  { categorySlug: 'modules', slug: 'social_media_module', name: 'Social Media', description: 'Manage social media accounts, posts, and analytics', icon: 'Share2', pricingMode: 'flat', basePrice: 350, monthlyPrice: 350, quarterlyPrice: 1000, halfYearlyPrice: 1900, yearlyPrice: 3500, lifetimePrice: 17500, usageLimitKey: 'socialMedia', defaultLimit: 50, sortOrder: 32, isActive: true },

  // ── Modules: Previously missing from Package Publishing ──
  // Brand group
  { categorySlug: 'modules', slug: 'brand_module', name: 'Brand Hub', description: 'Central hub for brand strategy, visual identity, assets, and stationery', icon: 'Palette', pricingMode: 'flat', basePrice: 100, monthlyPrice: 100, quarterlyPrice: 280, halfYearlyPrice: 550, yearlyPrice: 1000, lifetimePrice: 5000, usageLimitKey: 'brandStrategies', defaultLimit: 1, sortOrder: 33, isActive: true },
  { categorySlug: 'modules', slug: 'brand_strategy_module', name: 'Brand Strategy', description: 'Brand psychology, archetypes, positioning, messaging, and tone of voice', icon: 'Brain', pricingMode: 'flat', basePrice: 300, monthlyPrice: 300, quarterlyPrice: 850, halfYearlyPrice: 1600, yearlyPrice: 3000, lifetimePrice: 15000, usageLimitKey: 'brandStrategies', defaultLimit: 5, sortOrder: 34, isActive: true },
  { categorySlug: 'modules', slug: 'visual_identity_module', name: 'Visual Identity', description: 'Complete design system with colors, typography, spacing, and live previews', icon: 'Paintbrush', pricingMode: 'flat', basePrice: 250, monthlyPrice: 250, quarterlyPrice: 700, halfYearlyPrice: 1400, yearlyPrice: 2500, lifetimePrice: 12500, usageLimitKey: 'visualIdentities', defaultLimit: 5, sortOrder: 35, isActive: true },
  { categorySlug: 'modules', slug: 'brand_manual_module', name: 'Brand Manual', description: 'View and download complete brand guidelines document', icon: 'BookOpen', pricingMode: 'flat', basePrice: 200, monthlyPrice: 200, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 2000, lifetimePrice: 10000, usageLimitKey: 'brandManuals', defaultLimit: 3, sortOrder: 36, isActive: true },
  { categorySlug: 'modules', slug: 'brand_assets_module', name: 'Brand Assets', description: 'Logos, favicons, patterns, backgrounds', icon: 'Image', pricingMode: 'flat', basePrice: 200, monthlyPrice: 200, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 2000, lifetimePrice: 10000, usageLimitKey: 'brandAssets', defaultLimit: 50, sortOrder: 37, isActive: true },
  // Foundation
  { categorySlug: 'modules', slug: 'founders_module', name: 'Founders', description: 'Founder profiles with assets and social links', icon: 'Crown', pricingMode: 'flat', basePrice: 100, monthlyPrice: 100, quarterlyPrice: 280, halfYearlyPrice: 550, yearlyPrice: 1000, lifetimePrice: 5000, usageLimitKey: 'founders', defaultLimit: 50, sortOrder: 38, isActive: true },
  { categorySlug: 'modules', slug: 'employees_module', name: 'Employees', description: 'Team management with departments and levels', icon: 'Users2', pricingMode: 'flat', basePrice: 150, monthlyPrice: 150, quarterlyPrice: 430, halfYearlyPrice: 800, yearlyPrice: 1500, lifetimePrice: 7500, usageLimitKey: 'employees', defaultLimit: 50, sortOrder: 39, isActive: true },
  { categorySlug: 'modules', slug: 'products_module', name: 'Products', description: 'Product catalog with categories and marketing copy', icon: 'Package', pricingMode: 'flat', basePrice: 100, monthlyPrice: 100, quarterlyPrice: 280, halfYearlyPrice: 550, yearlyPrice: 1000, lifetimePrice: 5000, usageLimitKey: 'products', defaultLimit: 50, sortOrder: 40, isActive: true },
  { categorySlug: 'modules', slug: 'icp_module', name: 'ICPs & Personas', description: 'Ideal Customer Profiles and Buyer Personas', icon: 'Target', pricingMode: 'flat', basePrice: 100, monthlyPrice: 100, quarterlyPrice: 280, halfYearlyPrice: 550, yearlyPrice: 1000, lifetimePrice: 5000, usageLimitKey: 'icps', defaultLimit: 50, sortOrder: 41, isActive: true },
  { categorySlug: 'modules', slug: 'competitors_module', name: 'Competitors', description: 'Competitive analysis with SWOT', icon: 'Swords', pricingMode: 'flat', basePrice: 100, monthlyPrice: 100, quarterlyPrice: 280, halfYearlyPrice: 550, yearlyPrice: 1000, lifetimePrice: 5000, usageLimitKey: 'competitors', defaultLimit: 50, sortOrder: 42, isActive: true },
  // Sales
  { categorySlug: 'modules', slug: 'whatsapp_module', name: 'WhatsApp Nurturing', description: 'AI-powered WhatsApp lead nurturing sequences with strategic frameworks, smart content blocks, and campaign analytics', icon: 'MessageSquareText', pricingMode: 'flat', basePrice: 400, monthlyPrice: 400, quarterlyPrice: 1100, halfYearlyPrice: 2200, yearlyPrice: 4000, lifetimePrice: 20000, usageLimitKey: 'whatsappSequences', defaultLimit: 20, sortOrder: 43, isActive: true },
  // Marketing
  { categorySlug: 'modules', slug: 'guerrilla_module', name: 'Guerrilla Marketing', description: 'Unconventional and creative marketing tactics to generate buzz and maximize impact on a budget', icon: 'Flame', pricingMode: 'flat', basePrice: 300, monthlyPrice: 300, quarterlyPrice: 850, halfYearlyPrice: 1600, yearlyPrice: 3000, lifetimePrice: 15000, usageLimitKey: 'guerrillaCampaigns', defaultLimit: 20, sortOrder: 44, isActive: true },
  { categorySlug: 'modules', slug: 'interview_module', name: 'Interview & Media Prep', description: 'Prepare for interviews, press appearances, and media engagements with AI-powered coaching', icon: 'Podcast', pricingMode: 'flat', basePrice: 250, monthlyPrice: 250, quarterlyPrice: 700, halfYearlyPrice: 1400, yearlyPrice: 2500, lifetimePrice: 12500, usageLimitKey: 'interviewPreps', defaultLimit: 20, sortOrder: 45, isActive: true },
  { categorySlug: 'modules', slug: 'speaking_module', name: 'Speaking Engagements', description: 'AI-powered speech generator for 16+ speech types with smart context, stage instructions, and delivery guidance', icon: 'Mic2', pricingMode: 'flat', basePrice: 250, monthlyPrice: 250, quarterlyPrice: 700, halfYearlyPrice: 1400, yearlyPrice: 2500, lifetimePrice: 12500, usageLimitKey: 'speakingScripts', defaultLimit: 20, sortOrder: 46, isActive: true },
  { categorySlug: 'modules', slug: 'moat_module', name: 'MOAT Analysis', description: 'AI-powered competitive MOAT analysis with strategic action planning', icon: 'Shield', pricingMode: 'flat', basePrice: 350, monthlyPrice: 350, quarterlyPrice: 1000, halfYearlyPrice: 1900, yearlyPrice: 3500, lifetimePrice: 17500, usageLimitKey: 'moatAnalyses', defaultLimit: 10, sortOrder: 47, isActive: true },
  { categorySlug: 'modules', slug: 'marketing_calendar_module', name: 'Marketing Calendar & Timing', description: 'Plan, schedule, and optimise marketing activities with seasonal insights and budget tracking', icon: 'CalendarClock', pricingMode: 'flat', basePrice: 300, monthlyPrice: 300, quarterlyPrice: 850, halfYearlyPrice: 1600, yearlyPrice: 3000, lifetimePrice: 15000, usageLimitKey: 'marketingCalendars', defaultLimit: 20, sortOrder: 48, isActive: true },
  { categorySlug: 'modules', slug: 'marketing_channels_module', name: 'Marketing Channels Map', description: 'Map, analyse, and optimise marketing channels across the customer funnel', icon: 'GitBranch', pricingMode: 'flat', basePrice: 300, monthlyPrice: 300, quarterlyPrice: 850, halfYearlyPrice: 1600, yearlyPrice: 3000, lifetimePrice: 15000, usageLimitKey: 'marketingChannels', defaultLimit: 20, sortOrder: 49, isActive: true },
  { categorySlug: 'modules', slug: 'geo_optimization_module', name: 'AI Discoverability (GEO)', description: 'Generate GEO-optimised content for Quora, AI Overviews, People Also Ask, ChatGPT citations, and directory submissions', icon: 'Globe', pricingMode: 'flat', basePrice: 350, monthlyPrice: 350, quarterlyPrice: 1000, halfYearlyPrice: 1900, yearlyPrice: 3500, lifetimePrice: 17500, usageLimitKey: 'geoOptimizations', defaultLimit: 20, sortOrder: 50, isActive: true },
  // Publishing
  { categorySlug: 'modules', slug: 'magazine_module', name: 'Magazine & Sponsorship', description: 'Magazine articles, ads, issues, sponsorship options, deals, and asset management', icon: 'Newspaper', pricingMode: 'flat', basePrice: 400, monthlyPrice: 400, quarterlyPrice: 1100, halfYearlyPrice: 2200, yearlyPrice: 4000, lifetimePrice: 20000, usageLimitKey: 'magazineSponsorships', defaultLimit: 10, sortOrder: 51, isActive: true },
  // Sales Performance
  { categorySlug: 'modules', slug: 'commission_tracker_module', name: 'Commission Tracker', description: 'Track rep commissions, payouts, splits, and approval workflows', icon: 'DollarSign', pricingMode: 'flat', basePrice: 200, monthlyPrice: 200, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 2000, lifetimePrice: 10000, usageLimitKey: 'commissionTracking', defaultLimit: 50, sortOrder: 52, isActive: true },
  { categorySlug: 'modules', slug: 'sales_targets_module', name: 'Sales Targets & Proposals', description: 'Set quotas, track attainment, create proposals and quotes with approval workflows', icon: 'Target', pricingMode: 'flat', basePrice: 300, monthlyPrice: 300, quarterlyPrice: 850, halfYearlyPrice: 1600, yearlyPrice: 3000, lifetimePrice: 15000, usageLimitKey: 'salesTargets', defaultLimit: 20, sortOrder: 53, isActive: true },
  { categorySlug: 'modules', slug: 'sales_playbooks_module', name: 'Sales Playbooks', description: 'Structured selling guides with stages, objection handling, and best practices', icon: 'BookOpen', pricingMode: 'flat', basePrice: 250, monthlyPrice: 250, quarterlyPrice: 700, halfYearlyPrice: 1400, yearlyPrice: 2500, lifetimePrice: 12500, usageLimitKey: 'salesPlaybooks', defaultLimit: 10, sortOrder: 54, isActive: true },

  // ── AI Providers ──
  { categorySlug: 'ai_providers', slug: 'anthropic_provider', name: 'Anthropic', description: 'Anthropic Claude models', icon: 'Cpu', pricingMode: 'flat', basePrice: 0, monthlyPrice: 0, quarterlyPrice: 0, halfYearlyPrice: 0, yearlyPrice: 0, lifetimePrice: 0, sortOrder: 1, isActive: true },
  { categorySlug: 'ai_providers', slug: 'zhipu_provider', name: 'Zhipu AI', description: 'Zhipu AI GLM models', icon: 'Cpu', pricingMode: 'flat', basePrice: 0, monthlyPrice: 0, quarterlyPrice: 0, halfYearlyPrice: 0, yearlyPrice: 0, lifetimePrice: 0, sortOrder: 2, isActive: true },
  { categorySlug: 'ai_providers', slug: 'ollama_provider', name: 'Ollama', description: 'Ollama local models', icon: 'Cpu', pricingMode: 'flat', basePrice: 0, monthlyPrice: 0, quarterlyPrice: 0, halfYearlyPrice: 0, yearlyPrice: 0, lifetimePrice: 0, sortOrder: 3, isActive: true },
  { categorySlug: 'ai_providers', slug: 'openai_provider', name: 'OpenAI', description: 'OpenAI GPT models', icon: 'Cpu', pricingMode: 'flat', basePrice: 0, monthlyPrice: 0, quarterlyPrice: 0, halfYearlyPrice: 0, yearlyPrice: 0, lifetimePrice: 0, sortOrder: 4, isActive: true },

  // ── AI Models (nested under providers) ──
  // Anthropic
  { categorySlug: 'ai_models', slug: 'claude_sonnet_4_6', name: 'Claude Sonnet 4.6', description: 'Anthropic Claude Sonnet 4.6 — balanced performance and intelligence', icon: 'Brain', parentItemSlug: 'anthropic_provider', pricingMode: 'per_unit', basePrice: 450, monthlyPrice: 450, quarterlyPrice: 1280, halfYearlyPrice: 2400, yearlyPrice: 4500, lifetimePrice: 22500, usageLimitKey: 'tokenLimits', defaultLimit: 800000, metadata: { tokens: '800K tokens/month' }, sortOrder: 1, isActive: true },
  // Zhipu AI
  { categorySlug: 'ai_models', slug: 'glm_4_plus', name: 'GLM-4-Plus', description: 'Zhipu AI GLM-4-Plus — advanced bilingual large language model', icon: 'Brain', parentItemSlug: 'zhipu_provider', pricingMode: 'per_unit', basePrice: 200, monthlyPrice: 200, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 2000, lifetimePrice: 10000, usageLimitKey: 'tokenLimits', defaultLimit: 400000, metadata: { tokens: '400K tokens/month' }, sortOrder: 2, isActive: true },
  // Ollama
  { categorySlug: 'ai_models', slug: 'glm_5_1', name: 'GLM-5.1', description: 'Ollama GLM-5.1 — latest GLM model via local inference', icon: 'Brain', parentItemSlug: 'ollama_provider', pricingMode: 'flat', basePrice: 0, monthlyPrice: 0, quarterlyPrice: 0, halfYearlyPrice: 0, yearlyPrice: 0, lifetimePrice: 0, usageLimitKey: 'tokenLimits', defaultLimit: 0, metadata: { tokens: 'Unlimited local tokens' }, sortOrder: 3, isActive: true },
  // OpenAI
  { categorySlug: 'ai_models', slug: 'gpt_4o', name: 'GPT-4o', description: 'OpenAI GPT-4o — high-intelligence multimodal model', icon: 'Brain', parentItemSlug: 'openai_provider', pricingMode: 'per_unit', basePrice: 500, monthlyPrice: 500, quarterlyPrice: 1400, halfYearlyPrice: 2700, yearlyPrice: 5000, lifetimePrice: 25000, usageLimitKey: 'tokenLimits', defaultLimit: 800000, metadata: { tokens: '800K tokens/month' }, sortOrder: 4, isActive: true },

  // ── Languages ──
  { categorySlug: 'languages', slug: 'lang_english', name: 'English', description: 'English language support', icon: 'Globe', pricingMode: 'flat', basePrice: 0, monthlyPrice: 0, quarterlyPrice: 0, halfYearlyPrice: 0, yearlyPrice: 0, lifetimePrice: 0, defaultLimit: 1, sortOrder: 1, isActive: true },
  { categorySlug: 'languages', slug: 'lang_hindi', name: 'Hindi', description: 'Hindi language support', icon: 'Globe', pricingMode: 'flat', basePrice: 200, monthlyPrice: 200, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 2000, lifetimePrice: 10000, defaultLimit: 1, sortOrder: 2, isActive: true },
  { categorySlug: 'languages', slug: 'lang_marathi', name: 'Marathi', description: 'Marathi language support', icon: 'Globe', pricingMode: 'flat', basePrice: 200, monthlyPrice: 200, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 2000, lifetimePrice: 10000, defaultLimit: 1, sortOrder: 3, isActive: true },
  { categorySlug: 'languages', slug: 'lang_spanish', name: 'Spanish', description: 'Spanish language support', icon: 'Globe', pricingMode: 'flat', basePrice: 200, monthlyPrice: 200, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 2000, lifetimePrice: 10000, defaultLimit: 1, sortOrder: 4, isActive: true },
  { categorySlug: 'languages', slug: 'lang_french', name: 'French', description: 'French language support', icon: 'Globe', pricingMode: 'flat', basePrice: 200, monthlyPrice: 200, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 2000, lifetimePrice: 10000, defaultLimit: 1, sortOrder: 5, isActive: true },
  { categorySlug: 'languages', slug: 'lang_german', name: 'German', description: 'German language support', icon: 'Globe', pricingMode: 'flat', basePrice: 200, monthlyPrice: 200, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 2000, lifetimePrice: 10000, defaultLimit: 1, sortOrder: 6, isActive: true },

  // ── AI Features ──
  { categorySlug: 'ai_features', slug: 'image_generation', name: 'Image Generation', description: 'Generate images from text prompts using AI', icon: 'ImageIcon', pricingMode: 'per_unit', basePrice: 300, monthlyPrice: 300, quarterlyPrice: 850, halfYearlyPrice: 1600, yearlyPrice: 3000, lifetimePrice: 15000, usageLimitKey: 'imageGeneration', defaultLimit: 100, sortOrder: 1, isActive: true },
  { categorySlug: 'ai_features', slug: 'api_access', name: 'API Access', description: 'Full REST API access for integrations', icon: 'Code', pricingMode: 'flat', basePrice: 499, monthlyPrice: 499, quarterlyPrice: 1400, halfYearlyPrice: 2700, yearlyPrice: 4990, lifetimePrice: 25000, usageLimitKey: 'apiCalls', defaultLimit: 10000, sortOrder: 2, isActive: true },
  { categorySlug: 'ai_features', slug: 'ai_chat', name: 'AI Chat', description: 'Conversational AI chat interface for support and engagement', icon: 'MessageCircle', pricingMode: 'flat', basePrice: 500, monthlyPrice: 500, quarterlyPrice: 1400, halfYearlyPrice: 2700, yearlyPrice: 5000, lifetimePrice: 25000, usageLimitKey: 'chatbotConversations', defaultLimit: 1000, sortOrder: 3, isActive: true },
  { categorySlug: 'ai_features', slug: 'ai_assistant', name: 'AI Assistant', description: 'AI-powered assistant for content creation and suggestions', icon: 'Sparkles', pricingMode: 'flat', basePrice: 400, monthlyPrice: 400, quarterlyPrice: 1100, halfYearlyPrice: 2200, yearlyPrice: 4000, lifetimePrice: 20000, usageLimitKey: 'aiAssistants', defaultLimit: 5, sortOrder: 4, isActive: true },
  { categorySlug: 'ai_features', slug: 'ocr', name: 'OCR', description: 'Optical Character Recognition for document scanning', icon: 'Scan', pricingMode: 'per_unit', basePrice: 100, monthlyPrice: 100, quarterlyPrice: 280, halfYearlyPrice: 550, yearlyPrice: 1000, lifetimePrice: 5000, usageLimitKey: 'ocrScans', defaultLimit: 500, sortOrder: 5, isActive: true },
  { categorySlug: 'ai_features', slug: 'translation', name: 'Translation', description: 'AI-powered multi-language content translation', icon: 'Globe', pricingMode: 'flat', basePrice: 200, monthlyPrice: 200, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 2000, lifetimePrice: 10000, usageLimitKey: 'translations', defaultLimit: 200, sortOrder: 6, isActive: true },
  { categorySlug: 'ai_features', slug: 'speech_to_text', name: 'Speech to Text', description: 'Convert audio recordings to text with AI transcription', icon: 'Mic', pricingMode: 'per_unit', basePrice: 200, monthlyPrice: 200, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 2000, lifetimePrice: 10000, usageLimitKey: 'speechToText', defaultLimit: 100, sortOrder: 7, isActive: true },
  { categorySlug: 'ai_features', slug: 'text_to_speech', name: 'Text to Speech', description: 'Convert text to natural-sounding AI speech', icon: 'Volume2', pricingMode: 'per_unit', basePrice: 200, monthlyPrice: 200, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 2000, lifetimePrice: 10000, usageLimitKey: 'textToSpeech', defaultLimit: 100, sortOrder: 8, isActive: true },
  { categorySlug: 'ai_features', slug: 'image_editing', name: 'Image Editing', description: 'AI-powered image editing and enhancement tools', icon: 'Palette', pricingMode: 'per_unit', basePrice: 250, monthlyPrice: 250, quarterlyPrice: 700, halfYearlyPrice: 1400, yearlyPrice: 2500, lifetimePrice: 12500, usageLimitKey: 'imageEdits', defaultLimit: 200, sortOrder: 9, isActive: true },
  { categorySlug: 'ai_features', slug: 'video_generation', name: 'Video Generation', description: 'Create videos from text prompts with AI', icon: 'Video', pricingMode: 'per_unit', basePrice: 500, monthlyPrice: 500, quarterlyPrice: 1400, halfYearlyPrice: 2700, yearlyPrice: 5000, lifetimePrice: 25000, usageLimitKey: 'videoGeneration', defaultLimit: 10, sortOrder: 10, isActive: true },
  { categorySlug: 'ai_features', slug: 'ai_agents', name: 'AI Agents', description: 'Autonomous AI agents for task automation and decision making', icon: 'Brain', pricingMode: 'flat', basePrice: 600, monthlyPrice: 600, quarterlyPrice: 1700, halfYearlyPrice: 3300, yearlyPrice: 6000, lifetimePrice: 30000, usageLimitKey: 'aiAgents', defaultLimit: 5, sortOrder: 11, isActive: true },
  { categorySlug: 'ai_features', slug: 'workflow_automation', name: 'Workflow Automation', description: 'Automate repetitive tasks with AI-powered workflows', icon: 'Workflow', pricingMode: 'flat', basePrice: 400, monthlyPrice: 400, quarterlyPrice: 1100, halfYearlyPrice: 2200, yearlyPrice: 4000, lifetimePrice: 20000, usageLimitKey: 'workflowAutomations', defaultLimit: 50, sortOrder: 12, isActive: true },
  { categorySlug: 'ai_features', slug: 'prompt_library', name: 'Prompt Library', description: 'Pre-built and customizable AI prompt templates', icon: 'BookOpen', pricingMode: 'flat', basePrice: 100, monthlyPrice: 100, quarterlyPrice: 280, halfYearlyPrice: 550, yearlyPrice: 1000, lifetimePrice: 5000, usageLimitKey: 'promptTemplates', defaultLimit: 100, sortOrder: 13, isActive: true },
  { categorySlug: 'ai_features', slug: 'knowledge_base', name: 'Knowledge Base', description: 'AI-powered knowledge base for organizing and retrieving information', icon: 'BookMarked', pricingMode: 'flat', basePrice: 300, monthlyPrice: 300, quarterlyPrice: 850, halfYearlyPrice: 1600, yearlyPrice: 3000, lifetimePrice: 15000, usageLimitKey: 'knowledgeBase', defaultLimit: 50, sortOrder: 14, isActive: true },
  { categorySlug: 'ai_features', slug: 'smart_search', name: 'Smart Search', description: 'AI-powered semantic search across all platform content', icon: 'Search', pricingMode: 'flat', basePrice: 200, monthlyPrice: 200, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 2000, lifetimePrice: 10000, usageLimitKey: 'smartSearchQueries', defaultLimit: 1000, sortOrder: 15, isActive: true },
  { categorySlug: 'ai_features', slug: 'premium_support', name: 'Premium Support', description: '24/7 priority support with dedicated account manager', icon: 'Headphones', pricingMode: 'flat', basePrice: 1499, monthlyPrice: 1499, quarterlyPrice: 4200, halfYearlyPrice: 8200, yearlyPrice: 14990, lifetimePrice: 75000, defaultLimit: 1, sortOrder: 16, isActive: true },
  { categorySlug: 'ai_features', slug: 'white_label_feature', name: 'White Label', description: 'Full white-label branding removal and custom branding', icon: 'Palette', pricingMode: 'flat', basePrice: 2999, monthlyPrice: 2999, quarterlyPrice: 8500, halfYearlyPrice: 16500, yearlyPrice: 29990, lifetimePrice: 150000, defaultLimit: 1, sortOrder: 17, isActive: true },
  { categorySlug: 'ai_features', slug: 'team_collaboration', name: 'Team Collaboration', description: 'Real-time team collaboration tools and shared workspaces', icon: 'Users', pricingMode: 'flat', basePrice: 399, monthlyPrice: 399, quarterlyPrice: 1100, halfYearlyPrice: 2200, yearlyPrice: 3990, lifetimePrice: 20000, usageLimitKey: 'teamSeats', defaultLimit: 10, sortOrder: 18, isActive: true },
  { categorySlug: 'ai_features', slug: 'custom_branding_feature', name: 'Custom Branding', description: 'Custom logos, colors, and brand identity throughout the platform', icon: 'Brush', pricingMode: 'flat', basePrice: 199, monthlyPrice: 199, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 1990, lifetimePrice: 10000, defaultLimit: 1, sortOrder: 19, isActive: true },
  { categorySlug: 'ai_features', slug: 'analytics_dashboard', name: 'Analytics Dashboard', description: 'Advanced analytics with custom reports and data visualization', icon: 'BarChart3', pricingMode: 'flat', basePrice: 599, monthlyPrice: 599, quarterlyPrice: 1700, halfYearlyPrice: 3300, yearlyPrice: 5990, lifetimePrice: 30000, usageLimitKey: 'analyticsReports', defaultLimit: 100, sortOrder: 20, isActive: true },
  { categorySlug: 'ai_features', slug: 'webhooks', name: 'Webhooks', description: 'Real-time event notifications via webhooks for external integrations', icon: 'Link', pricingMode: 'flat', basePrice: 199, monthlyPrice: 199, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 1990, lifetimePrice: 10000, usageLimitKey: 'webhookEndpoints', defaultLimit: 10, sortOrder: 21, isActive: true },
  { categorySlug: 'ai_features', slug: 'priority_processing', name: 'Priority Processing', description: 'Faster AI processing with dedicated compute resources', icon: 'Zap', pricingMode: 'flat', basePrice: 299, monthlyPrice: 299, quarterlyPrice: 850, halfYearlyPrice: 1650, yearlyPrice: 2990, lifetimePrice: 15000, defaultLimit: 1, sortOrder: 22, isActive: true },
  { categorySlug: 'ai_features', slug: 'role_based_access', name: 'Role-Based Access', description: 'Granular role and permission management for team members', icon: 'Shield', pricingMode: 'flat', basePrice: 199, monthlyPrice: 199, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 1990, lifetimePrice: 10000, usageLimitKey: 'customRoles', defaultLimit: 10, sortOrder: 23, isActive: true },
  { categorySlug: 'ai_features', slug: 'audit_logs', name: 'Audit Logs', description: 'Comprehensive audit trail for compliance and security monitoring', icon: 'FileText', pricingMode: 'flat', basePrice: 149, monthlyPrice: 149, quarterlyPrice: 430, halfYearlyPrice: 800, yearlyPrice: 1490, lifetimePrice: 7500, usageLimitKey: 'auditLogRetention', defaultLimit: 90, sortOrder: 24, isActive: true },
  { categorySlug: 'ai_features', slug: 'sso_authentication', name: 'SSO Authentication', description: 'Single Sign-On integration with enterprise identity providers', icon: 'KeyRound', pricingMode: 'flat', basePrice: 399, monthlyPrice: 399, quarterlyPrice: 1100, halfYearlyPrice: 2200, yearlyPrice: 3990, lifetimePrice: 20000, defaultLimit: 1, sortOrder: 25, isActive: true },
  { categorySlug: 'ai_features', slug: 'ai_models_access', name: 'AI Models Access', description: 'Access to multiple AI models (GPT-4, Claude, Gemini, etc.)', icon: 'BrainCircuit', pricingMode: 'per_unit', basePrice: 500, monthlyPrice: 500, quarterlyPrice: 1400, halfYearlyPrice: 2700, yearlyPrice: 5000, lifetimePrice: 25000, usageLimitKey: 'modelTokens', defaultLimit: 1000000, sortOrder: 26, isActive: true },
  { categorySlug: 'ai_features', slug: 'multi_language_support', name: 'Multi-language Support', description: 'Content generation and interface in multiple languages', icon: 'Languages', pricingMode: 'flat', basePrice: 200, monthlyPrice: 200, quarterlyPrice: 570, halfYearlyPrice: 1100, yearlyPrice: 2000, lifetimePrice: 10000, usageLimitKey: 'supportedLanguages', defaultLimit: 5, sortOrder: 27, isActive: true },
  { categorySlug: 'ai_features', slug: 'custom_templates', name: 'Custom Templates', description: 'Create and manage custom document and content templates', icon: 'LayoutTemplate', pricingMode: 'flat', basePrice: 149, monthlyPrice: 149, quarterlyPrice: 430, halfYearlyPrice: 800, yearlyPrice: 1490, lifetimePrice: 7500, usageLimitKey: 'customTemplates', defaultLimit: 50, sortOrder: 28, isActive: true },
  { categorySlug: 'ai_features', slug: 'export_import', name: 'Export / Import', description: 'Bulk data export and import capabilities for all content types', icon: 'ArrowLeftRight', pricingMode: 'flat', basePrice: 99, monthlyPrice: 99, quarterlyPrice: 280, halfYearlyPrice: 550, yearlyPrice: 990, lifetimePrice: 5000, usageLimitKey: 'exportImportOps', defaultLimit: 100, sortOrder: 29, isActive: true },
  { categorySlug: 'ai_features', slug: 'scheduled_automation', name: 'Scheduled Automation', description: 'Schedule AI tasks, content generation, and workflows to run automatically', icon: 'Clock', pricingMode: 'flat', basePrice: 249, monthlyPrice: 249, quarterlyPrice: 700, halfYearlyPrice: 1400, yearlyPrice: 2490, lifetimePrice: 12500, usageLimitKey: 'scheduledTasks', defaultLimit: 50, sortOrder: 30, isActive: true },

  // ── Usage Limits (quantity-based) ──
  { categorySlug: 'usage_limits', slug: 'companies_limit', name: 'Companies', description: 'Number of companies allowed', icon: 'Building2', pricingMode: 'per_unit', basePrice: 500, monthlyPrice: 500, quarterlyPrice: 1400, halfYearlyPrice: 2700, yearlyPrice: 5000, lifetimePrice: 25000, usageLimitKey: 'companies', defaultLimit: 1, sortOrder: 1, isActive: true },
  { categorySlug: 'usage_limits', slug: 'founders_limit', name: 'Founders', description: 'Number of founder profiles', icon: 'User', pricingMode: 'per_unit', basePrice: 100, monthlyPrice: 100, quarterlyPrice: 280, halfYearlyPrice: 550, yearlyPrice: 1000, lifetimePrice: 5000, usageLimitKey: 'founders', defaultLimit: 3, sortOrder: 2, isActive: true },
  { categorySlug: 'usage_limits', slug: 'employees_limit', name: 'Employees', description: 'Number of employee records', icon: 'Users', pricingMode: 'per_unit', basePrice: 50, monthlyPrice: 50, quarterlyPrice: 140, halfYearlyPrice: 275, yearlyPrice: 500, lifetimePrice: 2500, usageLimitKey: 'employees', defaultLimit: 10, sortOrder: 3, isActive: true },
  { categorySlug: 'usage_limits', slug: 'products_limit', name: 'Products', description: 'Number of product listings', icon: 'Package', pricingMode: 'per_unit', basePrice: 50, monthlyPrice: 50, quarterlyPrice: 140, halfYearlyPrice: 275, yearlyPrice: 500, lifetimePrice: 2500, usageLimitKey: 'products', defaultLimit: 10, sortOrder: 4, isActive: true },
  { categorySlug: 'usage_limits', slug: 'icp_limit', name: 'ICPs', description: 'Ideal Customer Profiles', icon: 'Target', pricingMode: 'per_unit', basePrice: 100, monthlyPrice: 100, quarterlyPrice: 280, halfYearlyPrice: 550, yearlyPrice: 1000, lifetimePrice: 5000, usageLimitKey: 'icps', defaultLimit: 5, sortOrder: 5, isActive: true },
  { categorySlug: 'usage_limits', slug: 'personas_limit', name: 'Personas', description: 'Buyer personas', icon: 'UserCircle', pricingMode: 'per_unit', basePrice: 100, monthlyPrice: 100, quarterlyPrice: 280, halfYearlyPrice: 550, yearlyPrice: 1000, lifetimePrice: 5000, usageLimitKey: 'personas', defaultLimit: 5, sortOrder: 6, isActive: true },
  { categorySlug: 'usage_limits', slug: 'competitors_limit', name: 'Competitors', description: 'Competitor tracking entries', icon: 'Swords', pricingMode: 'per_unit', basePrice: 50, monthlyPrice: 50, quarterlyPrice: 140, halfYearlyPrice: 275, yearlyPrice: 500, lifetimePrice: 2500, usageLimitKey: 'competitors', defaultLimit: 5, sortOrder: 7, isActive: true },
  { categorySlug: 'usage_limits', slug: 'investors_limit', name: 'Investors', description: 'Investor contacts tracked', icon: 'HandCoins', pricingMode: 'per_unit', basePrice: 100, monthlyPrice: 100, quarterlyPrice: 280, halfYearlyPrice: 550, yearlyPrice: 1000, lifetimePrice: 5000, usageLimitKey: 'investors', defaultLimit: 10, sortOrder: 8, isActive: true },
  { categorySlug: 'usage_limits', slug: 'funding_rounds_limit', name: 'Funding Rounds', description: 'Funding round records', icon: 'TrendingUp', pricingMode: 'per_unit', basePrice: 100, monthlyPrice: 100, quarterlyPrice: 280, halfYearlyPrice: 550, yearlyPrice: 1000, lifetimePrice: 5000, usageLimitKey: 'fundingRounds', defaultLimit: 5, sortOrder: 9, isActive: true },
];

// ============================================
// DEFAULT RULES
// ============================================

const DEFAULT_RULES = [
  {
    slug: 'annual_discount',
    name: 'Annual Discount (2 months free)',
    description: 'Get 2 months free when subscribing annually',
    ruleType: 'discount' as const,
    conditions: { categorySlugs: [], itemSlugs: [], minItemsSelected: 0, billingCycle: 'yearly' },
    effect: { type: 'percentage' as const, value: 16.67, applyTo: 'total' as const },
    priority: 10,
    stackingBehavior: 'stackable' as const,
    isActive: true,
  },
  {
    slug: 'quarterly_discount',
    name: 'Quarterly Discount (5% off)',
    description: '5% discount for quarterly billing',
    ruleType: 'discount' as const,
    conditions: { categorySlugs: [], itemSlugs: [], minItemsSelected: 0, billingCycle: 'quarterly' },
    effect: { type: 'percentage' as const, value: 5, applyTo: 'total' as const },
    priority: 20,
    stackingBehavior: 'stackable' as const,
    isActive: true,
  },
  {
    slug: 'half_yearly_discount',
    name: 'Half-Yearly Discount (10% off)',
    description: '10% discount for half-yearly billing',
    ruleType: 'discount' as const,
    conditions: { categorySlugs: [], itemSlugs: [], minItemsSelected: 0, billingCycle: 'half_yearly' },
    effect: { type: 'percentage' as const, value: 10, applyTo: 'total' as const },
    priority: 15,
    stackingBehavior: 'stackable' as const,
    isActive: true,
  },
  {
    slug: 'bundle_5_modules',
    name: '5+ Module Bundle Discount',
    description: '15% off when 5 or more modules are selected',
    ruleType: 'discount' as const,
    conditions: { categorySlugs: ['modules'], itemSlugs: [], minItemsSelected: 5, billingCycle: '' },
    effect: { type: 'percentage' as const, value: 15, applyTo: 'category' as const, targetCategorySlug: 'modules' },
    priority: 30,
    stackingBehavior: 'stackable' as const,
    isActive: true,
  },
  {
    slug: 'minimum_monthly_price',
    name: 'Minimum Monthly Price',
    description: 'Ensure minimum monthly price of ₹499',
    ruleType: 'minimum' as const,
    conditions: { categorySlugs: [], itemSlugs: [], minItemsSelected: 0, billingCycle: 'monthly' },
    effect: { type: 'fixed_amount' as const, value: 499, applyTo: 'total' as const },
    priority: 1,
    stackingBehavior: 'exclusive' as const,
    isActive: true,
  },
];

// ============================================
// DEFAULT TAXES
// ============================================

const DEFAULT_TAXES = [
  {
    name: 'GST (18%)',
    code: 'GST_18',
    rate: 18,
    country: 'IN',
    region: '',
    isInclusive: false,
    applicableBillingCycles: ['monthly', 'quarterly', 'half_yearly', 'yearly', 'lifetime'],
    isActive: true,
  },
  {
    name: 'GST (5%)',
    code: 'GST_5',
    rate: 5,
    country: 'IN',
    region: '',
    isInclusive: false,
    applicableBillingCycles: ['monthly', 'quarterly', 'half_yearly', 'yearly', 'lifetime'],
    isActive: true,
  },
  {
    name: 'VAT (20%)',
    code: 'VAT_20',
    rate: 20,
    country: 'GB',
    region: '',
    isInclusive: true,
    applicableBillingCycles: ['monthly', 'quarterly', 'half_yearly', 'yearly', 'lifetime'],
    isActive: true,
  },
  {
    name: 'VAT (19%)',
    code: 'VAT_19',
    rate: 19,
    country: 'DE',
    region: '',
    isInclusive: true,
    applicableBillingCycles: ['monthly', 'quarterly', 'half_yearly', 'yearly', 'lifetime'],
    isActive: true,
  },
];

// ============================================
// DEFAULT ADDONS
// ============================================

const DEFAULT_ADDONS = [
  {
    slug: 'whatsapp_api',
    name: 'WhatsApp Business API',
    description: 'Connect WhatsApp Business API for automated messaging and customer engagement',
    icon: 'MessageCircle',
    categorySlug: 'workflow_automation',
    pricingMode: 'flat' as const,
    monthlyPrice: 999,
    quarterlyPrice: 2800,
    halfYearlyPrice: 5500,
    yearlyPrice: 9990,
    lifetimePrice: 50000,
    usageLimitKey: 'whatsappMessages',
    defaultLimit: 10000,
    isAvailableStandalone: true,
    sortOrder: 1,
    isActive: true,
  },
  {
    slug: 'crm_integration',
    name: 'CRM Integration',
    description: 'Sync with HubSpot, Salesforce, and other CRM platforms',
    icon: 'Database',
    categorySlug: 'workflow_automation',
    pricingMode: 'flat' as const,
    monthlyPrice: 799,
    quarterlyPrice: 2300,
    halfYearlyPrice: 4500,
    yearlyPrice: 7990,
    lifetimePrice: 40000,
    usageLimitKey: 'crmContacts',
    defaultLimit: 5000,
    isAvailableStandalone: true,
    sortOrder: 2,
    isActive: true,
  },
  {
    slug: 'zapier_integration',
    name: 'Zapier Integration',
    description: 'Connect to 5000+ apps via Zapier automation',
    icon: 'Workflow',
    categorySlug: 'workflow_automation',
    pricingMode: 'flat' as const,
    monthlyPrice: 499,
    quarterlyPrice: 1400,
    halfYearlyPrice: 2700,
    yearlyPrice: 4990,
    lifetimePrice: 25000,
    usageLimitKey: 'zapierTasks',
    defaultLimit: 1000,
    isAvailableStandalone: true,
    sortOrder: 3,
    isActive: true,
  },
  {
    slug: 'advanced_analytics',
    name: 'Advanced Analytics',
    description: 'Deep analytics dashboard with custom reports and data export',
    icon: 'BarChart3',
    categorySlug: 'usage_limits',
    pricingMode: 'flat' as const,
    monthlyPrice: 599,
    quarterlyPrice: 1700,
    halfYearlyPrice: 3300,
    yearlyPrice: 5990,
    lifetimePrice: 30000,
    usageLimitKey: 'analyticsReports',
    defaultLimit: 100,
    isAvailableStandalone: true,
    sortOrder: 4,
    isActive: true,
  },
  {
    slug: 'priority_support',
    name: 'Priority Support',
    description: '24/7 priority support with dedicated account manager',
    icon: 'Headphones',
    categorySlug: 'usage_limits',
    pricingMode: 'flat' as const,
    monthlyPrice: 1499,
    quarterlyPrice: 4200,
    halfYearlyPrice: 8200,
    yearlyPrice: 14990,
    lifetimePrice: 75000,
    isAvailableStandalone: true,
    sortOrder: 5,
    isActive: true,
  },
  {
    slug: 'white_label_addon',
    name: 'White Label',
    description: 'Full white-label branding removal and custom branding',
    icon: 'Palette',
    categorySlug: 'usage_limits',
    pricingMode: 'flat' as const,
    monthlyPrice: 2999,
    quarterlyPrice: 8500,
    halfYearlyPrice: 16500,
    yearlyPrice: 29990,
    lifetimePrice: 150000,
    isAvailableStandalone: true,
    sortOrder: 6,
    isActive: true,
  },
  {
    slug: 'api_access_pro',
    name: 'API Access Pro',
    description: 'Unlimited API access with higher rate limits and webhook support',
    icon: 'Code',
    categorySlug: 'api_limits',
    pricingMode: 'flat' as const,
    monthlyPrice: 999,
    quarterlyPrice: 2800,
    halfYearlyPrice: 5500,
    yearlyPrice: 9990,
    lifetimePrice: 50000,
    usageLimitKey: 'apiCalls',
    defaultLimit: 100000,
    isAvailableStandalone: true,
    sortOrder: 7,
    isActive: true,
  },
  {
    slug: 'extra_storage',
    name: 'Extra Storage (10 GB)',
    description: 'Additional 10 GB of storage for assets and media',
    icon: 'HardDrive',
    categorySlug: 'storage',
    pricingMode: 'per_unit' as const,
    monthlyPrice: 299,
    quarterlyPrice: 850,
    halfYearlyPrice: 1650,
    yearlyPrice: 2990,
    lifetimePrice: 15000,
    usageLimitKey: 'storageGB',
    defaultLimit: 10,
    isAvailableStandalone: true,
    sortOrder: 8,
    isActive: true,
  },
];

// ============================================
// SEED FUNCTION
// ============================================

export async function seedPricingConfig(): Promise<void> {
  try {
    const models = getModels();

    let categoriesCreated = 0;
    let categoriesUpdated = 0;
    let categoriesSkipped = 0;

    // Seed Categories — create new or update existing
    if (models.PricingCategory) {
      for (const cat of DEFAULT_CATEGORIES) {
        const existing = await (models.PricingCategory as any).findOne({ slug: cat.slug });
        if (!existing) {
          await (models.PricingCategory as any).create(cat);
          categoriesCreated++;
        } else {
          await (models.PricingCategory as any).findOneAndUpdate(
            { slug: cat.slug },
            { $set: cat },
            { runValidators: true },
          );
          categoriesUpdated++;
        }
      }
    }

    let itemsCreated = 0;
    let itemsUpdated = 0;
    let itemsSkipped = 0;

    // Seed Items — create new or update existing to keep data in sync
    if (models.PricingItem) {
      for (const item of DEFAULT_ITEMS) {
        const existing = await (models.PricingItem as any).findOne({ slug: item.slug });
        if (!existing) {
          await (models.PricingItem as any).create(item);
          itemsCreated++;
        } else {
          // Update existing item with latest seed data (preserves any extra fields)
          await (models.PricingItem as any).findOneAndUpdate(
            { slug: item.slug },
            { $set: item },
            { runValidators: true },
          );
          itemsUpdated++;
        }
      }

      // Deactivate old AI providers/models that are no longer in the seed data
      const seededItemSlugs = new Set(DEFAULT_ITEMS.map(i => i.slug));
      const aiCategories = ['ai_providers', 'ai_models'];
      const oldItems = await (models.PricingItem as any).find({
        categorySlug: { $in: aiCategories },
        slug: { $nin: [...seededItemSlugs] },
        isActive: true,
      });
      if (oldItems.length > 0) {
        const oldSlugs = oldItems.map((i: any) => i.slug);
        await (models.PricingItem as any).updateMany(
          { slug: { $in: oldSlugs } },
          { $set: { isActive: false } },
        );
        console.log(`[Seed] Deactivated ${oldItems.length} obsolete AI items: ${oldSlugs.join(', ')}`);
      }
    }

    let rulesCreated = 0;
    let rulesSkipped = 0;

    // Seed Rules
    if (models.PricingRule) {
      for (const rule of DEFAULT_RULES) {
        const existing = await (models.PricingRule as any).findOne({ slug: rule.slug });
        if (!existing) {
          await (models.PricingRule as any).create(rule);
          rulesCreated++;
        } else {
          rulesSkipped++;
        }
      }
    }

    let taxesCreated = 0;
    let taxesSkipped = 0;

    // Seed Taxes
    if (models.PricingTax) {
      for (const tax of DEFAULT_TAXES) {
        const existing = await (models.PricingTax as any).findOne({ code: tax.code });
        if (!existing) {
          await (models.PricingTax as any).create(tax);
          taxesCreated++;
        } else {
          taxesSkipped++;
        }
      }
    }

    let addonsCreated = 0;
    let addonsSkipped = 0;

    // Seed Addons
    if (models.PricingAddon) {
      for (const addon of DEFAULT_ADDONS) {
        const existing = await (models.PricingAddon as any).findOne({ slug: addon.slug });
        if (!existing) {
          await (models.PricingAddon as any).create(addon);
          addonsCreated++;
        } else {
          addonsSkipped++;
        }
      }
    }

    console.log(`[Seed] Pricing Config: ${categoriesCreated} categories created, ${categoriesUpdated} updated, ${categoriesSkipped} skipped | ${itemsCreated} items created, ${itemsUpdated} updated, ${itemsSkipped} skipped | ${rulesCreated} rules created, ${rulesSkipped} skipped | ${taxesCreated} taxes created, ${taxesSkipped} skipped | ${addonsCreated} addons created, ${addonsSkipped} skipped`);
  } catch (error) {
    console.error('[Seed] Error seeding pricing config:', error);
    // Don't throw — seeding failure shouldn't crash the server
  }
}