@extends('layouts.app')

@section('content')
<div class="container-fluid">

<h3 class="mb-3">🔐 Bitácora de Auditoría</h3>

<div class="card">
<div class="card-header bg-dark text-white">
Registro de cambios del sistema
</div>

<div class="card-body">

<table class="table table-bordered table-striped align-middle">
<thead class="bg-light">
<tr>
<th>Fecha</th>
<th>Usuario</th>
<th>Evento</th>
<th>Modelo</th>
<th>ID</th>
<th>IP</th>
<th>Detalle</th>
</tr>
</thead>

<tbody>
@foreach($audits as $audit)
<tr>
<td>{{ $audit->created_at }}</td>
<td>{{ $audit->user->name ?? 'Sistema' }}</td>

<td>
<span class="badge
@if($audit->event=='created') bg-success
@elseif($audit->event=='updated') bg-warning text-dark
@elseif($audit->event=='deleted') bg-danger
@else bg-secondary
@endif">
{{ strtoupper($audit->event) }}
</span>
</td>

<td>{{ class_basename($audit->auditable_type) }}</td>
<td>{{ $audit->auditable_id }}</td>
<td>{{ $audit->ip_address }}</td>

<td>
<button class="btn btn-sm btn-secondary"
data-toggle="modal"
data-target="#audit{{ $audit->id }}">
Ver detalle
</button>
</td>
</tr>
@endforeach
</tbody>
</table>

{{ $audits->links() }}

</div>
</div>

{{-- ================= MODALES ================= --}}
@foreach($audits as $audit)

@php
$antes = $audit->old_values ?? [];
$despues = $audit->new_values ?? [];

/* Campos que NO queremos ver */
$ignorar = [
'password','remember_token',
'created_at','updated_at',
'email_verified_at'
];

/* Detectar SOLO los campos que cambiaron */
$camposCambiados = collect($antes)
->keys()
->merge(collect($despues)->keys())
->unique()
->reject(fn($c) => in_array($c, $ignorar))
->filter(function($campo) use ($antes,$despues) {
    return ($antes[$campo] ?? null) != ($despues[$campo] ?? null);
})
->values();
@endphp

<div class="modal fade" id="audit{{ $audit->id }}" tabindex="-1" role="dialog">
<div class="modal-dialog modal-xl" role="document">
<div class="modal-content">

<div class="modal-header bg-dark text-white">
<h5 class="modal-title">
🔍 Auditoría #{{ $audit->id }} — {{ class_basename($audit->auditable_type) }}
</h5>
<button type="button" class="close text-white" data-dismiss="modal">&times;</button>
</div>

<div class="modal-body">

<p>
<strong>Evento:</strong>
<span class="badge
@if($audit->event=='created') bg-success
@elseif($audit->event=='updated') bg-warning text-dark
@elseif($audit->event=='deleted') bg-danger
@else bg-secondary
@endif">
{{ strtoupper($audit->event) }}
</span>
</p>

<p>
<strong>Usuario:</strong> {{ $audit->user->name ?? 'Sistema' }} |
<strong>Modelo:</strong> {{ class_basename($audit->auditable_type) }} |
<strong>ID:</strong> {{ $audit->auditable_id }}
</p>

<hr>

<h5 class="mb-3">📌 Cambios reales detectados</h5>

@if($camposCambiados->isEmpty())
<div class="alert alert-info">
No hubo cambios relevantes en campos visibles.
</div>
@else

<table class="table table-bordered">
<thead>
<tr>
<th>Campo</th>
<th class="text-danger">ANTES</th>
<th class="text-success">DESPUÉS</th>
</tr>
</thead>

<tbody>
@foreach($camposCambiados as $campo)
<tr>
<td><strong>{{ ucfirst(str_replace('_',' ', $campo)) }}</strong></td>

<td class="bg-danger text-white">
@php $valorAntes = $antes[$campo] ?? null; @endphp

@if(is_array($valorAntes))
<pre class="text-white mb-0">{{ json_encode($valorAntes, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) }}</pre>
@else
{{ $valorAntes ?? '—' }}
@endif
</td>

<td class="bg-success text-white">
@php $valorDespues = $despues[$campo] ?? null; @endphp

@if(is_array($valorDespues))
<pre class="text-white mb-0">{{ json_encode($valorDespues, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) }}</pre>
@else
{{ $valorDespues ?? '—' }}
@endif
</td>
</tr>
@endforeach
</tbody>
</table>

@endif

</div>

<div class="modal-footer">
<button class="btn btn-secondary" data-dismiss="modal">Cerrar</button>
</div>

</div>
</div>
</div>

@endforeach
{{-- ============== FIN MODALES ============== --}}

</div>
@endsection
