@extends('layouts.stage_academy_portal')

@section('title', __('stage_academy_portal.nav.categories'))

@section('body')
    @php
        $locale = app()->getLocale();
        $otherLocale = $locale === 'ar' ? 'en' : 'ar';
        $textDir = $locale === 'ar' ? 'rtl' : 'ltr';
        $active = $active ?? 'categories';
    @endphp

    <nav class="navbar navbar-expand-lg navbar-light bg-white bg-opacity-75 border-bottom sap-portal-navbar" style="backdrop-filter: blur(10px);">
        <div class="container">
            <div class="d-flex flex-column">
                <span class="navbar-brand fw-bold mb-0 sap-brand-shine">{{ __('stage_academy_portal.brand') }}</span>
            </div>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#sapNavbar" aria-controls="sapNavbar" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>

            <div class="collapse navbar-collapse" id="sapNavbar">
                <div class="d-flex flex-column flex-lg-row {{ $locale === 'ar' ? 'flex-lg-row-reverse' : '' }} w-100 align-items-lg-center justify-content-lg-between gap-2 py-2 py-lg-0">
                    <ul class="navbar-nav mb-0">
                        <li class="nav-item">
                            <a class="nav-link {{ $active === 'my_courses' ? 'active fw-semibold' : '' }}" href="{{ route('stage-academy-portal.my-courses') }}">{{ __('stage_academy_portal.nav.my_courses') }}</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link {{ $active === 'profile' ? 'active fw-semibold' : '' }}" href="{{ route('stage-academy-portal.profile') }}">{{ __('stage_academy_portal.nav.profile') }}</a>
                        </li>
                    </ul>

                    <div class="d-flex align-items-center gap-2">
                        <div class="sap-welcome-text d-flex align-items-center gap-2 small text-secondary" dir="{{ $textDir }}">
                            @if(! empty($portalUser->image) && \Illuminate\Support\Facades\Schema::hasColumn('stage_academy_users', 'image'))
                                <img src="{{ asset('storage/'.$portalUser->image) }}" alt="{{ $portalUser->name }}" style="width: 32px; height: 32px; object-fit: cover; border-radius: 50%;">
                            @else
                                <div class="d-flex align-items-center justify-content-center bg-white border rounded-circle" style="width: 32px; height: 32px;">
                                    <i class="bi bi-person-fill text-secondary"></i>
                                </div>
                            @endif
                            <span>{{ __('stage_academy_portal.welcome') }} <strong>{{ $portalUser->name }}</strong></span>
                        </div>
                        <a href="{{ route('lang.switch', $otherLocale) }}" class="btn btn-sm btn-outline-secondary" dir="ltr">
                            {{ strtoupper($otherLocale) }}
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </nav>

    <div class="container py-3 py-md-4">
        <div class="card border-0 shadow-sm rounded-4 overflow-hidden mb-3">
            <div class="card-body p-4 p-md-5" style="background: radial-gradient(700px 350px at 10% 20%, rgba(34, 197, 94, 0.16), transparent 55%), radial-gradient(700px 350px at 90% 10%, rgba(16, 185, 129, 0.10), transparent 55%);">
                <div class="d-flex align-items-start justify-content-between gap-3 flex-wrap">
                    <div>
                        <div class="fw-bold fs-4">{{ __('stage_academy_portal.categories.title') }}</div>
                        <div class="text-secondary mt-2">{{ __('stage_academy_portal.categories.subtitle') }}</div>
                    </div>
                    <div class="badge rounded-pill text-bg-success px-3 py-2">
                        {{ $portalUser->name }}
                    </div>
                </div>
            </div>
        </div>

        @if(isset($categories) && $categories->isNotEmpty())
            <div class="row g-3 g-md-4">
                @foreach($categories as $category)
                    <div class="col-12 col-md-6">
                        <div class="card border-0 shadow-sm rounded-4 h-100">
                            <div class="card-body p-3 p-md-4">
                                <div class="d-flex align-items-start justify-content-between gap-3">
                                    <div>
                                        <a href="{{ route('stage-academy-portal.categories.show', $category->id) }}" class="text-decoration-none text-reset">
                                            <div class="fw-bold">
                                                {{ $locale === 'ar' ? $category->name_ar : $category->name_en }}
                                            </div>
                                        </a>
                                        @php
                                            $categoryDescription = $locale === 'ar' ? (string) $category->description_ar : (string) $category->description_en;
                                            $categoryDescription = trim($categoryDescription);
                                        @endphp
                                        @if($categoryDescription !== '')
                                            <div class="text-secondary mt-2 text-break">
                                                {{ \Illuminate\Support\Str::limit($categoryDescription, 170) }}
                                            </div>
                                        @endif
                                    </div>
                                    <div class="sap-icon-bubble flex-shrink-0">
                                        <i class="bi bi-grid"></i>
                                    </div>
                                </div>

                                @if($category->subCategories && $category->subCategories->isNotEmpty())
                                    <div class="mt-3 d-flex flex-wrap gap-2">
                                        @foreach($category->subCategories as $subCategory)
                                            <a href="{{ route('stage-academy-portal.sub-categories.show', $subCategory->id) }}" class="btn btn-sm btn-outline-success">
                                                {{ $locale === 'ar' ? $subCategory->name_ar : $subCategory->name_en }}
                                            </a>
                                        @endforeach
                                    </div>
                                @endif
                            </div>
                        </div>
                    </div>
                @endforeach
            </div>
        @else
            <div class="card border-0 shadow-sm rounded-4">
                <div class="card-body p-4 p-md-5 text-center text-secondary">
                    <i class="bi bi-grid-3x3-gap fs-2 d-block mb-2"></i>
                    <div class="fw-semibold">{{ __('stage_academy_portal.categories.empty_title') }}</div>
                    <div class="mt-1">{{ __('stage_academy_portal.categories.empty_body') }}</div>
                </div>
            </div>
        @endif
    </div>
@endsection
