@extends('layouts.public')

@section('title', $coach->user->fullName())

@section('content')
    <a href="{{ route('coaches.index') }}" class="inline-flex items-center gap-1.5 text-sm text-steel-500 hover:text-steel-600 mb-4">
        <x-icon name="arrow-right" class="w-4 h-4 rotate-180" /> بازگشت به لیست مربیان
    </a>

    <div class="card-pad">
        <div class="flex items-center gap-4 mb-5">
            @if($coach->photo_path)
                <img src="{{ Storage::disk('public')->url($coach->photo_path) }}" class="w-20 h-20 rounded-full object-cover">
            @else
                <div class="w-20 h-20 rounded-full bg-ink-950 text-white flex items-center justify-center text-xl font-semibold">
                    {{ mb_substr($coach->user->first_name, 0, 1) }}
                </div>
            @endif
            <div>
                <h1 class="text-lg font-bold text-ink-950">{{ $coach->user->fullName() }}</h1>
                <div class="text-sm mt-1">
                    @if($coach->user->isOnline())
                        <span class="inline-flex items-center gap-1.5 text-emerald-600"><span class="dot-online"></span> آنلاین</span>
                    @else
                        <span class="text-slate-400">آخرین بازدید: {{ verta($coach->user->last_seen_at)->format('Y/m/d H:i') }}</span>
                    @endif
                </div>
            </div>
        </div>

        <div class="grid grid-cols-3 gap-3 mb-6">
            <div class="rounded-xl bg-slate-50 py-3 text-center">
                <div class="text-lg font-bold text-ink-950">{{ $coach->experience_years ?? 0 }}</div>
                <div class="eyebrow mt-0.5">سال سابقه</div>
            </div>
            <div class="rounded-xl bg-slate-50 py-3 text-center">
                <div class="text-lg font-bold text-ink-950">{{ $certificatesCount }}</div>
                <div class="eyebrow mt-0.5">مدرک تأیید‌شده</div>
            </div>
            <div class="rounded-xl bg-slate-50 py-3 text-center">
                @if($coach->averageRating())
                    <div class="text-lg font-bold text-ember-500 flex items-center justify-center gap-1">
                        {{ $coach->averageRating() }} <x-icon name="star" class="w-3.5 h-3.5 fill-current" />
                    </div>
                    <div class="eyebrow mt-0.5">{{ $coach->reviewsCount() }} نظر</div>
                @else
                    <div class="text-lg font-bold text-slate-300">—</div>
                    <div class="eyebrow mt-0.5">بدون امتیاز</div>
                @endif
            </div>
        </div>

        <div class="flex flex-wrap gap-1.5 mb-6">
            @foreach($coach->goals as $goal)
                <span class="badge-slate">{{ $goal->title }}</span>
            @endforeach
        </div>

        <h2 class="font-semibold text-sm mb-2">درباره مربی</h2>
        <p class="text-sm text-slate-600 whitespace-pre-line mb-5">{{ $coach->bio ?: 'توضیحی ثبت نشده است.' }}</p>

        <h2 class="font-semibold text-sm mb-2">سابقه کاری</h2>
        <p class="text-sm text-slate-600 whitespace-pre-line mb-6">{{ $coach->work_history ?: 'توضیحی ثبت نشده است.' }}</p>

        @if($coach->program_price)
            <div class="text-lg font-bold text-ink-950 mb-4">قیمت پایه: {{ number_format($coach->program_price) }} تومان</div>
        @endif

        @auth
            @if(auth()->user()->isAthlete())
                @php $athlete = auth()->user()->athlete; @endphp
                @if($athlete->hasActiveRelationshipWith($coach))
                    <div class="flex items-center gap-2 rounded-xl bg-emerald-50 text-emerald-700 text-sm px-4 py-3">
                        <x-icon name="check" class="w-4 h-4 shrink-0" />
                        شما با این مربی همکاری فعال دارید.
                        <a href="{{ route('athlete.coaches.index') }}" class="underline font-medium">مشاهده جزئیات</a>
                    </div>
                @else
                    <form method="POST" action="{{ route('athlete.coach-requests.store', $coach) }}">
                        @csrf
                        <textarea name="message" rows="2" placeholder="پیام اختیاری برای مربی..." class="field-input mb-2"></textarea>
                        <button class="btn-primary">درخواست برنامه</button>
                    </form>
                    @error('request') <p class="field-error">{{ $message }}</p> @enderror
                @endif
            @endif
        @else
            <a href="{{ route('login') }}" class="btn-primary">برای درخواست برنامه وارد شوید</a>
        @endauth
    </div>

    @if($coach->reviewsCount() > 0)
        <div class="card-pad mt-5">
            <h2 class="font-semibold text-sm mb-4">نظرات ورزشکاران ({{ $coach->reviewsCount() }})</h2>
            <div class="space-y-4">
                @foreach($coach->approvedReviews()->with('coachRequest.athlete.user')->limit(10)->get() as $review)
                    <div class="border-b border-slate-50 pb-4 last:border-0 last:pb-0">
                        <div class="flex items-center justify-between">
                            <span class="text-sm font-medium text-ink-900">{{ $review->coachRequest->athlete->user->fullName() }}</span>
                            <span class="flex items-center gap-0.5 text-ember-500">
                                @for($i = 1; $i <= 5; $i++)
                                    <x-icon name="star" class="w-3.5 h-3.5 {{ $i <= $review->rating ? 'fill-current' : 'text-slate-200' }}" />
                                @endfor
                            </span>
                        </div>
                        @if($review->comment)
                            <p class="text-sm text-slate-600 mt-1.5">{{ $review->comment }}</p>
                        @endif
                        <div class="text-xs text-slate-400 mt-1.5">{{ verta($review->created_at)->format('Y/m/d') }}</div>
                    </div>
                @endforeach
            </div>
        </div>
    @endif
@endsection
