<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class PlanSeeder extends Seeder
{
    public function run(): void
    {
        $plans = [
            [
                'name' => 'Free',
                'slug' => 'free',
                'monthly_price' => 0,
                'yearly_price' => 0,
                'description' => 'Get started with AI-powered email management',
                'is_active' => true,
                'is_popular' => false,
                'sort_order' => 1,
                'features' => json_encode([
                    '1 email account',
                    '50 AI replies/month',
                    '100 contacts',
                    'Basic inbox',
                    'Email support',
                ]),
                'limits' => [
                    'email_accounts' => 1,
                    'ai_replies' => 50,
                    'contacts' => 100,
                    'team_members' => 1,
                    'storage_mb' => 100,
                    'campaigns_per_month' => 1,
                    'workflows' => 1,
                    'kb_documents' => 5,
                    'kb_file_size_mb' => 5,
                    'knowledge_base' => false,
                    'campaigns' => false,
                    'deal_pipeline' => false,
                    'analytics' => false,
                    'whatsapp' => false,
                    'sms' => false,
                    'telegram' => false,
                    'slack' => false,
                    'live_chat' => false,
                    'custom_roles' => false,
                    'api_access' => false,
                    'white_label' => false,
                    'priority_support' => false,
                    'sso_saml' => false,
                    'temp_mail' => false,
                ],
            ],
            [
                'name' => 'Starter',
                'slug' => 'starter',
                'monthly_price' => 19,
                'yearly_price' => 190,
                'description' => 'For small teams getting serious about AI communication',
                'is_active' => true,
                'is_popular' => false,
                'sort_order' => 2,
                'features' => json_encode([
                    '3 email accounts',
                    '500 AI replies/month',
                    '2,500 contacts',
                    '3 team members',
                    'WhatsApp & Telegram',
                    '5 workflows',
                    'Basic analytics',
                    'Email & chat support',
                ]),
                'limits' => [
                    'email_accounts' => 3,
                    'ai_replies' => 500,
                    'contacts' => 2500,
                    'team_members' => 3,
                    'storage_mb' => 1000,
                    'campaigns_per_month' => 10,
                    'workflows' => 5,
                    'kb_documents' => 25,
                    'kb_file_size_mb' => 10,
                    'knowledge_base' => true,
                    'campaigns' => true,
                    'deal_pipeline' => false,
                    'analytics' => true,
                    'whatsapp' => true,
                    'sms' => false,
                    'telegram' => true,
                    'slack' => false,
                    'live_chat' => true,
                    'custom_roles' => false,
                    'api_access' => false,
                    'white_label' => false,
                    'priority_support' => false,
                    'sso_saml' => false,
                    'temp_mail' => false,
                ],
            ],
            [
                'name' => 'Pro',
                'slug' => 'pro',
                'monthly_price' => 49,
                'yearly_price' => 490,
                'description' => 'For growing businesses with advanced automation needs',
                'is_active' => true,
                'is_popular' => true,
                'sort_order' => 3,
                'features' => json_encode([
                    '10 email accounts',
                    '5,000 AI replies/month',
                    '25,000 contacts',
                    '10 team members',
                    'All channels (WhatsApp, SMS, Telegram, Slack, Chat)',
                    'Unlimited workflows',
                    'Deal pipeline & CRM',
                    'A/B testing',
                    'Advanced analytics',
                    'API access',
                    'Priority support',
                ]),
                'limits' => [
                    'email_accounts' => 10,
                    'ai_replies' => 5000,
                    'contacts' => 25000,
                    'team_members' => 10,
                    'storage_mb' => 10000,
                    'campaigns_per_month' => null, // unlimited
                    'workflows' => null,
                    'kb_documents' => 100,
                    'kb_file_size_mb' => 25,
                    'knowledge_base' => true,
                    'campaigns' => true,
                    'deal_pipeline' => true,
                    'analytics' => true,
                    'whatsapp' => true,
                    'sms' => true,
                    'telegram' => true,
                    'slack' => true,
                    'live_chat' => true,
                    'custom_roles' => false,
                    'api_access' => true,
                    'white_label' => false,
                    'priority_support' => true,
                    'sso_saml' => false,
                    'temp_mail' => true,
                ],
            ],
            [
                'name' => 'Enterprise',
                'slug' => 'enterprise',
                'monthly_price' => 199,
                'yearly_price' => 1990,
                'description' => 'For large teams and agencies with unlimited needs',
                'is_active' => true,
                'is_popular' => false,
                'sort_order' => 4,
                'features' => json_encode([
                    'Unlimited email accounts',
                    'Unlimited AI replies',
                    'Unlimited contacts',
                    'Unlimited team members',
                    'All channels',
                    'Dedicated account manager',
                    'SLA guarantee',
                    'Custom integrations',
                    // Reserved for future release: white-label, custom roles, SSO/SAML.
                ]),
                'limits' => [
                    'email_accounts' => null,
                    'ai_replies' => null,
                    'contacts' => null,
                    'team_members' => null,
                    'storage_mb' => null,
                    'campaigns_per_month' => null,
                    'workflows' => null,
                    'kb_documents' => null,
                    'kb_file_size_mb' => 50,
                    'knowledge_base' => true,
                    'campaigns' => true,
                    'deal_pipeline' => true,
                    'analytics' => true,
                    'whatsapp' => true,
                    'sms' => true,
                    'telegram' => true,
                    'slack' => true,
                    'live_chat' => true,
                    'custom_roles' => true,
                    'api_access' => true,
                    'white_label' => true,
                    'priority_support' => true,
                    'sso_saml' => true,
                    'temp_mail' => true,
                ],
            ],
        ];

        foreach ($plans as $planData) {
            $limits = $planData['limits'];
            unset($planData['limits']);

            $plan = DB::table('plans')->insertGetId(array_merge($planData, [
                'created_at' => now(),
                'updated_at' => now(),
            ]));

            foreach ($limits as $key => $value) {
                DB::table('plan_features')->insert([
                    'plan_id' => $plan,
                    'feature_key' => $key,
                    'enabled' => is_bool($value) ? $value : true,
                    'limit' => is_bool($value) ? null : $value,
                    'created_at' => now(),
                    'updated_at' => now(),
                ]);
            }
        }
    }
}
