@extends('layouts.admin', ['title' => 'مزودو الشحن | RaseedNow'])

@section('content')
    <section class="lc-page-shell">
        <div class="section-heading">
            <div><span>Provider Hub</span><h1>مزودو الشحن والتكاملات</h1></div>
            <a class="secondary-button" href="{{ route('admin.fulfillment.index') }}">مركز التشغيل</a>
        </div>

        @if($errors->any())<div class="mt-4 rounded-xl border border-red-200 bg-red-50 p-3 text-sm font-bold text-red-700">{{ $errors->first() }}</div>@endif

        <div class="mt-6 grid gap-4 md:grid-cols-4">
            <div class="ac-kpi" style="--accent:#14b8a6"><span>المزودون</span><strong>{{ $summary['providers'] }}</strong></div>
            <div class="ac-kpi" style="--accent:#16a34a"><span>نشط</span><strong>{{ $summary['active'] }}</strong></div>
            <div class="ac-kpi" style="--accent:#2563eb"><span>ربط منتجات</span><strong>{{ $summary['mappings'] }}</strong></div>
            <div class="ac-kpi" style="--accent:#ef4444"><span>فشل آخر 24 ساعة</span><strong>{{ $summary['failedLogs'] }}</strong></div>
        </div>

        <div class="mt-6 grid gap-6 xl:grid-cols-[0.9fr_1.1fr]">
            <div class="surface p-6">
                <div class="ac-panel-head"><h2>إضافة مزود</h2><span>استخدم Mock للتجربة وLive بعد إدخال المفاتيح</span></div>
                <form method="POST" action="{{ route('admin.providers.store') }}" class="mt-4 grid gap-3">
                    @csrf
                    <label class="form-field"><span>اسم المزود</span><input name="name" value="Midasbuy PUBG" required></label>
                    <label class="form-field"><span>كود داخلي</span><input name="code" value="midasbuy_pubg" required></label>
                    <label class="form-field"><span>النوع</span><select name="type"><option value="midasbuy">Midasbuy / PUBG</option><option value="generic">Generic HTTP</option></select></label>
                    <label class="form-field"><span>الوضع</span><select name="mode"><option value="mock">Mock للتجربة</option><option value="live">Live حقيقي</option></select></label>
                    <label class="form-field"><span>Base URL</span><input name="base_url" value="https://midasbuy-api.com/api/v1"></label>
                    <label class="form-field"><span>Auth Header</span><input name="auth_header" value="X-Api-Key" required></label>
                    <label class="form-field"><span>اسم ENV للمفتاح</span><input name="api_key_env" value="MIDASBUY_API_KEY"></label>
                    <label class="form-field"><span>API Key مباشر اختياري</span><input name="api_key" autocomplete="off" placeholder="يفضل ENV"></label>
                    <div class="grid gap-3 md:grid-cols-2">
                        <label class="form-field"><span>Timeout</span><input name="timeout_seconds" type="number" value="20" min="3" max="60"></label>
                        <label class="form-field"><span>Retries</span><input name="retry_attempts" type="number" value="1" min="0" max="5"></label>
                    </div>
                    <div class="grid gap-2 rounded-2xl border border-[#e5edf0] bg-white p-4">
                        <label class="flex items-center gap-2 text-sm font-black text-[#003d36]"><input name="is_active" type="checkbox" value="1" checked> نشط</label>
                        <label class="flex items-center gap-2 text-sm font-black text-[#003d36]"><input name="player_lookup_enabled" type="checkbox" value="1" checked> تفعيل فحص اللاعب</label>
                        <label class="flex items-center gap-2 text-sm font-black text-[#003d36]"><input name="code_check_enabled" type="checkbox" value="1" checked> تفعيل فحص الكود</label>
                        <label class="flex items-center gap-2 text-sm font-black text-[#003d36]"><input name="async_tasks_enabled" type="checkbox" value="1"> تنفيذ Async عند الحاجة</label>
                    </div>
                    <button class="primary-button" type="submit">إضافة المزود</button>
                </form>
            </div>

            <div class="surface p-6">
                <div class="ac-panel-head"><h2>المزودون الحاليون</h2><span>الحالة، الوضع، وعدد الربوط</span></div>
                <div class="grid gap-4">
                    @forelse($providers as $provider)
                        <div class="rounded-2xl border border-[#e5edf0] bg-[#f8fafc] p-4">
                            <form method="POST" action="{{ route('admin.providers.update', $provider) }}" class="grid gap-3 md:grid-cols-3">
                                @csrf
                                @method('PATCH')
                                <label class="form-field"><span>الاسم</span><input name="name" value="{{ $provider->name }}"></label>
                                <label class="form-field"><span>النوع</span><select name="type"><option value="midasbuy" @selected($provider->type === 'midasbuy')>Midasbuy</option><option value="generic" @selected($provider->type === 'generic')>Generic</option></select></label>
                                <label class="form-field"><span>الوضع</span><select name="mode"><option value="mock" @selected($provider->mode === 'mock')>Mock</option><option value="live" @selected($provider->mode === 'live')>Live</option></select></label>
                                <label class="form-field md:col-span-2"><span>Base URL</span><input name="base_url" value="{{ $provider->base_url }}"></label>
                                <label class="form-field"><span>Auth Header</span><input name="auth_header" value="{{ $provider->auth_header }}"></label>
                                <label class="form-field"><span>ENV</span><input name="api_key_env" value="{{ $provider->api_key_env }}"></label>
                                <label class="form-field"><span>API Key جديد</span><input name="api_key" autocomplete="off" placeholder="اتركه فارغًا للإبقاء"></label>
                                <label class="form-field"><span>Timeout</span><input name="timeout_seconds" type="number" value="{{ $provider->timeout_seconds }}"></label>
                                <label class="form-field"><span>Retries</span><input name="retry_attempts" type="number" value="{{ $provider->retry_attempts }}"></label>
                                <label class="flex items-center gap-2 text-sm font-black text-[#003d36]"><input name="is_active" type="checkbox" value="1" @checked($provider->is_active)> نشط</label>
                                <div class="flex flex-wrap items-center gap-2 md:col-span-3">
                                    <span class="ac-badge">{{ $provider->code }}</span>
                                    <span class="ac-badge">{{ $provider->health_status }}</span>
                                    <span class="ac-badge">{{ $provider->mappings_count }} ربط</span>
                                    <button class="primary-button" type="submit">حفظ</button>
                            </form>
                                    <form method="POST" action="{{ route('admin.providers.health', $provider) }}">
                                        @csrf
                                        <button class="secondary-button" type="submit">اختبار الصحة</button>
                                    </form>
                                </div>
                        </div>
                    @empty
                        <div class="ac-empty">لا توجد مزودات بعد.</div>
                    @endforelse
                </div>
            </div>
        </div>

        <div class="surface mt-6 p-6">
            <div class="ac-panel-head"><h2>ربط منتج بمزود</h2><span>حدد المنتج وطريقة تنفيذ المزود وعدد الأكواد المطلوبة</span></div>
            <form method="POST" action="{{ route('admin.providers.mappings.store') }}" class="mt-4 grid gap-3 md:grid-cols-6">
                @csrf
                <label class="form-field md:col-span-2"><span>المزود</span><select name="provider_id" required>@foreach($providers as $provider)<option value="{{ $provider->id }}">{{ $provider->name }} / {{ $provider->mode }}</option>@endforeach</select></label>
                <label class="form-field md:col-span-2"><span>المنتج</span><select name="product_variant_id" required>@foreach($products as $product)<option value="{{ $product->id }}">{{ $product->game?->name_ar }} — {{ $product->name_ar }} — {{ $product->sku }}</option>@endforeach</select></label>
                <label class="form-field"><span>Provider SKU</span><input name="provider_sku" placeholder="اختياري"></label>
                <label class="form-field"><span>النوع</span><select name="fulfillment_type"><option value="pubg_uc_code">PUBG UC Code</option><option value="generic_http">Generic HTTP</option><option value="manual_provider">Manual Provider</option></select></label>
                <label class="form-field"><span>الوضع</span><select name="mode"><option value="single">Single</option><option value="bulk">Bulk</option><option value="async">Async Task</option></select></label>
                <label class="form-field"><span>أكواد لكل طلب</span><input name="codes_per_order" type="number" min="1" max="5" value="1"></label>
                <label class="flex items-center gap-2 text-sm font-black text-[#003d36]"><input name="is_active" type="checkbox" value="1" checked> نشط</label>
                <button class="primary-button md:col-span-2" type="submit">حفظ الربط</button>
            </form>
        </div>

        <div class="surface mt-6 overflow-hidden p-6">
            <div class="ac-panel-head"><h2>خرائط المنتجات</h2></div>
            <div class="ac-table-wrap">
                <table class="ac-table">
                    <thead><tr><th>المزود</th><th>المنتج</th><th>SKU</th><th>النوع</th><th>الوضع</th><th>أكواد</th><th>الحالة</th><th>إجراءات</th></tr></thead>
                    <tbody>
                    @forelse($mappings as $mapping)
                        <tr>
                            <td>{{ $mapping->provider?->name }}</td>
                            <td>{{ $mapping->productVariant?->game?->name_ar }} — {{ $mapping->productVariant?->name_ar }}</td>
                            <td>{{ $mapping->provider_sku ?: $mapping->productVariant?->sku }}</td>
                            <td>{{ $mapping->fulfillment_type }}</td>
                            <td>{{ $mapping->mode }}</td>
                            <td>{{ $mapping->codes_per_order }}</td>
                            <td><span class="ac-badge {{ $mapping->is_active ? 'green' : 'red' }}">{{ $mapping->is_active ? 'نشط' : 'متوقف' }}</span></td>
                            <td>
                                <form method="POST" action="{{ route('admin.providers.mappings.destroy', $mapping) }}">
                                    @csrf
                                    @method('DELETE')
                                    <button class="secondary-button" type="submit">حذف</button>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="8"><div class="ac-empty">لا يوجد ربط منتجات بعد.</div></td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
            <div class="mt-4">{{ $mappings->links() }}</div>
        </div>
    </section>
@endsection
