{{-- PRC-1113 — Migrate-from-WHMCS admin dashboard. Faz-1 dispatcher try/catch'e sarar. --}}
@extends('admin.layouts.app')

@section('content')
<div class="container-fluid px-4">
    <x-admin.page-header title="Migrate from WHMCS" subtitle="WHMCS DB → procp tek-yönlü göç (salt-okunur, idempotent, dry-run)" icon="fas fa-right-left" />

    <x-admin.alert />

    {{-- Connection --}}
    <x-admin.card>
        <x-slot:header>
            <span><i class="fas fa-database me-2"></i>Bağlantı</span>
        </x-slot:header>
        <p class="mb-2"><strong>Host:</strong> {{ $creds['host'] }}:{{ $creds['port'] }} &nbsp;|&nbsp;
            <strong>DB:</strong> {{ $creds['database'] ?: '—' }} &nbsp;|&nbsp;
            <strong>User:</strong> {{ $creds['username'] ?: '—' }}</p>
        <p class="text-muted small">Bağlantı bilgileri bu addon'un <em>Configure</em> sayfasından düzenlenir (şifre encrypted).</p>
        <form method="POST" action="{{ route('admin.addons.dispatch', ['name' => $addonName, 'action' => 'test-connection']) }}">
            @csrf
            <button type="submit" class="btn btn-outline-primary btn-sm"><i class="fas fa-plug me-1"></i>Bağlantıyı Test Et</button>
        </form>
    </x-admin.card>

    {{-- Mapping status --}}
    <x-admin.card>
        <x-slot:header><span><i class="fas fa-diagram-project me-2"></i>Eşleme</span></x-slot:header>
        <div class="row text-center">
            <div class="col"><div class="fs-4">{{ $productMap }}</div><div class="text-muted small">Ürün eşleme</div></div>
            <div class="col"><div class="fs-4">{{ $tldMap }}</div><div class="text-muted small">TLD eşleme</div></div>
            <div class="col"><div class="fs-4">{{ $currencyMap }}</div><div class="text-muted small">Currency eşleme</div></div>
        </div>
        <p class="text-muted small mt-3 mb-0">Ürün/TLD/currency eşlemesi import öncesi doldurulmalı; eşlenmemiş kayıtlar dry-run'da uyarı verir ve import'ta atlanır.</p>
    </x-admin.card>

    {{-- Run --}}
    <x-admin.card>
        <x-slot:header><span><i class="fas fa-play me-2"></i>Çalıştır</span></x-slot:header>
        <div class="d-flex gap-2 mb-3">
            <form method="POST" action="{{ route('admin.addons.dispatch', ['name' => $addonName, 'action' => 'dry-run']) }}">
                @csrf
                <button type="submit" class="btn btn-outline-secondary"><i class="fas fa-flask me-1"></i>Dry-Run (yazma)</button>
            </form>
            <form method="POST" action="{{ route('admin.addons.dispatch', ['name' => $addonName, 'action' => 'import']) }}"
                  onsubmit="return confirm('Gerçek göç başlatılsın mı? İdempotent — tekrar çalıştırılabilir.');">
                @csrf
                <button type="submit" class="btn btn-primary"><i class="fas fa-right-left me-1"></i>Göçü Başlat</button>
            </form>
        </div>

        <table class="table table-sm align-middle mb-0">
            <thead><tr><th>Varlık</th><th class="text-end">Ledger'da import</th></tr></thead>
            <tbody>
                @foreach($counts as $type => $n)
                <tr><td class="text-capitalize">{{ $type }}</td><td class="text-end">{{ $n }}</td></tr>
                @endforeach
            </tbody>
        </table>
    </x-admin.card>

    {{-- Last report --}}
    @if($report)
    <x-admin.card>
        <x-slot:header><span><i class="fas fa-file-lines me-2"></i>Son Rapor {{ $reportDry ? '(DRY-RUN)' : '' }}</span></x-slot:header>
        <table class="table table-sm align-middle mb-0">
            <thead><tr><th>Varlık</th><th class="text-end">Imported</th><th class="text-end">Skipped</th><th class="text-end">Failed</th><th>Uyarılar</th></tr></thead>
            <tbody>
                @foreach($report as $type => $r)
                <tr>
                    <td class="text-capitalize">{{ $type }}</td>
                    <td class="text-end text-success">{{ $r['imported'] }}</td>
                    <td class="text-end text-muted">{{ $r['skipped'] }}</td>
                    <td class="text-end {{ $r['failed'] ? 'text-danger' : '' }}">{{ $r['failed'] }}</td>
                    <td class="small text-muted">
                        @foreach(array_slice($r['warnings'] ?? [], 0, 5) as $w)<div>{{ $w }}</div>@endforeach
                        @if(count($r['warnings'] ?? []) > 5)<div>… +{{ count($r['warnings']) - 5 }}</div>@endif
                    </td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </x-admin.card>
    @endif
</div>
@endsection
