<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class LoyaltyLevel extends Model
{
    protected $fillable = [
        'name',
        'slug',
        'min_points',
        'discount_percent',
        'icon',
        'color',
        'benefits',
        'sort_order',
        'is_active',
    ];

    protected $casts = [
        'min_points' => 'integer',
        'discount_percent' => 'decimal:2',
        'benefits' => 'array',
        'is_active' => 'boolean',
    ];

    protected static function boot()
    {
        parent::boot();
        
        static::creating(function ($level) {
            if (empty($level->slug)) {
                $level->slug = Str::slug($level->name);
            }
        });
    }

    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    public function scopeOrdered($query)
    {
        return $query->orderBy('sort_order')->orderBy('min_points');
    }

    /**
     * Obtenir le niveau actuel d'un utilisateur basé sur ses points
     */
    public static function getUserLevel(int $points): ?self
    {
        return static::active()
            ->where('min_points', '<=', $points)
            ->orderByDesc('min_points')
            ->first();
    }

    /**
     * Obtenir le prochain niveau après le niveau actuel
     */
    public static function getNextLevel(int $currentPoints): ?self
    {
        return static::active()
            ->where('min_points', '>', $currentPoints)
            ->orderBy('min_points')
            ->first();
    }

    /**
     * Obtenir tous les niveaux avec fallback aux valeurs par défaut
     */
    public static function getAllLevelsWithDefaults(): array
    {
        $dbLevels = static::active()->ordered()->get();
        
        if ($dbLevels->isEmpty()) {
            // Retourner les niveaux par défaut
            return [
                ['name' => 'Débutant', 'min_points' => 0, 'discount_percent' => 0, 'color' => '#9CA3AF', 'icon' => '⭐'],
                ['name' => 'Bronze', 'min_points' => 1000, 'discount_percent' => 3, 'color' => '#CD7F32', 'icon' => '🥉'],
                ['name' => 'Argent', 'min_points' => 5000, 'discount_percent' => 5, 'color' => '#C0C0C0', 'icon' => '🥈'],
                ['name' => 'Or', 'min_points' => 10000, 'discount_percent' => 7, 'color' => '#FFD700', 'icon' => '🥇'],
                ['name' => 'Platine', 'min_points' => 25000, 'discount_percent' => 10, 'color' => '#E5E4E2', 'icon' => '🏆'],
                ['name' => 'Diamant', 'min_points' => 50000, 'discount_percent' => 15, 'color' => '#B9F2FF', 'icon' => '💎'],
            ];
        }
        
        return $dbLevels->map(function ($level) {
            return [
                'id' => $level->id,
                'name' => $level->name,
                'min_points' => $level->min_points,
                'discount_percent' => $level->discount_percent,
                'color' => $level->color ?? '#9CA3AF',
                'icon' => $level->icon ?? '⭐',
            ];
        })->toArray();
    }
}
