<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateMlmLevelsTable extends Migration
{
    public function up()
    {
        // Vérifier si table existe déjà
        if (Schema::hasTable('mlm_levels')) {
            return; // Table existe déjà
        }

        Schema::create('mlm_levels', function (Blueprint $table) {
            $table->id();
            $table->integer('level')->unique(); // 1-7
            $table->decimal('commission_rate', 5, 2); // Pourcentage
            $table->string('description')->nullable();
            $table->boolean('active')->default(true);
            $table->timestamps();
        });

        // Seed niveaux par défaut (seulement si vide)
        if (\DB::table('mlm_levels')->count() === 0) {
            \DB::table('mlm_levels')->insert([
            ['level' => 1, 'commission_rate' => 5.00, 'description' => 'Sponsor direct'],
            ['level' => 2, 'commission_rate' => 3.00, 'description' => 'Niveau 2'],
            ['level' => 3, 'commission_rate' => 2.00, 'description' => 'Niveau 3'],
            ['level' => 4, 'commission_rate' => 1.00, 'description' => 'Niveau 4'],
            ['level' => 5, 'commission_rate' => 0.50, 'description' => 'Niveau 5'],
            ['level' => 6, 'commission_rate' => 0.50, 'description' => 'Niveau 6'],
            ['level' => 7, 'commission_rate' => 0.50, 'description' => 'Niveau 7'],
        ]);
        }
    }

    public function down()
    {
        Schema::dropIfExists('mlm_levels');
    }
}
