@push('styles')
<link rel="stylesheet" href="{{ asset('assets/css/workflow-actions.css') }}">
@endpush
@extends('admin.admin_master')

@section('admin')
<div class="page-content">
    <div class="container-fluid">
        <div class="row mb-4">
            <div class="col-12">
                <div class="page-header-card">
                    <div class="page-header-content">
                        <div class="d-flex align-items-center justify-content-between">
                            <div class="d-flex align-items-center">
                                <div class="page-icon me-3">
                                    <div class="avatar-md">
                                        <div class="avatar-title bg-warning-subtle text-warning rounded-circle">
                                            <i class="ri-file-warning-line font-size-24"></i>
                                        </div>
                                    </div>
                                </div>
                                <div class="flex-grow-1">
                                    <h2 class="page-title mb-1">System Logs</h2>
                                    <h5 class="text-muted mb-0">Workflow, Folder and File Activity</h5>
                                    <nav aria-label="breadcrumb">
                                        <ol class="breadcrumb mb-0">
                                            <li class="breadcrumb-item">
                                                <a href="{{ route('dashboard') }}" class="text-primary">
                                                    <i class="ri-home-4-line me-1"></i>
                                                </a>
                                            </li>
                                            <li class="breadcrumb-item active" aria-current="page">System Logs</li>
                                        </ol>
                                    </nav>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row mb-4">
            <div class="col-12">
                <div class="card shadow-sm border-0 search-filter-card">
                    <div class="card-body p-3">
                        <form method="GET">
                            <div class="row g-3 align-items-center">
                                <div class="col-md-3">
                                    <select name="entity_type" class="form-select">
                                        <option value="">All Types</option>
                                        <option value="workflow" @if(request('entity_type')=='workflow') selected @endif>Workflow</option>
                                        <option value="step" @if(request('entity_type')=='step') selected @endif>Step</option>
                                        <option value="transition" @if(request('entity_type')=='transition') selected @endif>Transition</option>
                                        <option value="folder" @if(request('entity_type')=='folder') selected @endif>Folder</option>
                                        <option value="file" @if(request('entity_type')=='file') selected @endif>File</option>
                                        <option value="user" @if(request('entity_type')=='user') selected @endif>User</option>
                                        <option value="role" @if(request('entity_type')=='role') selected @endif>Role</option>
                                        <option value="auth" @if(request('entity_type')=='auth') selected @endif>Auth</option>
                                        <option value="file_share" @if(request('entity_type')=='file_share') selected @endif>File Share</option>
                                        <option value="folder_share" @if(request('entity_type')=='folder_share') selected @endif>Folder Share</option>
                                    </select>
                                </div>
                                <div class="col-md-3">
                                    <input type="text" name="user" class="form-control" placeholder="User name or email" value="{{ request('user') }}">
                                </div>
                                <div class="col-md-3">
                                    <input type="text" name="action" class="form-control" placeholder="Action (created, updated, deleted, shared)" value="{{ request('action') }}">
                                </div>
                                <div class="col-md-3">
                                    <button type="submit" class="btn btn-primary w-100">Filter</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        <div class="card">
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>User</th>
                                <th>Type</th>
                                <th>Entity ID</th>
                                <th>Action</th>
                                <th>Changes</th>
                            </tr>
                        </thead>
                        <tbody>
                @forelse($audits as $audit)
                    @php
                        $entityLabelRaw = $audit->after['name'] ?? $audit->before['name'] ?? $audit->after['file_name'] ?? $audit->before['file_name'] ?? $audit->entity_id;
                        $entityLabel = (is_array($entityLabelRaw) || is_object($entityLabelRaw))
                            ? json_encode($entityLabelRaw)
                            : $entityLabelRaw;
                        $userLabel = $audit->user
                            ? ($audit->user->name ?? trim(($audit->user->first_name ?? '') . ' ' . ($audit->user->last_name ?? '')))
                            : (($audit->after['attempted_email'] ?? null) ?: 'System');
                    @endphp
                    <tr>
                        <td>{{ $audit->created_at->format('Y-m-d H:i') }}</td>
                        <td>{{ $userLabel }}</td>
                        <td>{{ ucfirst($audit->entity_type) }}</td>
                        <td>{{ $entityLabel }}</td>
                        <td>{{ ucfirst($audit->action) }}</td>
                        <td>
                            @if($audit->action === 'updated')
                                @php
                                    $beforeData = (array) ($audit->before ?? []);
                                    $afterData = (array) ($audit->after ?? []);
                                    $allKeys = array_unique(array_merge(array_keys($beforeData), array_keys($afterData)));
                                    $ignoredKeys = ['updated_at', 'created_at'];
                                    $changedKeys = array_filter($allKeys, function ($key) use ($beforeData, $afterData) {
                                        return ($beforeData[$key] ?? null) !== ($afterData[$key] ?? null);
                                    });
                                    $changedKeys = array_values(array_filter($changedKeys, function ($key) use ($ignoredKeys) {
                                        return !in_array($key, $ignoredKeys, true);
                                    }));
                                @endphp
                                <span class="text-info">Changed:</span><br>
                                @forelse($changedKeys as $key)
                                    <span class="text-warning">{{ $key }}</span>:
                                    <span class="text-danger">
                                        @php $beforeVal = $beforeData[$key] ?? null; @endphp
                                        @if(is_array($beforeVal) || is_object($beforeVal))
                                            {{ json_encode($beforeVal) }}
                                        @else
                                            {{ $beforeVal }}
                                        @endif
                                    </span>
                                    <span class="text-muted">→</span>
                                    <span class="text-success">
                                        @php $val = $afterData[$key] ?? null; @endphp
                                        @if(is_array($val) || is_object($val))
                                            {{ json_encode($val) }}
                                        @else
                                            {{ $val }}
                                        @endif
                                    </span><br>
                                @empty
                                    <span class="text-muted">No field changes captured.</span>
                                @endforelse
                            @elseif($audit->action === 'created')
                                <span class="text-success">Created</span>
                            @elseif($audit->action === 'deleted')
                                <span class="text-danger">Deleted</span>
                            @elseif($audit->action === 'shared')
                                <span class="text-primary">Shared</span>
                            @elseif($audit->action === 'renamed')
                                <span class="text-warning">Renamed</span>
                            @endif
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="6" class="text-muted">No audit records found.</td></tr>
                @endforelse
            </tbody>
        </table>
        <div class="mt-2">
            {{ $audits->links() }}
        </div>
    </div>
</div>
@endsection
