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

    <div class="row">
        <div class="col-xl-4">
            <div class="card">
                <div class="card-header">
                    <h4 class="card-title mb-0">Keşif Filtresi</h4>
                </div>
                <div class="card-body">
                    <form method="GET" action="{{ route('admin.integrations.discovery.index') }}">
                        <div class="mb-3">
                            <label class="form-label">Platform</label>
                            <select name="platform" class="form-control default-select" onchange="this.form.submit()">
                                <option value="">Tüm platformlar</option>
                                @foreach(['google_ads' => 'Google Ads', 'meta_ads' => 'Meta Ads', 'search_console' => 'Search Console', 'ga4' => 'GA4'] as $value => $label)
                                    <option value="{{ $value }}" @selected($platform === $value)>{{ $label }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Müşteri</label>
                            <select name="client_id" class="form-control default-select" onchange="this.form.submit()">
                                <option value="">Tüm müşteriler</option>
                                @foreach($clients as $client)
                                    <option value="{{ $client->id }}" @selected((int) $clientId === (int) $client->id)>{{ $client->company_name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Bağlantı</label>
                            <select name="connection_id" class="form-control default-select">
                                @forelse($connections as $connection)
                                    <option value="{{ $connection->id }}" @selected($selectedConnection && $selectedConnection->id === $connection->id)>
                                        #{{ $connection->id }} · {{ strtoupper(str_replace('_', ' ', $connection->platform)) }} · {{ $connection->connection_name }}
                                    </option>
                                @empty
                                    <option value="">Bağlantı yok</option>
                                @endforelse
                            </select>
                        </div>
                        <button class="btn btn-outline-primary w-100">Seçimi Getir</button>
                    </form>
                </div>
            </div>

            @if($selectedConnection)
                <div class="card">
                    <div class="card-header">
                        <h4 class="card-title mb-0">Seçili Bağlantı</h4>
                    </div>
                    <div class="card-body">
                        <p class="mb-1"><strong>{{ $selectedConnection->connection_name }}</strong></p>
                        <p class="text-muted mb-2">{{ $selectedConnection->client?->company_name }} · {{ strtoupper(str_replace('_', ' ', $selectedConnection->platform)) }}</p>
                        <p class="mb-2">Mevcut hesap: <strong>{{ $selectedConnection->account_id ?: '-' }}</strong></p>
                        <p class="mb-3">Durum: @include('admin.partials.status-badge', ['status' => $selectedConnection->status])</p>

                        @if($selectedConnection->secureCredential)
                            <form method="POST" action="{{ route('admin.integrations.discovery.discover', $selectedConnection) }}">
                                @csrf
                                <div class="mb-3">
                                    <label class="form-label">Liste limiti</label>
                                    <input type="number" name="limit" min="1" max="200" value="50" class="form-control">
                                </div>
                                <button class="btn btn-primary w-100">Hesapları / Mülkleri Keşfet</button>
                            </form>
                        @else
                            <div class="alert alert-warning mb-0">Bu bağlantıda token kasası yok. Önce bağlantıyı düzenleyip token bilgilerini kaydetmelisin.</div>
                        @endif
                    </div>
                </div>
            @endif
        </div>

        <div class="col-xl-8">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h4 class="card-title mb-0">Bulunan Hesaplar / Mülkler</h4>
                    @if($result)
                        <span class="badge light badge-{{ $result['ok'] ? 'success' : 'danger' }}">{{ $result['ok'] ? 'Başarılı' : 'Hata' }}</span>
                    @endif
                </div>
                <div class="card-body">
                    @if($result)
                        <div class="alert alert-{{ $result['ok'] ? 'success' : 'danger' }}">
                            {{ $result['message'] }}
                        </div>

                        @if(!empty($result['accounts']))
                            <div class="table-responsive">
                                <table class="table table-responsive-md align-middle">
                                    <thead>
                                        <tr>
                                            <th>Hesap / Mülk</th>
                                            <th>ID</th>
                                            <th>Para Birimi</th>
                                            <th>Zaman Dilimi</th>
                                            <th>Kaynak</th>
                                            <th class="text-end">İşlem</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach($result['accounts'] as $account)
                                            <tr>
                                                <td>
                                                    <strong>{{ $account['account_name'] ?? '-' }}</strong>
                                                    <small class="d-block text-muted">{{ strtoupper(str_replace('_', ' ', $account['platform'] ?? '')) }}</small>
                                                </td>
                                                <td>{{ $account['account_id'] ?? '-' }}</td>
                                                <td>{{ $account['currency_code'] ?? '-' }}</td>
                                                <td>{{ $account['timezone'] ?? '-' }}</td>
                                                <td><small>{{ $account['resource_name'] ?? '-' }}</small></td>
                                                <td class="text-end">
                                                    @if($selectedConnection)
                                                        <form method="POST" action="{{ route('admin.integrations.discovery.attach', $selectedConnection) }}" class="d-inline">
                                                            @csrf
                                                            <input type="hidden" name="account_id" value="{{ $account['account_id'] ?? '' }}">
                                                            <input type="hidden" name="account_name" value="{{ $account['account_name'] ?? '' }}">
                                                            <input type="hidden" name="currency_code" value="{{ $account['currency_code'] ?? '' }}">
                                                            <input type="hidden" name="timezone" value="{{ $account['timezone'] ?? '' }}">
                                                            <input type="hidden" name="status" value="{{ $account['status'] ?? 'active' }}">
                                                            <input type="hidden" name="resource_name" value="{{ $account['resource_name'] ?? '' }}">
                                                            <input type="hidden" name="meta" value='@json($account["meta"] ?? [])'>
                                                            <button class="btn btn-xs btn-primary">Bu Hesabı Bağla</button>
                                                        </form>
                                                    @endif
                                                </td>
                                            </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        @else
                            <p class="text-muted mb-0">Keşif çalıştı ama listelenecek hesap/mülk bulunamadı. Yetki kapsamını, token izinlerini ve bağlı Business/Google hesabını kontrol et.</p>
                        @endif
                    @else
                        <div class="text-center py-5">
                            <h5>Henüz keşif çalıştırılmadı</h5>
                            <p class="text-muted mb-0">Soldan bağlantıyı seçip “Hesapları / Mülkleri Keşfet” butonuna bas.</p>
                        </div>
                    @endif
                </div>
            </div>

            @if($selectedConnection && !empty($selectedConnection->meta['last_discovery']))
                <div class="card">
                    <div class="card-header"><h4 class="card-title mb-0">Son Keşif Özeti</h4></div>
                    <div class="card-body">
                        <pre class="bg-light p-3 rounded mb-0" style="white-space: pre-wrap;">{{ json_encode($selectedConnection->meta['last_discovery'], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) }}</pre>
                    </div>
                </div>
            @endif
        </div>
    </div>
</div>
@endsection
