<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\MlmLevel;
use App\Models\Rank;
use App\Models\Setting;

class MlmSeeder extends Seeder
{
    public function run(): void
    {
        $this->seedMlmLevels();
        $this->seedRanks();
        $this->seedMlmSettings();
    }

    protected function seedMlmLevels(): void
    {
        $levels = [
            ['level' => 1, 'name' => 'Niveau 1 - Filleul Direct', 'commission_rate' => 10, 'bonus_rate' => 0, 'is_active' => true],
            ['level' => 2, 'name' => 'Niveau 2 - Filleul Indirect', 'commission_rate' => 5, 'bonus_rate' => 0, 'is_active' => true],
        ];

        foreach ($levels as $level) {
            MlmLevel::updateOrCreate(
                ['level' => $level['level']],
                $level
            );
        }

        MlmLevel::where('level', '>', 2)->update(['is_active' => false]);
    }

    protected function seedRanks(): void
    {
        $ranks = [
            [
                'name' => 'Débutant',
                'slug' => 'debutant',
                'level' => 0,
                'min_personal_sales' => 0,
                'min_team_sales' => 0,
                'min_direct_referrals' => 0,
                'min_team_size' => 0,
                'commission_bonus' => 0,
                'badge_color' => '#6B7280',
                'benefits' => json_encode(['description' => 'Niveau de départ pour tous les membres']),
            ],
            [
                'name' => 'Bronze',
                'slug' => 'bronze',
                'level' => 1,
                'min_personal_sales' => 50000,
                'min_team_sales' => 100000,
                'min_direct_referrals' => 3,
                'min_team_size' => 5,
                'commission_bonus' => 1,
                'badge_color' => '#CD7F32',
                'benefits' => json_encode(['bonus_commission' => '1%', 'priority_support' => false]),
            ],
            [
                'name' => 'Argent',
                'slug' => 'argent',
                'level' => 2,
                'min_personal_sales' => 150000,
                'min_team_sales' => 500000,
                'min_direct_referrals' => 5,
                'min_team_size' => 15,
                'commission_bonus' => 2,
                'badge_color' => '#C0C0C0',
                'benefits' => json_encode(['bonus_commission' => '2%', 'priority_support' => true]),
            ],
            [
                'name' => 'Or',
                'slug' => 'or',
                'level' => 3,
                'min_personal_sales' => 300000,
                'min_team_sales' => 1500000,
                'min_direct_referrals' => 10,
                'min_team_size' => 50,
                'commission_bonus' => 3,
                'badge_color' => '#FFD700',
                'benefits' => json_encode(['bonus_commission' => '3%', 'priority_support' => true, 'exclusive_events' => true]),
            ],
            [
                'name' => 'Platine',
                'slug' => 'platine',
                'level' => 4,
                'min_personal_sales' => 500000,
                'min_team_sales' => 5000000,
                'min_direct_referrals' => 15,
                'min_team_size' => 150,
                'commission_bonus' => 4,
                'badge_color' => '#E5E4E2',
                'benefits' => json_encode(['bonus_commission' => '4%', 'priority_support' => true, 'exclusive_events' => true, 'personal_mentor' => true]),
            ],
            [
                'name' => 'Diamant',
                'slug' => 'diamant',
                'level' => 5,
                'min_personal_sales' => 1000000,
                'min_team_sales' => 15000000,
                'min_direct_referrals' => 25,
                'min_team_size' => 500,
                'commission_bonus' => 5,
                'badge_color' => '#B9F2FF',
                'benefits' => json_encode(['bonus_commission' => '5%', 'priority_support' => true, 'exclusive_events' => true, 'personal_mentor' => true, 'leadership_council' => true, 'travel_rewards' => true]),
            ],
        ];

        foreach ($ranks as $rank) {
            Rank::updateOrCreate(
                ['slug' => $rank['slug']],
                $rank
            );
        }
    }

    protected function seedMlmSettings(): void
    {
        $settings = [
            'mlm_max_levels' => '2',
            'commission_level1' => '10',
            'commission_level2' => '5',
            'quick_start_bonus' => '5000',
            'require_monthly_purchase' => '0',
            'inactivity_days_limit' => '30',
            'min_withdrawal_amount' => '5000',
            'withdrawal_fee_rate' => '2',
            'loyalty_points_per_currency' => '100',
            'loyalty_point_value' => '1',
            'loyalty_points_expiration_months' => '12',
            'default_seller_commission' => '10',
            'require_product_approval' => '1',
            'base_currency' => 'XOF',
            'order_auto_confirm_days' => '7',
            'mlm_module_enabled' => '1',
            'seller_module_enabled' => '1',
            'rating_module_enabled' => '1',
            'loyalty_module_enabled' => '1',
        ];

        foreach ($settings as $key => $value) {
            Setting::updateOrCreate(
                ['key' => $key],
                ['value' => $value, 'type' => 'string']
            );
        }
    }
}
