@extends('layout.admin')

@section('title', 'অ্যাক্টিভিটি লক')

@section('content')
<div class="min-h-screen bg-slate-50">
  <div class="mx-auto max-w-7xl px-3 sm:px-4 py-4 sm:py-6 space-y-5">
    <div class="flex items-start justify-between gap-3 flex-wrap">
      <div>
        <h1 class="text-xl sm:text-2xl font-extrabold text-slate-900 leading-tight">অ্যাক্টিভিটি লক</h1>
        <p class="mt-1 text-xs sm:text-sm font-semibold text-slate-500">
          Admin name, email, referral code, transaction reference, route বা payload দিয়ে search করতে পারবেন। verification trail-এ কে verify করেছে, কোন reference দিয়েছে, আর sensitive field এখন hidden থাকবে।
        </p>
      </div>
      <a href="{{ route('admin.dashboard') }}"
         class="inline-flex items-center gap-2 rounded-xl border border-slate-200 bg-white px-4 py-3 text-sm font-extrabold text-slate-700 hover:bg-slate-50">
        <i class="fa-solid fa-arrow-left"></i>
        <span>Dashboard</span>
      </a>
    </div>

    <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
      <div class="rounded-2xl border border-slate-200 bg-white p-5 shadow-[0_14px_30px_rgba(2,6,23,0.06)]">
        <div class="text-xs font-extrabold uppercase tracking-[0.16em] text-slate-400">Total Action Logs</div>
        <div class="mt-2 text-3xl font-black text-slate-900">{{ number_format((int) ($summary['total_logs'] ?? 0)) }}</div>
      </div>
      <div class="rounded-2xl border border-slate-200 bg-white p-5 shadow-[0_14px_30px_rgba(2,6,23,0.06)]">
        <div class="text-xs font-extrabold uppercase tracking-[0.16em] text-slate-400">Admins Found</div>
        <div class="mt-2 text-3xl font-black text-slate-900">{{ number_format((int) ($summary['unique_admins'] ?? 0)) }}</div>
      </div>
      <div class="rounded-2xl border border-slate-200 bg-white p-5 shadow-[0_14px_30px_rgba(2,6,23,0.06)]">
        <div class="text-xs font-extrabold uppercase tracking-[0.16em] text-slate-400">Filtered Action Logs</div>
        <div class="mt-2 text-3xl font-black text-slate-900">{{ number_format((int) ($summary['filtered_logs'] ?? 0)) }}</div>
      </div>
    </div>

    <div class="rounded-2xl border border-slate-200 bg-white p-4 sm:p-5 shadow-[0_14px_30px_rgba(2,6,23,0.06)]">
      <form method="GET" action="{{ route('admin.activity-lock.index') }}" class="grid grid-cols-1 lg:grid-cols-[minmax(0,1fr)_auto_auto] gap-3">
        <input
          type="text"
          name="search"
          value="{{ $search }}"
          placeholder="Admin / referral code / transaction / mobile / route দিয়ে search করুন"
          class="w-full rounded-xl border border-slate-300 bg-white px-4 py-3 text-sm font-bold text-slate-900 outline-none focus:border-slate-900 focus:ring-4 focus:ring-slate-100"
        />
        <button type="submit"
                class="rounded-xl bg-slate-900 px-5 py-3 text-sm font-extrabold text-white hover:bg-slate-800">
          <i class="fa-solid fa-magnifying-glass mr-2"></i> Search
        </button>
        <a href="{{ route('admin.activity-lock.index') }}"
           class="rounded-xl border border-slate-200 bg-white px-5 py-3 text-center text-sm font-extrabold text-slate-700 hover:bg-slate-50">
          <i class="fa-solid fa-rotate-left mr-2"></i> Reset
        </a>
      </form>
      <div class="mt-3 rounded-xl border border-emerald-100 bg-emerald-50 px-4 py-3 text-xs sm:text-sm font-bold text-emerald-700">
        Password, admin password, API key, secret, token, PIN, OTP type field auto hidden রাখা হয়েছে।
      </div>
    </div>

    @if($search !== '')
      <div class="rounded-2xl border border-slate-200 bg-white p-4 sm:p-5 shadow-[0_14px_30px_rgba(2,6,23,0.06)]">
        <div class="flex items-start justify-between gap-3 flex-wrap">
          <div>
            <div class="text-xs font-extrabold uppercase tracking-[0.16em] text-slate-400">Verification Trail</div>
            <div class="mt-2 text-lg font-black text-slate-900">Search result for "{{ $search }}"</div>
            <div class="mt-1 text-xs sm:text-sm font-semibold text-slate-500">
              Matching referral / transaction / verification history এখানে আলাদা করে readable summary হিসেবে দেখানো হচ্ছে।
            </div>
          </div>
          <div class="inline-flex rounded-full bg-slate-100 px-4 py-2 text-xs font-extrabold text-slate-700">
            Match: {{ number_format($verificationTrail->count()) }}
          </div>
        </div>

        @if($verificationTrail->isNotEmpty())
          <div class="mt-4 grid grid-cols-1 xl:grid-cols-2 gap-4">
            @foreach($verificationTrail as $trail)
              <div class="rounded-2xl border border-slate-200 bg-slate-50 p-4">
                <div class="flex items-start justify-between gap-3 flex-wrap">
                  <div>
                    <div class="text-sm font-black text-slate-900">{{ $trail['account_name'] ?: 'Unknown User' }}</div>
                    <div class="mt-1 text-xs font-semibold text-slate-500">
                      Ref: {{ $trail['account_referral_code'] ?: '—' }}
                      @if($trail['account_mobile']) · Mobile: {{ $trail['account_mobile'] }} @endif
                    </div>
                  </div>
                  <div class="inline-flex rounded-full bg-sky-100 px-3 py-1 text-[11px] font-extrabold text-sky-700">
                    {{ $trail['event_label'] }}
                  </div>
                </div>

                <div class="mt-4 grid grid-cols-1 sm:grid-cols-2 gap-3 text-xs sm:text-sm">
                  <div class="rounded-xl border border-white bg-white p-3">
                    <div class="text-[11px] font-extrabold uppercase tracking-[0.14em] text-slate-400">Verified By</div>
                    <div class="mt-1 font-extrabold text-slate-900">{{ $trail['admin_name'] }}</div>
                    <div class="mt-1 text-xs font-semibold text-slate-500">{{ $trail['admin_email'] ?: 'No email' }}</div>
                  </div>
                  <div class="rounded-xl border border-white bg-white p-3">
                    <div class="text-[11px] font-extrabold uppercase tracking-[0.14em] text-slate-400">Reference</div>
                    <div class="mt-1 font-extrabold text-slate-900">{{ $trail['transaction_reference'] ?: '—' }}</div>
                    <div class="mt-1 text-xs font-semibold text-slate-500">
                      {{ $trail['verification_channel'] ?: 'No channel' }}
                      @if($trail['selected_option']) · Option: {{ $trail['selected_option'] }} @endif
                    </div>
                  </div>
                  <div class="rounded-xl border border-white bg-white p-3">
                    <div class="text-[11px] font-extrabold uppercase tracking-[0.14em] text-slate-400">Commission / Count</div>
                    <div class="mt-1 font-extrabold text-slate-900">{{ $trail['commission_label'] ?: 'No commission flag' }}</div>
                    <div class="mt-1 text-xs font-semibold text-slate-500">
                      {{ $trail['typing_job_label'] ?: 'Typing N/A' }}
                      @if($trail['welcome_offer_label']) · {{ $trail['welcome_offer_label'] }} @endif
                      @if($trail['leadership_label']) · {{ $trail['leadership_label'] }} @endif
                    </div>
                  </div>
                  <div class="rounded-xl border border-white bg-white p-3">
                    <div class="text-[11px] font-extrabold uppercase tracking-[0.14em] text-slate-400">Request Trail</div>
                    <div class="mt-1 font-extrabold text-slate-900">
                      Request ID: {{ $trail['request_id'] ?: '—' }}
                    </div>
                    <div class="mt-1 text-xs font-semibold text-slate-500">
                      Status: {{ $trail['request_status'] ?: '—' }}
                      @if($trail['request_referral_code']) · Ref: {{ $trail['request_referral_code'] }} @endif
                    </div>
                  </div>
                </div>

                @if($trail['note'])
                  <div class="mt-3 rounded-xl border border-amber-100 bg-amber-50 px-3 py-2 text-xs sm:text-sm font-semibold text-amber-700">
                    {{ $trail['note'] }}
                  </div>
                @endif

                <div class="mt-3 text-xs font-semibold text-slate-500">
                  Time: {{ $trail['happened_at_label'] }}
                </div>
              </div>
            @endforeach
          </div>
        @else
          <div class="mt-4 rounded-2xl border border-dashed border-slate-300 bg-slate-50 px-5 py-8 text-center text-sm font-bold text-slate-500">
            এই search-এ কোনো verification trail পাওয়া যায়নি।
          </div>
        @endif
      </div>
    @endif

    <div class="rounded-2xl border border-slate-200 bg-white shadow-[0_14px_30px_rgba(2,6,23,0.06)] overflow-hidden">
      @if(!$tableAvailable)
        <div class="px-5 py-6 text-sm font-bold text-amber-700 bg-amber-50 border border-amber-200">
          `admin_activity_logs` table এখনো ready না। Migration run হলে activity history এখানে show হবে।
        </div>
      @else
        <div class="overflow-x-auto">
          <table class="min-w-[1720px] w-full divide-y divide-slate-200">
            <thead class="bg-slate-50">
              <tr class="text-left text-[11px] font-extrabold uppercase tracking-[0.14em] text-slate-500">
                <th class="px-4 py-3">Admin</th>
                <th class="px-4 py-3">Activity</th>
                <th class="px-4 py-3">Target / Verify Trail</th>
                <th class="px-4 py-3">Route / Context</th>
                <th class="px-4 py-3">Payload</th>
                <th class="px-4 py-3">Status</th>
                <th class="px-4 py-3">Time</th>
              </tr>
            </thead>
            <tbody class="divide-y divide-slate-100 bg-white text-sm">
              @forelse($logs as $log)
                @php
                  $queryPayload = (array) ($log->masked_query_payload ?? []);
                  $requestPayload = (array) ($log->masked_request_payload ?? []);
                  $displayContext = (array) ($log->display_context ?? []);
                  $userContext = (array) ($displayContext['user'] ?? []);
                  $requestContext = (array) ($displayContext['activation_request'] ?? []);
                  $verificationContext = (array) ($displayContext['verification'] ?? []);
                  $routeContext = (array) ($displayContext['route_context'] ?? []);
                @endphp
                <tr class="align-top">
                  <td class="px-4 py-4">
                    <div class="font-extrabold text-slate-900">{{ $log->admin_name ?: 'Unknown Admin' }}</div>
                    <div class="mt-1 text-xs font-semibold text-slate-500">{{ $log->admin_email ?: 'No email' }}</div>
                    <div class="mt-2 inline-flex rounded-full bg-slate-100 px-3 py-1 text-[11px] font-extrabold text-slate-700">
                      ID: {{ $log->admin_id ?: '—' }}
                    </div>
                  </td>
                  <td class="px-4 py-4">
                    <div class="inline-flex rounded-full px-3 py-1 text-[11px] font-extrabold {{ in_array($log->method, ['POST', 'PUT', 'PATCH', 'DELETE'], true) ? 'bg-rose-100 text-rose-700' : 'bg-sky-100 text-sky-700' }}">
                      {{ $log->method ?: 'GET' }}
                    </div>
                    <div class="mt-3 font-extrabold text-slate-900">{{ $log->activity_summary ?: 'Admin action' }}</div>
                    @if($log->ip_address)
                      <div class="mt-1 text-xs font-semibold text-slate-500">IP: {{ $log->ip_address }}</div>
                    @endif
                  </td>
                  <td class="px-4 py-4">
                    <div class="space-y-3">
                      @if($userContext)
                        <div class="rounded-xl border border-slate-200 bg-slate-50 p-3">
                          <div class="text-[11px] font-extrabold uppercase tracking-[0.14em] text-slate-400">Target User</div>
                          <div class="mt-1 font-extrabold text-slate-900">{{ $userContext['name'] ?: 'Unknown User' }}</div>
                          <div class="mt-1 text-xs font-semibold text-slate-500">
                            Ref: {{ $userContext['referral_code'] ?: '—' }}
                            @if($userContext['mobile']) · Mobile: {{ $userContext['mobile'] }} @endif
                          </div>
                          <div class="mt-2 inline-flex rounded-full px-3 py-1 text-[11px] font-extrabold {{ (int) ($userContext['is_active'] ?? 0) === 1 ? 'bg-emerald-100 text-emerald-700' : 'bg-amber-100 text-amber-700' }}">
                            {{ (int) ($userContext['is_active'] ?? 0) === 1 ? 'Verified' : 'Inactive' }}
                          </div>
                        </div>
                      @endif

                      @if($verificationContext)
                        <div class="rounded-xl border border-slate-200 bg-sky-50 p-3">
                          <div class="text-[11px] font-extrabold uppercase tracking-[0.14em] text-sky-600">Verification Context</div>
                          <div class="mt-1 font-extrabold text-slate-900">
                            {{ $verificationContext['action_label'] ?: 'Action' }}
                          </div>
                          <div class="mt-1 text-xs font-semibold text-slate-600">
                            {{ $verificationContext['commission_label'] ?: 'Commission N/A' }}
                          </div>
                          @if($verificationContext['transaction_reference'] ?? null)
                            <div class="mt-2 text-xs font-bold text-slate-700">
                              Ref: {{ $verificationContext['transaction_reference'] }}
                            </div>
                          @endif
                          @if(($verificationContext['typing_job'] ?? '') !== '' || ($verificationContext['welcome_offer'] ?? '') !== '' || ($verificationContext['leadership'] ?? '') !== '')
                            <div class="mt-2 text-xs font-semibold text-slate-600">
                              {{ $verificationContext['typing_job'] ?: 'Typing N/A' }}
                              @if($verificationContext['welcome_offer']) · {{ $verificationContext['welcome_offer'] }} @endif
                              @if($verificationContext['leadership']) · {{ $verificationContext['leadership'] }} @endif
                            </div>
                          @endif
                        </div>
                      @endif

                      @if($requestContext)
                        <div class="rounded-xl border border-slate-200 bg-amber-50 p-3">
                          <div class="text-[11px] font-extrabold uppercase tracking-[0.14em] text-amber-600">Activation Request</div>
                          <div class="mt-1 font-extrabold text-slate-900">
                            Request ID: {{ $requestContext['id'] ?: '—' }}
                          </div>
                          <div class="mt-1 text-xs font-semibold text-slate-600">
                            Status: {{ $requestContext['status'] ?: '—' }}
                            @if($requestContext['referral_code']) · Ref: {{ $requestContext['referral_code'] }} @endif
                          </div>
                        </div>
                      @endif

                      @if(!$userContext && !$verificationContext && !$requestContext)
                        <div class="text-xs font-semibold text-slate-400">No target context</div>
                      @endif
                    </div>
                  </td>
                  <td class="px-4 py-4">
                    <div class="font-bold text-slate-900">{{ $log->route_name ?: 'Route unavailable' }}</div>
                    <div class="mt-1 text-xs font-semibold text-slate-500 break-all">{{ $log->path ?: '—' }}</div>
                    @if($log->full_url)
                      <div class="mt-2 text-[11px] font-semibold text-slate-400 break-all">{{ $log->full_url }}</div>
                    @endif

                    @if($routeContext)
                      <details class="mt-3 rounded-xl border border-slate-200 bg-slate-50 p-3">
                        <summary class="cursor-pointer text-xs font-extrabold text-slate-700">Route Context</summary>
                        <pre class="mt-2 whitespace-pre-wrap break-all text-[11px] font-semibold text-slate-600">{{ json_encode($routeContext, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) }}</pre>
                      </details>
                    @endif
                  </td>
                  <td class="px-4 py-4">
                    @if($queryPayload || $requestPayload)
                      <div class="space-y-2">
                        @if($queryPayload)
                          <details class="rounded-xl border border-slate-200 bg-slate-50 p-3">
                            <summary class="cursor-pointer text-xs font-extrabold text-slate-700">Query Payload</summary>
                            <pre class="mt-2 whitespace-pre-wrap break-all text-[11px] font-semibold text-slate-600">{{ json_encode($queryPayload, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) }}</pre>
                          </details>
                        @endif
                        @if($requestPayload)
                          <details class="rounded-xl border border-slate-200 bg-slate-50 p-3">
                            <summary class="cursor-pointer text-xs font-extrabold text-slate-700">Request Payload</summary>
                            <pre class="mt-2 whitespace-pre-wrap break-all text-[11px] font-semibold text-slate-600">{{ json_encode($requestPayload, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) }}</pre>
                          </details>
                        @endif
                      </div>
                    @else
                      <div class="text-xs font-semibold text-slate-400">No payload</div>
                    @endif
                  </td>
                  <td class="px-4 py-4">
                    <div class="inline-flex rounded-full px-3 py-1 text-[11px] font-extrabold {{ (int) $log->response_status >= 400 ? 'bg-amber-100 text-amber-700' : 'bg-emerald-100 text-emerald-700' }}">
                      {{ $log->response_status ?: '—' }}
                    </div>
                  </td>
                  <td class="px-4 py-4">
                    <div class="font-bold text-slate-900">{{ optional($log->created_at)->format('d M Y') ?: '—' }}</div>
                    <div class="mt-1 text-xs font-semibold text-slate-500">{{ optional($log->created_at)->format('h:i:s A') ?: '' }}</div>
                  </td>
                </tr>
              @empty
                <tr>
                  <td colspan="7" class="px-4 py-10 text-center text-sm font-bold text-slate-500">
                    কোনো admin activity পাওয়া যায়নি।
                  </td>
                </tr>
              @endforelse
            </tbody>
          </table>
        </div>

        <div class="px-4 py-4 border-t border-slate-100">
          {{ $logs->links() }}
        </div>
      @endif
    </div>
  </div>
</div>
@endsection
