@extends('admin.layout')

@section('title', 'Plugins - Marketplace')
@section('header', 'Plugins')

@section('content')
@if($errors->any())
    <div class="mb-6 p-4 bg-red-50 border border-red-200 text-red-800 rounded-xl">
        @foreach($errors->all() as $error)
            <p>{{ $error }}</p>
        @endforeach
    </div>
@endif
<div class="flex justify-between items-center mb-6">
    <div>
        <h1 class="text-2xl font-bold text-slate-800">Plugins</h1>
        <p class="text-slate-600">Section +, Forum, e-commerce... Étendez les fonctionnalités de votre site.</p>
    </div>
    <a href="{{ route('admin.marketplace.index') }}" class="text-indigo-600 hover:underline">← Marketplace</a>
</div>

<div class="mb-6 p-4 bg-indigo-50 border border-indigo-100 rounded-lg">
    <h3 class="font-medium text-indigo-800 mb-2">Installer un plugin personnalisé (ZIP)</h3>
    <p class="text-sm text-indigo-700 mb-3">Uploadez un fichier ZIP contenant votre plugin. Le ZIP doit contenir un dossier avec plugin.json.</p>
    <form action="{{ route('admin.marketplace.upload-plugin') }}" method="POST" enctype="multipart/form-data" class="flex flex-wrap items-end gap-3">
        @csrf
        <input type="file" name="zip" accept=".zip" required class="block w-full max-w-xs text-sm text-slate-500 file:mr-4 file:py-2 file:px-4 file:rounded-lg file:border-0 file:bg-indigo-100 file:text-indigo-700 hover:file:bg-indigo-200">
        <button type="submit" class="px-4 py-2 bg-indigo-500 text-white rounded-lg text-sm font-medium hover:bg-indigo-600 transition">Installer</button>
    </form>
    @error('zip')
        <p class="text-sm text-red-600 mt-2">{{ $message }}</p>
    @enderror
    @php
        $marketplaceOnly = config('cms.marketplace_only_plugins', []);
        $customPlugins = collect($installedPlugins ?? [])->filter(fn ($p) => ($p['source'] ?? '') === 'custom' && !in_array($p['slug'] ?? '', $marketplaceOnly, true));
    @endphp
    @if($customPlugins->isNotEmpty())
        <p class="text-sm text-indigo-600 mt-3 font-medium">Plugins personnalisés installés :</p>
        <div class="flex flex-wrap gap-2 mt-1">
            @foreach($customPlugins as $plugin)
                <span class="px-3 py-1 bg-indigo-100 text-indigo-800 rounded-full text-sm">{{ $plugin['name'] ?? $plugin['slug'] }}</span>
            @endforeach
        </div>
    @endif
</div>

<div class="grid md:grid-cols-2 lg:grid-cols-3 gap-6 items-start">
    @foreach($catalog ?? [] as $plugin)
        @php
            $purchase = $purchases[$plugin['slug'] ?? ''] ?? null;
            $hasRow = $purchase !== null;
            $revoked = $hasRow && ! $purchase->is_active;
            $adminOn = $hasRow && ($purchase->admin_enabled ?? true);
            $isPurchased = $hasRow;
            $isActiveLocal = $adminOn;
            $isActiveReal = \App\Models\MarketplacePurchase::hasPurchased('plugin', $plugin['slug'] ?? '');
            $licenseInvalid = $hasRow && $purchase->is_active && $adminOn && ! $isActiveReal;
            $canUse = $isActiveReal;
            $showRepurchase = ! $hasRow || $revoked || $licenseInvalid;
        @endphp
        <div class="bg-white rounded-xl shadow-lg overflow-hidden border {{ $isActiveReal ? 'border-emerald-200 ring-2 ring-emerald-100' : 'border-slate-200' }}">
            {{-- Hauteur = ratio intrinsèque du PNG : plus de bande noire (vs cadre 3:2 + object-contain). Exporter en 3:2 pour des cartes alignées. --}}
            <div class="w-full overflow-hidden bg-zinc-950 leading-none">
                <img src="{{ $plugin['screenshot'] }}" alt="{{ $plugin['name'] }}" class="block w-full h-auto">
            </div>
            <div class="p-4">
                <div class="flex items-center gap-2 mb-1">
                    <h3 class="font-semibold text-slate-800">{{ $plugin['name'] }}</h3>
                    @if($isActiveReal)
                        <span class="px-2 py-0.5 bg-emerald-100 text-emerald-700 text-xs font-medium rounded-full">Actif</span>
                    @elseif($licenseInvalid)
                        <span class="px-2 py-0.5 bg-amber-100 text-amber-700 text-xs font-medium rounded-full" title="Licence refusée par le serveur">Licence invalide</span>
                    @elseif($revoked)
                        <span class="px-2 py-0.5 bg-red-100 text-red-700 text-xs font-medium rounded-full">Révoquée</span>
                    @elseif($isPurchased && $purchase->is_active && ! $adminOn)
                        <span class="px-2 py-0.5 bg-slate-100 text-slate-600 text-xs font-medium rounded-full">Désactivé</span>
                    @endif
                </div>
                <p class="text-sm text-slate-500 mt-1">{{ $plugin['description'] }}</p>
                <div class="mt-4 flex items-center justify-between gap-2 flex-wrap">
                    <span class="font-bold text-amber-600"><x-marketplace-price :price="$plugin['price'] ?? 0" /> €</span>
                    @if($canUse)
                        <form action="{{ route('admin.marketplace.activate') }}" method="POST" class="inline">
                            @csrf
                            <input type="hidden" name="type" value="plugin">
                            <input type="hidden" name="slug" value="{{ $plugin['slug'] }}">
                            <input type="hidden" name="active" value="0">
                            <button type="submit" class="px-3 py-1.5 rounded text-sm font-medium bg-slate-200 text-slate-700 hover:bg-slate-300">
                                Désactiver
                            </button>
                        </form>
                    @elseif($hasRow && $purchase->is_active && ! $adminOn)
                        <form action="{{ route('admin.marketplace.activate') }}" method="POST" class="inline">
                            @csrf
                            <input type="hidden" name="type" value="plugin">
                            <input type="hidden" name="slug" value="{{ $plugin['slug'] }}">
                            <input type="hidden" name="active" value="1">
                            <button type="submit" class="px-3 py-1.5 bg-emerald-500 text-white rounded text-sm font-medium hover:bg-emerald-600">
                                Activer
                            </button>
                        </form>
                    @elseif($showRepurchase)
                        <form action="{{ route('admin.marketplace.purchase') }}" method="POST" class="inline">
                            @csrf
                            <input type="hidden" name="type" value="plugin">
                            <input type="hidden" name="slug" value="{{ $plugin['slug'] }}">
                            <button type="submit" class="px-3 py-1.5 bg-amber-500 text-white rounded text-sm font-medium hover:bg-amber-600">
                                {{ ($hasRow && ($revoked || $licenseInvalid)) ? 'Racheter' : 'Acheter' }}
                            </button>
                        </form>
                    @endif
                </div>
            </div>
        </div>
    @endforeach
</div>

<p class="mt-6 text-sm text-slate-500">Paiement sécurisé par Stripe. Une fois acheté, activez le plugin pour l'utiliser sur votre site.</p>
@endsection
