@extends('layouts.default')
@section('title', $page_title)
@section('content')
<div class="container-fluid">
    @include('admin.partials.page-header', ['actions' => new \Illuminate\Support\HtmlString('
        <a href="'.route('admin.sync.quality', ['platform' => $platform, 'client_id' => $connection->client_id, 'connection_id' => $connection->id]).'" class="btn btn-outline-warning me-2">Kalite Kontrolü</a>
        <a href="'.route('admin.integrations.preview.index', ['platform' => $platform, 'client_id' => $connection->client_id, 'connection_id' => $connection->id]).'" class="btn btn-outline-info me-2">Veri Önizle</a>
        <a href="'.route('admin.integrations.discovery.index', ['platform' => $platform, 'client_id' => $connection->client_id, 'connection_id' => $connection->id]).'" class="btn btn-outline-success me-2">Hesap Keşfi</a>
        <a href="'.route('admin.accounts.'.str_replace('_', '-', $platform).'.edit', $connection).'" class="btn btn-outline-primary me-2">Düzenle</a>
        <form action="'.route('admin.accounts.connections.test', $connection).'" method="POST" class="d-inline">'.csrf_field().'<button class="btn btn-primary">Bağlantıyı Test Et</button></form>
    ')])

    <div class="row">
        <div class="col-xl-8">
            <div class="card">
                <div class="card-header"><h4 class="card-title mb-0">Bağlantı Özeti</h4></div>
                <div class="card-body">
                    <dl class="row mb-0">
                        <dt class="col-md-3">Platform</dt><dd class="col-md-9">{{ strtoupper(str_replace('_', ' ', $connection->platform)) }}</dd>
                        <dt class="col-md-3">Müşteri</dt><dd class="col-md-9">{{ $connection->client?->company_name }}</dd>
                        <dt class="col-md-3">Bağlantı</dt><dd class="col-md-9">{{ $connection->connection_name }}</dd>
                        <dt class="col-md-3">Hesap ID</dt><dd class="col-md-9">{{ $connection->account_id ?: '-' }}</dd>
                        <dt class="col-md-3">Hesap Adı</dt><dd class="col-md-9">{{ $connection->account_name ?: '-' }}</dd>
                        <dt class="col-md-3">Durum</dt><dd class="col-md-9">@include('admin.partials.status-badge', ['status' => $connection->status])</dd>
                        <dt class="col-md-3">Son Test/Senkron</dt><dd class="col-md-9">{{ $connection->last_sync_at?->format('d.m.Y H:i') ?? '-' }}</dd>
                        <dt class="col-md-3">Son Hata</dt><dd class="col-md-9">{{ $connection->last_error ?: '-' }}</dd>
                    </dl>
                </div>
            </div>

            <div class="card">
                <div class="card-header"><h4 class="card-title mb-0">Manuel Veri Senkronizasyonu</h4></div>
                <div class="card-body">
                    <form action="{{ route('admin.sync.connection', $connection) }}" method="POST" class="row g-3 align-items-end">
                        @csrf
                        <div class="col-md-4">
                            <label class="form-label">Başlangıç</label>
                            <input type="date" name="from" class="form-control" value="{{ now()->subDay()->toDateString() }}">
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Bitiş</label>
                            <input type="date" name="to" class="form-control" value="{{ now()->subDay()->toDateString() }}">
                        </div>
                        <div class="col-md-4">
                            <button class="btn btn-primary w-100">Snapshot Çek</button>
                        </div>
                    </form>

                    <div class="border-top pt-3 mt-3">
                        <form action="{{ route('admin.integrations.preview.connection', $connection) }}" method="POST" class="row g-3 align-items-end">
                            @csrf
                            <div class="col-md-4">
                                <label class="form-label">Önizleme Başlangıç</label>
                                <input type="date" name="from" class="form-control" value="{{ now()->subDays(7)->toDateString() }}">
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">Önizleme Bitiş</label>
                                <input type="date" name="to" class="form-control" value="{{ now()->subDay()->toDateString() }}">
                            </div>
                            <div class="col-md-2">
                                <label class="form-label">Limit</label>
                                <input type="number" name="limit" class="form-control" value="10" min="1" max="50">
                            </div>
                            <div class="col-md-2">
                                <button class="btn btn-outline-info w-100">Önizle</button>
                            </div>
                        </form>
                    </div>

                    @php($lastSync = $connection->meta['last_sync_result'] ?? null)
                    @if($lastSync)
                        <div class="alert alert-light border mt-3 mb-0">
                            <strong>Son senkron sonucu:</strong> {{ $lastSync['message'] ?? '-' }}
                            <pre class="mb-0 mt-2" style="white-space: pre-wrap;">{{ json_encode($lastSync['counts'] ?? [], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) }}</pre>
                        </div>
                    @endif
                </div>
            </div>

            <div class="card">
                <div class="card-header"><h4 class="card-title mb-0">Son Test Verisi</h4></div>
                <div class="card-body">
                    @php($lastPreview = $connection->meta['last_preview_result'] ?? null)
                    @php($lastData = $lastPreview ?: ($connection->meta['last_test_data'] ?? null))
                    @if($lastData)
                        <pre class="bg-light p-3 rounded mb-0" style="white-space: pre-wrap;">{{ json_encode($lastData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) }}</pre>
                    @else
                        <p class="text-muted mb-0">Henüz bağlantı testi yapılmadı.</p>
                    @endif
                </div>
            </div>
        </div>

        <div class="col-xl-4">
            <div class="card">
                <div class="card-header"><h4 class="card-title mb-0">Token Kasası</h4></div>
                <div class="card-body">
                    @if($connection->secureCredential)
                        <p class="mb-1"><strong>{{ $connection->secureCredential->name }}</strong></p>
                        <p class="text-muted mb-2">Son kontrol: {{ $connection->secureCredential->last_checked_at?->format('d.m.Y H:i') ?? '-' }}</p>
                        <p class="mb-3">Durum: @include('admin.partials.status-badge', ['status' => $connection->secureCredential->is_active ? 'active' : 'error'])</p>
                        @if(!empty($maskedPayload))
                            <ul class="list-group mb-3">
                                @foreach($maskedPayload as $key => $value)
                                    <li class="list-group-item d-flex justify-content-between"><span>{{ $key }}</span><small>{{ $value }}</small></li>
                                @endforeach
                            </ul>
                        @endif
                        @if($connection->secureCredential->last_error)
                            <div class="alert alert-danger">{{ $connection->secureCredential->last_error }}</div>
                        @endif
                    @else
                        <p class="text-muted">Bu bağlantı için token kasası henüz oluşmadı.</p>
                    @endif

                    @if(in_array($platform, ['google_ads', 'search_console', 'ga4'], true) && $connection->secureCredential)
                        <a href="{{ route('admin.integrations.google.redirect', $connection) }}" class="btn btn-outline-primary w-100 mb-2">Google OAuth Yetkilendir</a>
                        <small class="text-muted d-block mb-3">Callback URL: {{ route('admin.integrations.google.callback') }}</small>
                    @endif

                    @if($connection->secureCredential)
                        <a href="{{ route('admin.integrations.discovery.index', ['platform' => $platform, 'client_id' => $connection->client_id, 'connection_id' => $connection->id]) }}" class="btn btn-outline-success w-100">Hesap / Mülk Keşfi Yap</a>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
