@extends('admin.layouts.app')

@section('title', __('admin/logs.details_title') . ' #' . $log->id)

@section('content')
<div class="container-fluid px-4">
    <x-admin.page-header :title="__('admin/logs.details_title') . ' #' . $log->id" icon="fas fa-clipboard-list">
        <a href="{{ url()->previous() }}" class="btn btn-outline-secondary btn-sm">
            <i class="fas fa-arrow-left"></i> {{ __('admin/logs.details_back_to_list') }}
        </a>
    </x-admin.page-header>

    <x-admin.card>
        <div class="row">
            <!-- Genel Bilgiler -->
            <div class="col-md-6">
                <x-admin.card :title="__('admin/logs.details_general_info')" icon="fas fa-info-circle">
                    <table class="table table-borderless">
                        <tr>
                            <td class="fw-bold">{{ __('admin/logs.fields_id') }}:</td>
                            <td>{{ $log->id }}</td>
                        </tr>
                        <tr>
                            <td class="fw-bold">{{ __('admin/logs.fields_type') }}:</td>
                            <td>
                                <span class="badge {{ getTypeBadgeClass($log->type) }}">
                                    {{ $log->type_label }}
                                </span>
                            </td>
                        </tr>
                        <tr>
                            <td class="fw-bold">{{ __('admin/logs.fields_module') }}:</td>
                            <td>{{ $log->module ?: '-' }}</td>
                        </tr>
                        <tr>
                            <td class="fw-bold">{{ __('admin/logs.fields_observer') }}:</td>
                            <td>{{ $log->observer ?: '-' }}</td>
                        </tr>
                        <tr>
                            <td class="fw-bold">{{ __('admin/logs.fields_ip_address') }}:</td>
                            <td>{{ $log->ip_address ?: '-' }}</td>
                        </tr>
                        <tr>
                            <td class="fw-bold">{{ __('admin/logs.fields_method') }}:</td>
                            <td>{{ $log->method ?: '-' }}</td>
                        </tr>
                        <tr>
                            <td class="fw-bold">{{ __('admin/logs.fields_elapsed_ms') }}:</td>
                            <td>{{ $log->elapsed_ms ? $log->elapsed_ms . 'ms' : '-' }}</td>
                        </tr>
                        <tr>
                            <td class="fw-bold">{{ __('admin/logs.fields_created_at') }}:</td>
                            <td>{{ $log->created_at->format($dateTimeSecFormat) }}</td>
                        </tr>
                        @if($log->user_agent)
                        <tr>
                            <td class="fw-bold">{{ __('admin/logs.fields_user_agent') }}:</td>
                            <td>
                                <small class="text-muted">{{ Str::limit($log->user_agent, 100) }}</small>
                            </td>
                        </tr>
                        @endif
                        @if($log->session_id)
                        <tr>
                            <td class="fw-bold">{{ __('admin/logs.fields_session_id') }}:</td>
                            <td>{{ $log->session_id }}</td>
                        </tr>
                        @endif
                    </table>
                </x-admin.card>
            </div>

            <!-- Kullanıcı Bilgileri -->
            <div class="col-md-6">
                <x-admin.card :title="__('admin/logs.details_user_details')" icon="fas fa-user">
                    @if($log->client_id && $log->client)
                        <div class="alert alert-info">
                            <h6><i class="fas fa-user"></i> {{ __('admin/logs.types_client') }}</h6>
                            <p class="mb-1"><strong>{{ __('admin/logs.fields_name') }}:</strong> {{ $log->client->name }}</p>
                            <p class="mb-1"><strong>{{ __('admin/logs.fields_email') }}:</strong> {{ $log->client->email }}</p>
                            <p class="mb-0"><strong>ID:</strong> {{ $log->client->id }}</p>
                        </div>
                    @elseif($log->admin_id && $log->admin)
                        <div class="alert alert-warning">
                            <h6><i class="fas fa-user-shield"></i> {{ __('admin/logs.types_admin') }}</h6>
                            <p class="mb-1"><strong>{{ __('admin/logs.fields_name') }}:</strong> {{ $log->admin->name }}</p>
                            <p class="mb-1"><strong>{{ __('admin/logs.fields_email') }}:</strong> {{ $log->admin->email }}</p>
                            <p class="mb-0"><strong>ID:</strong> {{ $log->admin->id }}</p>
                        </div>
                    @else
                        <div class="alert alert-secondary">
                            <h6><i class="fas fa-server"></i> {{ __('admin/logs.types_system') }}</h6>
                            <p class="mb-0">{{ __('admin/logs.messages_system_log_created') }}</p>
                        </div>
                    @endif
                </x-admin.card>
            </div>
        </div>

        <!-- Açıklama -->
        @if($log->description)
        <div class="row mt-3">
            <div class="col-12">
                <x-admin.card :title="__('admin/logs.fields_description')" icon="fas fa-align-left">
                    <p class="mb-0">{{ $log->description }}</p>
                </x-admin.card>
            </div>
        </div>
        @endif

        <!-- İstek Detayları -->
        @if($log->request && is_array($log->request) && count($log->request) > 0)
        <div class="row mt-3">
            <div class="col-12">
                <x-admin.card :title="__('admin/logs.details_request_details')" icon="fas fa-arrow-right">
                    <pre class=" p-3 rounded"><code>{{ json_encode($log->request, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) }}</code></pre>
                </x-admin.card>
            </div>
        </div>
        @endif

        <!-- Yanıt Detayları -->
        @if($log->response && is_array($log->response) && count($log->response) > 0)
        <div class="row mt-3">
            <div class="col-12">
                <x-admin.card :title="__('admin/logs.details_response_details')" icon="fas fa-arrow-left">
                    <pre class=" p-3 rounded"><code>{{ json_encode($log->response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) }}</code></pre>
                </x-admin.card>
            </div>
        </div>
        @endif
    </x-admin.card>
</div>
@endsection

@php
function getTypeBadgeClass($type) {
    $typeClasses = [
        'system' => 'bg-primary',
        'payment' => 'bg-success',
        'mail' => 'bg-info',
        'payment_provider' => 'bg-warning',
        'module' => 'bg-secondary',
        'client' => 'bg-dark',
        'admin' => 'bg-danger',
        'php' => ' text-dark'
    ];

    return $typeClasses[$type] ?? 'bg-secondary';
}
@endphp
