@extends('layouts.default')
@section('title', 'API SDK Durumu')
@section('content')
<div class="container-fluid">
    @include('admin.partials.page-header')

    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
    @endif
    @if(session('error'))
        <div class="alert alert-danger">{{ session('error') }}</div>
    @endif

    <div class="row">
        <div class="col-xl-3 col-sm-6">
            <div class="card"><div class="card-body"><span class="text-muted">SDK Paketleri</span><h3 class="mb-0">{{ $summary['packages'] }}</h3></div></div>
        </div>
        <div class="col-xl-3 col-sm-6">
            <div class="card"><div class="card-body"><span class="text-muted">Kurulu</span><h3 class="mb-0 text-success">{{ $summary['installed'] }}</h3></div></div>
        </div>
        <div class="col-xl-3 col-sm-6">
            <div class="card"><div class="card-body"><span class="text-muted">Eksik</span><h3 class="mb-0 text-warning">{{ $summary['missing'] }}</h3></div></div>
        </div>
        <div class="col-xl-3 col-sm-6">
            <div class="card"><div class="card-body"><span class="text-muted">Extension Hatası</span><h3 class="mb-0 text-danger">{{ $summary['extension_errors'] }}</h3></div></div>
        </div>
    </div>

    @if(!empty($warnings))
        <div class="alert alert-warning">
            @foreach($warnings as $warning)
                <div>{{ $warning }}</div>
            @endforeach
        </div>
    @else
        <div class="alert alert-success">Zorunlu PHP extension kontrolleri başarılı görünüyor.</div>
    @endif

    <div class="card">
        <div class="card-header border-0 pb-0">
            <h4 class="card-title mb-0">Composer Paketleri</h4>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table align-middle">
                    <thead>
                        <tr>
                            <th>Durum</th>
                            <th>Paket</th>
                            <th>Kullanım</th>
                            <th>Kurulum</th>
                            <th>Not</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($packages as $package)
                            <tr>
                                <td>
                                    <span class="badge light badge-{{ $package['status'] ? 'success' : 'warning' }}">
                                        {{ $package['status'] ? 'KURULU' : 'EKSİK' }}
                                    </span>
                                </td>
                                <td>
                                    <strong>{{ $package['name'] }}</strong><br>
                                    <code>{{ $package['package'] }}</code>
                                </td>
                                <td>{{ $package['required_for'] }}</td>
                                <td><code>{{ $package['install'] }}</code></td>
                                <td class="text-muted">{{ $package['notes'] }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="card">
        <div class="card-header border-0 pb-0">
            <h4 class="card-title mb-0">PHP Extension Kontrolleri</h4>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table align-middle">
                    <thead>
                        <tr>
                            <th>Durum</th>
                            <th>Extension</th>
                            <th>Zorunlu</th>
                            <th>Not</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($extensions as $extension)
                            <tr>
                                <td>
                                    <span class="badge light badge-{{ $extension['status'] ? 'success' : (($extension['required'] ?? false) ? 'danger' : 'info') }}">
                                        {{ $extension['status'] ? 'AKTİF' : 'PASİF' }}
                                    </span>
                                </td>
                                <td><strong>{{ $extension['name'] }}</strong><br><code>{{ $extension['extension'] }}</code></td>
                                <td>{{ $extension['required'] ? 'Evet' : 'Hayır' }}</td>
                                <td class="text-muted">{{ $extension['notes'] }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="card">
        <div class="card-header border-0 pb-0">
            <h4 class="card-title mb-0">Kurulum Komutları</h4>
        </div>
        <div class="card-body">
            <p class="mb-2">Tüm resmi SDK paketlerini kurmak için:</p>
            <pre><code>{{ $commands['all'] }}</code></pre>
            <p class="mb-2">Kurulumdan sonra:</p>
            <pre class="mb-0"><code>{{ $commands['clear'] }}</code></pre>
        </div>
    </div>

    <div class="card">
        <div class="card-header border-0 pb-0">
            <h4 class="card-title mb-0">SDK ile Bağlantı Testi</h4>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table align-middle">
                    <thead>
                        <tr>
                            <th>Müşteri</th>
                            <th>Platform</th>
                            <th>Bağlantı</th>
                            <th>Durum</th>
                            <th>Son SDK Testi</th>
                            <th class="text-end">İşlem</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($connections as $connection)
                            @php($lastProbe = $connection->meta['last_sdk_probe_result'] ?? null)
                            <tr>
                                <td>{{ $connection->client->company_name ?? '-' }}</td>
                                <td><span class="badge light badge-info">{{ $connection->platform }}</span></td>
                                <td>
                                    <strong>{{ $connection->connection_name }}</strong><br>
                                    <span class="text-muted">{{ $connection->account_id }}</span>
                                </td>
                                <td>{{ $connection->status }}</td>
                                <td>
                                    @if($lastProbe)
                                        <span class="badge light badge-{{ ($lastProbe['ok'] ?? false) ? 'success' : 'danger' }}">
                                            {{ ($lastProbe['ok'] ?? false) ? 'Başarılı' : 'Hata' }}
                                        </span>
                                        <div class="text-muted small">{{ $lastProbe['message'] ?? '' }}</div>
                                    @else
                                        <span class="text-muted">Henüz SDK testi yok</span>
                                    @endif
                                </td>
                                <td class="text-end">
                                    <form action="{{ route('admin.system.api-sdk.probe', $connection) }}" method="post" class="d-inline">
                                        @csrf
                                        <button type="submit" class="btn btn-sm btn-primary">SDK Test Et</button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="6" class="text-center text-muted">Henüz platform bağlantısı yok.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection
