@extends('layouts.seller')

@section('title', 'Mes Coupons')

@push('styles')
<style>
    /* ===== ULTRA MODERN COUPONS PAGE ===== */
    .coupons-hero {
        background: linear-gradient(135deg, rgba(236, 72, 153, 0.15) 0%, rgba(219, 39, 119, 0.1) 50%, rgba(190, 24, 93, 0.08) 100%);
        border-radius: 24px;
        padding: 32px;
        margin-bottom: 32px;
        border: 1px solid rgba(236, 72, 153, 0.2);
        position: relative;
        overflow: hidden;
    }
    
    .coupons-hero::before {
        content: '';
        position: absolute;
        top: -50%;
        right: -20%;
        width: 400px;
        height: 400px;
        background: radial-gradient(circle, rgba(236, 72, 153, 0.1) 0%, transparent 70%);
        animation: pulse-glow 4s ease-in-out infinite;
    }
    
    @keyframes pulse-glow {
        0%, 100% { transform: scale(1); opacity: 0.5; }
        50% { transform: scale(1.1); opacity: 0.8; }
    }
    
    .hero-title {
        font-size: 2rem;
        font-weight: 700;
        background: linear-gradient(135deg, #ec4899, #db2777, #be185d);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        background-clip: text;
        margin-bottom: 8px;
    }
    
    .hero-subtitle { color: var(--muted); font-size: 1rem; }
    
    /* Stats Grid */
    .stats-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 20px;
        margin-bottom: 32px;
    }
    
    .stat-box {
        background: linear-gradient(145deg, #ffffff 0%, #f8fafc 100%);
        border-radius: 20px;
        padding: 24px;
        border: 1px solid rgba(236, 72, 153, 0.1);
        position: relative;
        overflow: hidden;
        transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    }
    
    .stat-box:hover {
        transform: translateY(-8px);
        box-shadow: 0 20px 40px rgba(236, 72, 153, 0.15);
    }
    
    .stat-box::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        width: 4px;
        height: 100%;
        background: linear-gradient(180deg, #ec4899, #db2777);
        border-radius: 4px 0 0 4px;
    }
    
    .stat-box.green::before { background: linear-gradient(180deg, #22c55e, #16a34a); }
    .stat-box.blue::before { background: linear-gradient(180deg, #3b82f6, #2563eb); }
    .stat-box.orange::before { background: linear-gradient(180deg, #f59e0b, #d97706); }
    
    .stat-icon {
        width: 48px;
        height: 48px;
        border-radius: 14px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.4rem;
        margin-bottom: 16px;
        background: linear-gradient(135deg, rgba(236, 72, 153, 0.15), rgba(219, 39, 119, 0.1));
        color: #ec4899;
    }
    
    .stat-box.green .stat-icon { background: linear-gradient(135deg, rgba(34, 197, 94, 0.15), rgba(22, 163, 74, 0.1)); color: #22c55e; }
    .stat-box.blue .stat-icon { background: linear-gradient(135deg, rgba(59, 130, 246, 0.15), rgba(37, 99, 235, 0.1)); color: #3b82f6; }
    .stat-box.orange .stat-icon { background: linear-gradient(135deg, rgba(245, 158, 11, 0.15), rgba(217, 119, 6, 0.1)); color: #f59e0b; }
    
    .stat-label { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); font-weight: 600; margin-bottom: 8px; }
    .stat-value { font-size: 1.8rem; font-weight: 700; color: var(--text); }
    
    /* Action Bar */
    .action-bar {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 20px;
        margin-bottom: 24px;
        flex-wrap: wrap;
    }
    
    .btn-create {
        background: linear-gradient(135deg, #ec4899, #db2777);
        color: white;
        border: none;
        border-radius: 14px;
        padding: 14px 28px;
        font-weight: 600;
        display: inline-flex;
        align-items: center;
        gap: 10px;
        transition: all 0.3s ease;
        text-decoration: none;
    }
    
    .btn-create:hover {
        transform: translateY(-3px);
        box-shadow: 0 10px 30px rgba(236, 72, 153, 0.35);
        color: white;
    }
    
    /* Coupons Grid */
    .coupons-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
        gap: 24px;
    }
    
    .coupon-card {
        background: #ffffff;
        border-radius: 20px;
        overflow: hidden;
        border: 1px solid rgba(0, 0, 0, 0.06);
        transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
        position: relative;
    }
    
    .coupon-card:hover {
        transform: translateY(-8px);
        box-shadow: 0 25px 50px rgba(0, 0, 0, 0.1);
    }
    
    .coupon-card::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        height: 4px;
        background: linear-gradient(90deg, #ec4899, #db2777, #be185d);
    }
    
    .coupon-card.expired::before { background: linear-gradient(90deg, #94a3b8, #64748b); }
    .coupon-card.inactive::before { background: linear-gradient(90deg, #f59e0b, #d97706); }
    
    .coupon-header {
        padding: 24px;
        background: linear-gradient(135deg, rgba(236, 72, 153, 0.05), rgba(219, 39, 119, 0.02));
        border-bottom: 1px dashed rgba(236, 72, 153, 0.2);
        position: relative;
    }
    
    .coupon-card.expired .coupon-header { background: linear-gradient(135deg, rgba(148, 163, 184, 0.05), rgba(100, 116, 139, 0.02)); }
    
    .coupon-badge {
        position: absolute;
        top: 16px;
        right: 16px;
        padding: 6px 14px;
        border-radius: 20px;
        font-size: 0.7rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }
    
    .coupon-badge.active { background: rgba(34, 197, 94, 0.1); color: #22c55e; }
    .coupon-badge.inactive { background: rgba(245, 158, 11, 0.1); color: #f59e0b; }
    .coupon-badge.expired { background: rgba(239, 68, 68, 0.1); color: #ef4444; }
    
    .coupon-code {
        display: inline-flex;
        align-items: center;
        gap: 10px;
        background: linear-gradient(135deg, #ec4899, #db2777);
        color: white;
        padding: 10px 20px;
        border-radius: 12px;
        font-family: 'Courier New', monospace;
        font-size: 1.2rem;
        font-weight: 700;
        letter-spacing: 2px;
        margin-bottom: 12px;
        cursor: pointer;
        transition: all 0.3s ease;
    }
    
    .coupon-card.expired .coupon-code { background: linear-gradient(135deg, #94a3b8, #64748b); }
    
    .coupon-code:hover { transform: scale(1.02); }
    .coupon-code i { font-size: 0.9rem; opacity: 0.8; }
    
    .coupon-discount {
        font-size: 2rem;
        font-weight: 700;
        color: var(--text);
    }
    
    .coupon-discount span { font-size: 1rem; font-weight: 500; color: var(--muted); }
    
    .coupon-body { padding: 24px; }
    
    .coupon-info {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 16px;
        margin-bottom: 20px;
    }
    
    .info-item {
        display: flex;
        align-items: center;
        gap: 10px;
    }
    
    .info-icon {
        width: 36px;
        height: 36px;
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: #f8fafc;
        color: var(--muted);
        font-size: 0.9rem;
    }
    
    .info-text { font-size: 0.85rem; }
    .info-text label { display: block; font-size: 0.7rem; text-transform: uppercase; color: var(--muted); font-weight: 600; }
    .info-text span { font-weight: 600; color: var(--text); }
    
    .coupon-progress {
        margin-bottom: 20px;
    }
    
    .progress-header {
        display: flex;
        justify-content: space-between;
        margin-bottom: 8px;
        font-size: 0.8rem;
    }
    
    .progress-label { color: var(--muted); }
    .progress-value { font-weight: 600; color: var(--text); }
    
    .progress-bar-bg {
        height: 8px;
        background: #f1f5f9;
        border-radius: 10px;
        overflow: hidden;
    }
    
    .progress-bar-fill {
        height: 100%;
        background: linear-gradient(90deg, #ec4899, #db2777);
        border-radius: 10px;
        transition: width 0.5s ease;
    }
    
    .coupon-actions {
        display: flex;
        gap: 12px;
    }
    
    .action-btn {
        flex: 1;
        padding: 12px;
        border-radius: 12px;
        border: none;
        font-weight: 600;
        font-size: 0.85rem;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        transition: all 0.3s ease;
        text-decoration: none;
    }
    
    .action-btn.edit {
        background: #f8fafc;
        color: var(--text);
    }
    
    .action-btn.edit:hover {
        background: #3b82f6;
        color: white;
    }
    
    .action-btn.delete {
        background: rgba(239, 68, 68, 0.1);
        color: #ef4444;
    }
    
    .action-btn.delete:hover {
        background: #ef4444;
        color: white;
    }
    
    /* Empty State */
    .empty-state {
        text-align: center;
        padding: 80px 20px;
        background: #ffffff;
        border-radius: 24px;
        border: 2px dashed rgba(236, 72, 153, 0.2);
    }
    
    .empty-icon {
        width: 120px;
        height: 120px;
        border-radius: 50%;
        background: linear-gradient(135deg, rgba(236, 72, 153, 0.1), rgba(219, 39, 119, 0.05));
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0 auto 24px;
        font-size: 3rem;
        color: #ec4899;
    }
    
    .empty-title { font-size: 1.5rem; font-weight: 700; color: var(--text); margin-bottom: 12px; }
    .empty-text { color: var(--muted); font-size: 1rem; margin-bottom: 24px; }
    
    .pagination-wrapper { margin-top: 32px; display: flex; justify-content: center; }
    
    /* Animations */
    .fade-in { animation: fadeIn 0.5s ease forwards; }
    @keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
    .stagger-1 { animation-delay: 0.1s; }
    .stagger-2 { animation-delay: 0.2s; }
    .stagger-3 { animation-delay: 0.3s; }
    .stagger-4 { animation-delay: 0.4s; }
</style>
@endpush

@section('content')
<div class="page-wrapper">
    <div class="coupons-hero fade-in">
        <div class="row align-items-center">
            <div class="col-lg-8">
                <p class="text-uppercase mb-2" style="font-size: 0.75rem; letter-spacing: 2px; color: #ec4899; font-weight: 600;">
                    <i class="bi bi-ticket-perforated me-2"></i>Gestion promotions
                </p>
                <h1 class="hero-title">Mes Coupons de Réduction</h1>
                <p class="hero-subtitle">Créez des codes promo pour booster vos ventes</p>
            </div>
            <div class="col-lg-4 text-lg-end mt-3 mt-lg-0">
                <a href="{{ route('seller.coupons.create') }}" class="btn-create">
                    <i class="bi bi-plus-lg"></i>
                    Créer un coupon
                </a>
            </div>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show mb-4" role="alert" style="border-radius: 16px; border: none; background: linear-gradient(135deg, rgba(34, 197, 94, 0.1), rgba(22, 163, 74, 0.05)); color: #16a34a;">
            <i class="bi bi-check-circle-fill me-2"></i>{{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <div class="stats-grid">
        <div class="stat-box fade-in stagger-1">
            <div class="stat-icon"><i class="bi bi-ticket-perforated"></i></div>
            <div class="stat-label">Total coupons</div>
            <div class="stat-value">{{ $stats['total'] ?? $coupons->total() ?? 0 }}</div>
        </div>
        <div class="stat-box green fade-in stagger-2">
            <div class="stat-icon"><i class="bi bi-check-circle"></i></div>
            <div class="stat-label">Actifs</div>
            <div class="stat-value">{{ $stats['active'] ?? 0 }}</div>
        </div>
        <div class="stat-box blue fade-in stagger-3">
            <div class="stat-icon"><i class="bi bi-people"></i></div>
            <div class="stat-label">Utilisations</div>
            <div class="stat-value">{{ $stats['total_uses'] ?? 0 }}</div>
        </div>
        <div class="stat-box orange fade-in stagger-4">
            <div class="stat-icon"><i class="bi bi-cash-coin"></i></div>
            <div class="stat-label">Réductions accordées</div>
            <div class="stat-value">{{ number_format($stats['total_discount'] ?? 0, 0, ',', ' ') }}</div>
        </div>
    </div>

    @if(isset($coupons) && $coupons->count() > 0)
    <div class="coupons-grid">
        @foreach($coupons as $index => $coupon)
        @php
            $isExpired = $coupon->expires_at && $coupon->expires_at->isPast();
            $isActive = $coupon->is_active && !$isExpired;
            $usagePercent = $coupon->usage_limit ? min(100, ($coupon->used_count / $coupon->usage_limit) * 100) : 0;
        @endphp
        <div class="coupon-card {{ $isExpired ? 'expired' : ($isActive ? '' : 'inactive') }} fade-in" style="animation-delay: {{ ($index % 4) * 0.1 }}s">
            <div class="coupon-header">
                <span class="coupon-badge {{ $isExpired ? 'expired' : ($isActive ? 'active' : 'inactive') }}">
                    {{ $isExpired ? 'Expiré' : ($isActive ? 'Actif' : 'Inactif') }}
                </span>
                <div class="coupon-code" onclick="navigator.clipboard.writeText('{{ $coupon->code }}'); alert('Code copié!')">
                    {{ $coupon->code }}
                    <i class="bi bi-clipboard"></i>
                </div>
                <div class="coupon-discount">
                    @if($coupon->type === 'percentage')
                        {{ $coupon->value }}% <span>de réduction</span>
                    @else
                        {{ number_format($coupon->value, 0, ',', ' ') }} <span>XOF</span>
                    @endif
                </div>
            </div>
            <div class="coupon-body">
                <div class="coupon-info">
                    <div class="info-item">
                        <div class="info-icon"><i class="bi bi-calendar3"></i></div>
                        <div class="info-text">
                            <label>Date d'expiration</label>
                            <span>{{ $coupon->expires_at ? $coupon->expires_at->format('d/m/Y') : 'Illimité' }}</span>
                        </div>
                    </div>
                    <div class="info-item">
                        <div class="info-icon"><i class="bi bi-cart3"></i></div>
                        <div class="info-text">
                            <label>Min. commande</label>
                            <span>{{ $coupon->min_amount ? number_format($coupon->min_amount, 0, ',', ' ').' XOF' : 'Aucun' }}</span>
                        </div>
                    </div>
                </div>
                @if($coupon->usage_limit)
                <div class="coupon-progress">
                    <div class="progress-header">
                        <span class="progress-label">Utilisations</span>
                        <span class="progress-value">{{ $coupon->used_count ?? 0 }} / {{ $coupon->usage_limit }}</span>
                    </div>
                    <div class="progress-bar-bg">
                        <div class="progress-bar-fill" style="width: {{ $usagePercent }}%"></div>
                    </div>
                </div>
                @else
                <div class="coupon-progress">
                    <div class="progress-header">
                        <span class="progress-label">Utilisations</span>
                        <span class="progress-value">{{ $coupon->used_count ?? 0 }} (illimité)</span>
                    </div>
                </div>
                @endif
                <div class="coupon-actions">
                    <form action="{{ route('seller.coupons.toggle', $coupon) }}" method="POST" class="d-inline">
                        @csrf
                        <button type="submit" class="action-btn edit">
                            <i class="bi bi-toggle-{{ $coupon->is_active ? 'on' : 'off' }}"></i> {{ $coupon->is_active ? 'Désactiver' : 'Activer' }}
                        </button>
                    </form>
                    <form action="{{ route('seller.coupons.destroy', $coupon) }}" method="POST" onsubmit="return confirm('Supprimer ce coupon ?')">
                        @csrf @method('DELETE')
                        <button type="submit" class="action-btn delete">
                            <i class="bi bi-trash"></i> Supprimer
                        </button>
                    </form>
                </div>
            </div>
        </div>
        @endforeach
    </div>
    @if($coupons->hasPages())
    <div class="pagination-wrapper">{{ $coupons->links() }}</div>
    @endif
    @else
    <div class="empty-state fade-in">
        <div class="empty-icon"><i class="bi bi-ticket-perforated"></i></div>
        <h3 class="empty-title">Aucun coupon</h3>
        <p class="empty-text">Créez votre premier code promo pour attirer plus de clients</p>
        <a href="{{ route('seller.coupons.create') }}" class="btn-create">
            <i class="bi bi-plus-lg"></i>
            Créer mon premier coupon
        </a>
    </div>
    @endif
</div>
@endsection
