@extends('layouts.member')

@section('title', 'Programme de Fidélité')
@section('page-title', 'Programme de Fidélité')

@section('content')
<div class="row g-4 mb-4">
    <div class="col-sm-6 col-lg-3">
        <div class="stat-card modern shadow-sm border-0 rounded-3 text-center">
            <p class="text-muted small mb-1">Points disponibles</p>
            <h4 class="text-success fw-700 mb-0">{{ number_format($availablePoints) }}</h4>
        </div>
    </div>
    <div class="col-sm-6 col-lg-3">
        <div class="stat-card modern shadow-sm border-0 rounded-3 text-center">
            <p class="text-muted small mb-1">Points gagnés</p>
            <h4 class="text-primary fw-700 mb-0">{{ number_format($totalPoints) }}</h4>
        </div>
    </div>
    <div class="col-sm-6 col-lg-3">
        <div class="stat-card modern shadow-sm border-0 rounded-3 text-center">
            <p class="text-muted small mb-1">Points utilisés</p>
            <h4 class="fw-700 mb-0">{{ number_format($usedPoints) }}</h4>
        </div>
    </div>
    <div class="col-sm-6 col-lg-3">
        <div class="stat-card modern shadow-sm border-0 rounded-3 text-center" style="border: 2px solid {{ $level['color'] }}33;">
            <p class="text-muted small mb-1">Votre niveau</p>
            <h4 class="fw-700 mb-0" style="color: {{ $level['color'] }}">{{ $level['icon'] ?? '⭐' }} {{ $level['name'] }}</h4>
            <small class="text-muted">{{ $level['discount'] }}% de réduction</small>
        </div>
    </div>
</div>

{{-- Progression vers le prochain niveau --}}
@if($nextLevel)
<div class="card modern shadow-sm border-0 rounded-3 mb-4">
    <div class="card-body">
        <div class="d-flex justify-content-between align-items-center mb-2">
            <span class="fw-600">Prochain niveau : {{ $nextLevel['name'] }}</span>
            <span class="text-muted">{{ number_format($nextLevel['points_needed']) }} points restants</span>
        </div>
        @php
            $currentLevelMin = $level['min_points'] ?? 0;
            $nextLevelMin = $nextLevel['points_needed'] + $totalPoints;
            $progress = $nextLevelMin > $currentLevelMin ? (($totalPoints - $currentLevelMin) / ($nextLevelMin - $currentLevelMin)) * 100 : 0;
        @endphp
        <div class="progress" style="height: 12px;">
            <div class="progress-bar bg-success" role="progressbar" style="width: {{ min(100, $progress) }}%"></div>
        </div>
        <small class="text-muted mt-1 d-block">Débloquez {{ $nextLevel['discount'] }}% de réduction en atteignant le niveau {{ $nextLevel['name'] }}</small>
    </div>
</div>
@else
<div class="alert alert-success border-0 rounded-3 mb-4">
    <i class="bi bi-trophy me-2"></i>Félicitations ! Vous avez atteint le niveau maximum : <strong>{{ $level['name'] }}</strong> avec {{ $level['discount'] }}% de réduction !
</div>
@endif

<div class="row g-4 mb-4">
    <div class="col-md-3 col-6">
        <div class="stat-card modern shadow-sm border-0 rounded-3 text-center bg-light">
            <p class="text-muted small mb-1">Commandes</p>
            <h5 class="fw-700 mb-0">{{ $stats['orders_count'] ?? 0 }}</h5>
        </div>
    </div>
    <div class="col-md-3 col-6">
        <div class="stat-card modern shadow-sm border-0 rounded-3 text-center bg-light">
            <p class="text-muted small mb-1">Total dépensé</p>
            <h5 class="fw-700 mb-0">{{ number_format($stats['total_spent'] ?? 0, 0, ',', ' ') }} XOF</h5>
        </div>
    </div>
    <div class="col-md-3 col-6">
        <div class="stat-card modern shadow-sm border-0 rounded-3 text-center bg-light">
            <p class="text-muted small mb-1">Commissions</p>
            <h5 class="fw-700 mb-0">{{ number_format($stats['commissions_earned'] ?? 0, 0, ',', ' ') }} XOF</h5>
        </div>
    </div>
    <div class="col-md-3 col-6">
        <div class="stat-card modern shadow-sm border-0 rounded-3 text-center bg-light">
            <p class="text-muted small mb-1">Points ce mois</p>
            <h5 class="fw-700 mb-0">+{{ number_format($stats['points_this_month'] ?? 0) }}</h5>
        </div>
    </div>
</div>

<div class="row g-4">
    <div class="col-lg-6">
        <div class="card modern shadow-sm border-0 rounded-3 h-100">
            <div class="card-header bg-white rounded-top-3 d-flex justify-content-between align-items-center">
                <h5 class="mb-0"><i class="bi bi-bar-chart me-2"></i>Niveaux de fidélité</h5>
                <small class="text-muted">Points seuil et remise</small>
            </div>
            <div class="card-body">
                @php
                    // Utiliser $allLevels passé par le contrôleur (ou fallback)
                    $displayLevels = $allLevels ?? [
                        ['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' => '💎'],
                    ];
                @endphp
                <div class="d-flex flex-column gap-2">
                    @foreach($displayLevels as $lvl)
                    @php
                        $lvlName = $lvl['name'] ?? '';
                        $lvlPoints = $lvl['min_points'] ?? $lvl['points'] ?? 0;
                        $lvlDiscount = $lvl['discount_percent'] ?? $lvl['discount'] ?? 0;
                        $lvlColor = $lvl['color'] ?? '#9CA3AF';
                        $lvlIcon = $lvl['icon'] ?? '⭐';
                        $isCurrentLevel = ($level['name'] ?? '') === $lvlName;
                    @endphp
                    <div class="d-flex align-items-center gap-3 p-3 rounded-3 {{ $isCurrentLevel ? 'bg-primary bg-opacity-10 border border-primary' : 'bg-light' }}">
                        <div class="badge-icon" style="background-color: {{ $lvlColor }}30; font-size: 20px;">
                            {{ $lvlIcon }}
                        </div>
                        <div class="flex-fill">
                            <div class="fw-600" style="color: {{ $lvlColor }}">{{ $lvlName }}</div>
                            <div class="text-muted small">{{ number_format($lvlPoints) }}+ points</div>
                        </div>
                        <span class="fw-600 badge {{ $isCurrentLevel ? 'bg-primary' : 'bg-secondary' }}">{{ $lvlDiscount }}%</span>
                    </div>
                    @endforeach
                </div>
            </div>
        </div>
    </div>

    <div class="col-lg-6">
        <div class="card modern shadow-sm border-0 rounded-3 h-100">
            <div class="card-header bg-white rounded-top-3 d-flex justify-content-between align-items-center">
                <h5 class="mb-0"><i class="bi bi-clock-history me-2"></i>Historique des points</h5>
                <small class="text-muted">Crédits et débits</small>
            </div>
            <div class="card-body p-0" style="max-height: 420px; overflow-y: auto;">
                @forelse($loyaltyPoints as $point)
                <div class="p-3 d-flex justify-content-between align-items-center border-bottom">
                    <div>
                        <div class="fw-600">{{ $point->description ?? $point->source }}</div>
                        <div class="text-muted small">{{ $point->created_at->format('d/m/Y H:i') }}</div>
                    </div>
                    @php
                        $isPositive = in_array($point->type, ['earned', 'bonus']);
                    @endphp
                    <span class="fw-700 {{ $isPositive ? 'text-success' : 'text-danger' }}">
                        {{ $isPositive ? '+' : '-' }}{{ number_format($point->points) }}
                    </span>
                </div>
                @empty
                <div class="p-5 text-center text-muted">
                    <i class="bi bi-inbox fs-1 d-block mb-2"></i>
                    Aucun historique de points
                </div>
                @endforelse
            </div>
            @if($loyaltyPoints->hasPages())
            <div class="card-footer bg-white">
                {{ $loyaltyPoints->links() }}
            </div>
            @endif
        </div>
    </div>
</div>

<style>
.badge-icon { width: 40px; height: 40px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; }
</style>
@endsection
