@forelse($items as $challenge)
@php
$participant = $myParticipants->get($challenge->id);
$target = max(1, (int) data_get($challenge->rules, 'target_score', 1));
$score = (int) ($participant?->score ?? 0);
$progress = min(100, (int) round(($score / $target) * 100));
$joined = $participant && in_array($participant->status, ['accepted', 'active', 'completed'], true);
$completed = $participant?->status === 'completed';
$ended = $challenge->status === 'completed' || $challenge->ends_at?->isPast();
$systemTitleKey = 'challenge_official_'.$challenge->period_kind.'_'.$challenge->challenge_type.'_title';
$systemTextKey = 'challenge_official_'.$challenge->period_kind.'_'.$challenge->challenge_type.'_text';
$title = $challenge->is_system && __($systemTitleKey) !== $systemTitleKey ? __($systemTitleKey) : $challenge->name;
$description = $challenge->is_system && __($systemTextKey) !== $systemTextKey ? __($systemTextKey) : $challenge->description;
$ranking = $challenge->participants->sortByDesc('score')->values();
@endphp
{{ $typeIcons[$challenge->challenge_type] ?? '⚡' }}
{{ $scopeIcons[$challenge->scope] ?? '🎯' }} {{ __('ui.challenge_scope_'.$challenge->scope) }}
{{ __('ui.challenge_period_'.$challenge->period_kind) }}
@if($challenge->is_system)✓ {{ __('ui.challenge_official') }}@endif
{{ $title }}
@if($description)
{{ $description }}
@endif
{{ __('ui.challenge_goal') }}{{ $target }} {{ __('ui.challenge_type_'.$challenge->challenge_type) }}
{{ __('ui.challenge_ends') }}{{ $challenge->ends_at?->locale(app()->getLocale())->diffForHumans() ?: '—' }}
@if($challenge->group)
{{ __('ui.challenge_scope_group') }}{{ $challenge->group->name }}
@endif
@if($challenge->is_system)
✦ {{ (int) $challenge->reward_xp }} XP
@if($challenge->reward_badge_key)🏆 {{ __('ui.challenge_reward_badge') }}@endif
@endif
@if($joined)
{{ __('ui.challenge_your_progress') }}{{ $score }} / {{ $target }}
@if($completed)
🏆 {{ __('ui.challenge_completed') }}@endif
@endif
@if($ranking->isNotEmpty())
{{ __('ui.challenge_leaders') }}
@foreach($ranking->take(3) as $ranked)
{{ ['🥇','🥈','🥉'][$loop->index] }} {{ $ranked->user?->username ? '@'.$ranked->user->username : ($ranked->user?->name ?: '—') }} {{ $ranked->score }}
@endforeach
@endif
@if(!$ended && !$joined)
@elseif(!$ended && $joined && !$completed && $challenge->scope !== 'personal')
@endif
@if(!$challenge->is_system && !$ended && (int) $challenge->created_by_user_id === (int) auth()->id())
@endif
@empty
⚡{{ __('ui.challenge_empty_title') }}
{{ __('ui.challenge_empty_text') }}
@endforelse