@extends('layouts.admin')

@section('title', 'Callback Logs')

@section('content')
    <div class="d-flex flex-wrap justify-content-between align-items-center gap-1 mb-2">
        <div>
            <span class="loot-status success">Provider callbacks</span>
            <h1 class="loot-section-title mt-1 mb-25">Logs</h1>
            <p class="loot-muted mb-0">Audit completed offerwall callbacks and inspect the provider payload.</p>
        </div>
    </div>

    <div class="loot-admin-grid mb-2">
        <div class="loot-admin-card"><span>Total</span><strong>{{ number_format($stats['total']) }}</strong></div>
        <div class="loot-admin-card"><span>Success</span><strong>{{ number_format($stats['success']) }}</strong></div>
        <div class="loot-admin-card"><span>Failed</span><strong>{{ number_format($stats['failed']) }}</strong></div>
        <div class="loot-admin-card"><span>Today</span><strong>{{ number_format($stats['today']) }}</strong></div>
    </div>

    <section class="user-page-card">
        <form method="GET" class="admin-leads-filters">
            <div class="admin-filter-search">
                <i data-feather="search"></i>
                <input class="form-control" name="search" value="{{ request('search') }}" placeholder="Search provider, user, endpoint, IP">
            </div>
            <select class="form-select" name="provider">
                <option value="">All providers</option>
                @foreach($providers as $provider)
                    <option value="{{ $provider }}" @selected(request('provider') === $provider)>{{ $provider }}</option>
                @endforeach
            </select>
            <select class="form-select" name="status">
                <option value="">All statuses</option>
                <option value="success" @selected(request('status') === 'success')>Success</option>
                <option value="failed" @selected(request('status') === 'failed')>Failed</option>
            </select>
            <select class="form-select" name="per_page">
                @foreach([20, 50, 100] as $size)
                    <option value="{{ $size }}" @selected((int) request('per_page', 20) === $size)>{{ $size }} / page</option>
                @endforeach
            </select>
            <button class="btn btn-loot">Apply</button>
            <a class="btn btn-outline-light" href="{{ route('admin.logs') }}">Reset</a>
        </form>
        <form id="callbackLogBulk" data-bulk-form="callback-logs" method="POST" action="{{ route('admin.logs.bulk') }}" class="admin-bulk-bar mb-1">
            @csrf
            <select class="form-select" name="action" required><option value="">Bulk actions</option><option value="delete">Delete selected logs</option></select>
            <button class="btn btn-outline-light" data-bulk-submit="callback-logs" disabled>Apply</button>
            <span class="loot-muted" data-bulk-count="callback-logs">0 selected</span>
            <button type="button" class="btn btn-outline-light ms-auto" data-bulk-export="callback-logs" data-export-url="{{ route('admin.export.logs', request()->query()) }}"><i data-feather="download" class="me-50"></i>Export CSV</button>
        </form>

        <div class="table-responsive admin-leads-table-wrap">
            <table class="table admin-leads-table mb-0">
                <thead><tr><th class="admin-check-cell"><input class="form-check-input" type="checkbox" data-bulk-select-all="callback-logs" aria-label="Select all callback logs"></th><th>Provider</th><th>User</th><th>Lead</th><th>Status</th><th>Endpoint</th><th>IP</th><th>Payload</th><th>Date</th></tr></thead>
                <tbody>
                @forelse($logs as $log)
                    <tr>
                        <td class="admin-check-cell"><input class="form-check-input" type="checkbox" name="ids[]" value="{{ $log->id }}" form="callbackLogBulk" data-bulk-item="callback-logs" aria-label="Select callback log {{ $log->id }}"></td>
                        <td><strong>{{ $log->provider }}</strong></td>
                        <td>{{ optional($log->user)->username ?? 'N/A' }}<div class="loot-muted small">{{ optional($log->user)->email }}</div></td>
                        <td>@if($log->lead)<a href="{{ route('admin.leads', ['search' => $log->lead_id]) }}">#{{ $log->lead_id }}</a>@else N/A @endif</td>
                        <td><span class="loot-status {{ $log->status === 'success' ? 'success' : 'danger' }}">{{ ucfirst($log->status) }}</span></td>
                        <td>{{ $log->method }} {{ $log->endpoint }}</td>
                        <td>{{ $log->ip }}</td>
                        <td><button class="btn btn-sm btn-outline-light" data-bs-toggle="modal" data-bs-target="#payload{{ $log->id }}">View</button></td>
                        <td>{{ $log->created_at->diffForHumans() }}</td>
                    </tr>
                @empty
                    <tr><td colspan="9" class="text-center py-4 loot-muted">No callback logs yet.</td></tr>
                @endforelse
                </tbody>
            </table>
        </div>
        <div class="mt-2">{{ $logs->links() }}</div>
    </section>

    @foreach($logs as $log)
        <div class="modal fade" id="payload{{ $log->id }}" tabindex="-1" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title loot-section-title">{{ $log->provider }} payload</h4>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>
                    <div class="modal-body">
                        <pre class="loot-code-block">{{ json_encode($log->payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) }}</pre>
                    </div>
                </div>
            </div>
        </div>
    @endforeach
@endsection
