@extends('layouts.dashboard')

@section('title', 'Audit Logs')
@section('page-title', 'Audit Logs')

@section('content')
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h5 class="mb-1">Admin Activity Audit</h5>
        <p class="text-muted mb-0">Track all admin actions for security and compliance</p>
    </div>
</div>

<!-- Filters -->
<div class="table-container mb-4">
    <form method="GET" class="row g-3">
        <div class="col-md-3">
            <label class="form-label">Admin</label>
            <select class="form-select form-select-sm" name="admin_id">
                <option value="">All Admins</option>
                @foreach($admins as $admin)
                    <option value="{{ $admin->id }}" {{ request('admin_id') == $admin->id ? 'selected' : '' }}>
                        {{ $admin->name }} ({{ $admin->role }})
                    </option>
                @endforeach
            </select>
        </div>
        <div class="col-md-2">
            <label class="form-label">Action</label>
            <select class="form-select form-select-sm" name="action">
                <option value="">All Actions</option>
                <option value="login" {{ request('action') === 'login' ? 'selected' : '' }}>Login</option>
                <option value="logout" {{ request('action') === 'logout' ? 'selected' : '' }}>Logout</option>
                <option value="create" {{ request('action') === 'create' ? 'selected' : '' }}>Create</option>
                <option value="update" {{ request('action') === 'update' ? 'selected' : '' }}>Update</option>
                <option value="delete" {{ request('action') === 'delete' ? 'selected' : '' }}>Delete</option>
                <option value="toggle_status" {{ request('action') === 'toggle_status' ? 'selected' : '' }}>Toggle Status</option>
                <option value="force_logout" {{ request('action') === 'force_logout' ? 'selected' : '' }}>Force Logout</option>
                <option value="permission_denied" {{ request('action') === 'permission_denied' ? 'selected' : '' }}>Permission Denied</option>
            </select>
        </div>
        <div class="col-md-2">
            <label class="form-label">From Date</label>
            <input type="date" class="form-control form-control-sm" name="from_date" value="{{ request('from_date') }}">
        </div>
        <div class="col-md-2">
            <label class="form-label">To Date</label>
            <input type="date" class="form-control form-control-sm" name="to_date" value="{{ request('to_date') }}">
        </div>
        <div class="col-md-3 d-flex align-items-end gap-2">
            <button type="submit" class="btn btn-primary btn-sm">
                <i class="bi bi-search me-1"></i>Filter
            </button>
            <a href="{{ route('dashboard.audit-logs') }}" class="btn btn-outline-secondary btn-sm">Clear</a>
        </div>
    </form>
</div>

<!-- Audit Logs Table -->
<div class="table-container">
    <div class="table-responsive">
        <table class="table table-hover mb-0">
            <thead>
                <tr>
                    <th>Timestamp</th>
                    <th>Admin</th>
                    <th>Action</th>
                    <th>Resource</th>
                    <th>Details</th>
                    <th>IP Address</th>
                </tr>
            </thead>
            <tbody>
                @forelse($auditLogs as $log)
                <tr>
                    <td>
                        <div class="fw-semibold">{{ $log->created_at->format('M d, Y') }}</div>
                        <small class="text-muted">{{ $log->created_at->format('H:i:s') }}</small>
                    </td>
                    <td>
                        <div class="d-flex align-items-center gap-2">
                            <div class="admin-avatar" style="width: 32px; height: 32px; border-radius: 8px; background: {{ $log->admin && $log->admin->isSuperAdmin() ? 'linear-gradient(135deg, #f093fb 0%, #f5576c 100%)' : 'linear-gradient(135deg, #4facfe 0%, #00f2fe 100%)' }}; display: flex; align-items: center; justify-content: center; color: white; font-weight: 600; font-size: 0.7rem;">
                                {{ strtoupper(substr($log->admin->name ?? 'X', 0, 2)) }}
                            </div>
                            <div>
                                <div class="fw-semibold">{{ $log->admin->name ?? 'Unknown' }}</div>
                                <small class="text-muted">{{ $log->admin->email ?? '' }}</small>
                            </div>
                        </div>
                    </td>
                    <td>
                        @php
                            $actionColor = match($log->action) {
                                'login' => 'success',
                                'logout' => 'secondary',
                                'create' => 'primary',
                                'update' => 'info',
                                'delete' => 'danger',
                                'toggle_status' => 'warning',
                                'force_logout' => 'warning',
                                'reset_password' => 'warning',
                                'permission_denied' => 'danger',
                                'view_sensitive' => 'dark',
                                default => 'secondary',
                            };
                        @endphp
                        <span class="badge bg-{{ $actionColor }}">
                            {{ ucwords(str_replace('_', ' ', $log->action)) }}
                        </span>
                    </td>
                    <td>
                        @if($log->resource_type)
                            <span class="badge bg-light text-dark">{{ ucfirst($log->resource_type) }}</span>
                            @if($log->resource_id)
                                <small class="text-muted">#{{ $log->resource_id }}</small>
                            @endif
                        @else
                            <span class="text-muted">-</span>
                        @endif
                    </td>
                    <td>
                        @if($log->new_values || $log->old_values)
                            <button type="button" class="btn btn-sm btn-outline-secondary" 
                                    data-bs-toggle="modal" data-bs-target="#detailsModal{{ $log->id }}">
                                <i class="bi bi-eye"></i> View
                            </button>
                            
                            <!-- Details Modal -->
                            <div class="modal fade" id="detailsModal{{ $log->id }}" tabindex="-1">
                                <div class="modal-dialog modal-lg">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h5 class="modal-title">
                                                <i class="bi bi-journal-text me-2"></i>
                                                {{ $log->getActionDescription() }}
                                            </h5>
                                            <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                        </div>
                                        <div class="modal-body">
                                            <div class="row mb-3">
                                                <div class="col-sm-4"><strong>Timestamp:</strong></div>
                                                <div class="col-sm-8">{{ $log->created_at->format('M d, Y H:i:s') }}</div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-sm-4"><strong>Admin:</strong></div>
                                                <div class="col-sm-8">{{ $log->admin->name ?? 'Unknown' }} ({{ $log->admin->email ?? '' }})</div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-sm-4"><strong>IP Address:</strong></div>
                                                <div class="col-sm-8">{{ $log->ip_address ?? 'N/A' }}</div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-sm-4"><strong>User Agent:</strong></div>
                                                <div class="col-sm-8"><small>{{ $log->user_agent ?? 'N/A' }}</small></div>
                                            </div>
                                            
                                            @if($log->old_values)
                                            <h6 class="mt-4 mb-2">Previous Values:</h6>
                                            <pre class="bg-light p-3 rounded" style="max-height: 200px; overflow: auto;"><code>{{ json_encode($log->old_values, JSON_PRETTY_PRINT) }}</code></pre>
                                            @endif
                                            
                                            @if($log->new_values)
                                            <h6 class="mt-3 mb-2">New Values:</h6>
                                            <pre class="bg-light p-3 rounded" style="max-height: 200px; overflow: auto;"><code>{{ json_encode($log->new_values, JSON_PRETTY_PRINT) }}</code></pre>
                                            @endif
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        @else
                            <span class="text-muted">-</span>
                        @endif
                    </td>
                    <td>
                        <code class="small">{{ $log->ip_address ?? 'N/A' }}</code>
                    </td>
                </tr>
                @empty
                <tr>
                    <td colspan="6" class="text-center py-4 text-muted">
                        <i class="bi bi-journal-x fs-1 d-block mb-2"></i>
                        No audit logs found
                    </td>
                </tr>
                @endforelse
            </tbody>
        </table>
    </div>

    @if($auditLogs->hasPages())
    <div class="d-flex justify-content-center mt-4">
        {{ $auditLogs->withQueryString()->links() }}
    </div>
    @endif
</div>

<!-- Summary Stats -->
<div class="row mt-4">
    <div class="col-md-3">
        <div class="stat-card">
            <div class="d-flex align-items-center gap-3">
                <div class="icon" style="background: rgba(40, 167, 69, 0.15); color: #28a745;">
                    <i class="bi bi-box-arrow-in-right"></i>
                </div>
                <div>
                    <div class="value">{{ App\Models\AdminAuditLog::where('action', 'login')->whereDate('created_at', today())->count() }}</div>
                    <div class="label">Logins Today</div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card">
            <div class="d-flex align-items-center gap-3">
                <div class="icon" style="background: rgba(0, 123, 255, 0.15); color: #007bff;">
                    <i class="bi bi-pencil-square"></i>
                </div>
                <div>
                    <div class="value">{{ App\Models\AdminAuditLog::whereIn('action', ['create', 'update', 'delete'])->whereDate('created_at', today())->count() }}</div>
                    <div class="label">Changes Today</div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card">
            <div class="d-flex align-items-center gap-3">
                <div class="icon" style="background: rgba(220, 53, 69, 0.15); color: #dc3545;">
                    <i class="bi bi-shield-x"></i>
                </div>
                <div>
                    <div class="value">{{ App\Models\AdminAuditLog::where('action', 'permission_denied')->whereDate('created_at', today())->count() }}</div>
                    <div class="label">Denied Access</div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card">
            <div class="d-flex align-items-center gap-3">
                <div class="icon" style="background: rgba(108, 117, 125, 0.15); color: #6c757d;">
                    <i class="bi bi-clock-history"></i>
                </div>
                <div>
                    <div class="value">{{ App\Models\AdminAuditLog::count() }}</div>
                    <div class="label">Total Logs</div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
