@extends('clientarea.profile.layout')

@section('profile-content')
<div class="card border-0 shadow-sm">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h5 class="mb-0">{{ __('store.clientarea_profile_navigation_account_activity') }}</h5>
    </div>
    <div class="card-body">
        <p class="text-muted mb-4">{{ __('store.clientarea_profile_account_activity_description') }}</p>

        @if($activities && $activities->count() > 0)
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead>
                        <tr>
                            <th style="width: 180px;">{{ __('store.activity_column_date') }}</th>
                            <th style="width: 160px;">{{ __('store.activity_column_action') }}</th>
                            <th>{{ __('store.activity_column_description') }}</th>
                            <th style="width: 140px;">{{ __('store.activity_column_ip') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($activities as $activity)
                            @php
                                $actionPrefix = Str::before($activity->action, '_');
                                $iconColor = match(true) {
                                    in_array($activity->action, ['login', 'register']) => 'success',
                                    $activity->action === 'login_failed' => 'danger',
                                    $activity->action === 'enable_2fa' => 'success',
                                    $activity->action === 'disable_2fa' => 'warning',
                                    $activity->action === 'logout' => 'secondary',
                                    Str::startsWith($activity->action, 'create_') => 'primary',
                                    Str::startsWith($activity->action, ['add_', 'activate_']) => 'primary',
                                    Str::startsWith($activity->action, ['update_', 'change_', 'toggle_', 'set_']) => 'info',
                                    Str::startsWith($activity->action, ['delete_', 'remove_', 'revoke_', 'reject_', 'leave_']) => 'danger',
                                    Str::startsWith($activity->action, ['request_', 'cancel_', 'withdraw_']) => 'warning',
                                    $activity->action === 'switch_account' => 'warning',
                                    Str::startsWith($activity->action, 'view_') => 'secondary',
                                    Str::startsWith($activity->action, ['manage_', 'process_', 'reply_', 'rate_']) => 'info',
                                    Str::startsWith($activity->action, ['download_', 'accept_']) => 'success',
                                    Str::startsWith($activity->action, ['pay_', 'renew_', 'invite_']) => 'primary',
                                    default => 'primary',
                                };
                                $icon = match(true) {
                                    $activity->action === 'login' => 'sign-in-alt',
                                    $activity->action === 'login_failed' => 'exclamation-triangle',
                                    in_array($activity->action, ['enable_2fa', 'disable_2fa']) => 'shield-alt',
                                    $activity->action === 'logout' => 'sign-out-alt',
                                    $activity->action === 'register' => 'user-plus',
                                    $activity->action === 'switch_account' => 'exchange-alt',
                                    $activity->action === 'create_order' => 'shopping-cart',
                                    $activity->action === 'create_ticket' => 'ticket-alt',
                                    $activity->action === 'reply_ticket' => 'reply',
                                    $activity->action === 'change_password' => 'key',
                                    $activity->action === 'update_profile' => 'user-edit',
                                    Str::contains($activity->action, 'invoice') => 'file-invoice',
                                    Str::contains($activity->action, 'service') => 'server',
                                    Str::contains($activity->action, 'domain') => 'globe',
                                    Str::contains($activity->action, 'ticket') => 'ticket-alt',
                                    Str::contains($activity->action, 'payment') => 'credit-card',
                                    Str::contains($activity->action, 'billing') => 'map-marker-alt',
                                    Str::contains($activity->action, 'dns') => 'network-wired',
                                    Str::contains($activity->action, 'nameserver') => 'server',
                                    Str::contains($activity->action, 'affiliate') => 'handshake',
                                    Str::contains($activity->action, 'authorization') || Str::contains($activity->action, 'authorized') => 'user-shield',
                                    Str::contains($activity->action, 'session') => 'desktop',
                                    Str::contains($activity->action, 'data') => 'shield-alt',
                                    Str::contains($activity->action, 'contract') => 'file-contract',
                                    Str::contains($activity->action, 'announcement') => 'bullhorn',
                                    Str::contains($activity->action, 'email') => 'envelope',
                                    Str::contains($activity->action, 'download') => 'download',
                                    Str::contains($activity->action, 'quote') => 'file-alt',
                                    Str::contains($activity->action, 'notification') => 'bell',
                                    Str::contains($activity->action, 'communication') || Str::contains($activity->action, 'marketing') => 'cog',
                                    Str::contains($activity->action, 'knowledgebase') => 'book',
                                    Str::contains($activity->action, 'network') => 'wifi',
                                    Str::contains($activity->action, 'closure') => 'times-circle',
                                    Str::contains($activity->action, 'fund') => 'wallet',
                                    Str::startsWith($activity->action, 'view_') => 'eye',
                                    Str::startsWith($activity->action, 'create_') => 'plus-circle',
                                    Str::startsWith($activity->action, 'update_') => 'edit',
                                    Str::startsWith($activity->action, 'delete_') || Str::startsWith($activity->action, 'remove_') => 'trash-alt',
                                    default => 'info-circle',
                                };

                                // Translate action label with fallback
                                $actionKey = 'store.activity_' . $activity->action;
                                $actionLabel = __($actionKey);
                                if ($actionLabel === $actionKey) {
                                    $actionLabel = ucfirst(str_replace('_', ' ', $activity->action));
                                }

                                // Translate description with fallback
                                $descKey = 'store.activity_desc_' . $activity->action;
                                $descLabel = __($descKey);
                                $description = ($descLabel !== $descKey) ? $descLabel : $activity->description;
                            @endphp
                            <tr>
                                <td>
                                    <small class="text-muted">{{ $activity->created_at->format($dateTimeFormat) }}</small>
                                </td>
                                <td>
                                    <span class="badge bg-{{ $iconColor }}">
                                        <i class="fas fa-{{ $icon }} me-1"></i>{{ $actionLabel }}
                                    </span>
                                </td>
                                <td>
                                    <div>{{ $description }}</div>
                                    @if($activity->entity_type)
                                        <small class="text-muted">{{ $activity->entity_type }} #{{ $activity->entity_id }}</small>
                                    @endif
                                    @if($activity->actorClient && $activity->authorization_id)
                                        <small class="badge bg-warning text-dark ms-1">{{ __('store.activity_authorized_user') }}</small>
                                    @endif
                                </td>
                                <td>
                                    @if($activity->ip_address)
                                        <small class="text-muted"><i class="fas fa-globe me-1"></i>{{ $activity->ip_address }}</small>
                                    @endif
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>

            <div class="mt-4">
                {{ $activities->links() }}
            </div>
        @else
            <div class="text-center py-5">
                <i class="fas fa-history fa-3x text-muted mb-3"></i>
                <h5>{{ __('store.clientarea_profile_account_activity_no_activities') }}</h5>
                <p class="text-muted">{{ __('store.clientarea_profile_account_activity_no_activities_description') }}</p>
            </div>
        @endif
    </div>
</div>
@endsection
