@extends('layouts.app')

@section('title', 'My Applications')

@section('content')
    @include('layouts.partials.page_header', [
        'title' => 'My Applications',
        'showSocial' => false,
        'breadcrumbs' => [
            ['label' => 'Home', 'url' => route('home')],
            ['label' => 'Dashboard', 'url' => route('dashboard')],
            ['label' => 'Applications', 'url' => ''],
        ],
    ])

    @php
        $statusLabels = [
            'draft' => 'Draft',
            'submitted' => 'Submitted',
            'under_review' => 'Under review',
            'visa_processing' => 'Visa processing',
            'approved' => 'Approved',
            'rejected' => 'Rejected',
        ];

        $badgeClasses = [
            'draft' => 'bg-light text-dark',
            'submitted' => 'bg-primary',
            'under_review' => 'bg-warning text-dark',
            'visa_processing' => 'bg-info text-dark',
            'approved' => 'bg-success',
            'rejected' => 'bg-danger',
        ];
    @endphp

    <section class="course-grid">
        <div class="container">
            @if($applications->isEmpty())
                <div class="row justify-content-center">
                    <div class="col-xl-8">
                        <div class="course-grid__left">
                            <div class="course-grid__sidebar">
                                <div class="course-grid__search course-grid__single text-center py-5">
                                    <h3 class="course-grid__title">No Applications Yet</h3>
                                    <p class="course-grid__search-text mb-4">
                                        Start with a program search and create your first application in a few steps.
                                    </p>
                                    <div class="d-flex flex-wrap justify-content-center gap-3">
                                        <a href="{{ route('programs.index') }}" class="thm-btn">
                                            <span>Browse Programs</span>
                                        </a>
                                        <a href="{{ route('portal.apply') }}" class="thm-btn-two">
                                            <span>Start Application</span>
                                            <i class="icon-angles-right"></i>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            @else
                <div class="row g-4">
                    @foreach($applications as $app)
                        <div class="col-12">
                            <div class="blog-list__single">
                                <div class="blog-list__content">
                                    <div class="d-flex flex-column flex-md-row justify-content-between gap-3">
                                        <div>
                                            <h3 class="blog-list__title-1 mb-2">
                                                <a href="{{ route('programs.show', $app->program) }}">
                                                    {{ $app->program->title ?? 'N/A' }}
                                                </a>
                                            </h3>
                                            <p class="blog-list__text mb-0">
                                                {{ $app->program->university->name ?? '' }}
                                                @if($app->program->university->country ?? null)
                                                    &middot; {{ $app->program->university->country->name }}
                                                @endif
                                            </p>
                                            @if($app->scholarship)
                                                <p class="blog-list__text mb-0 mt-2">
                                                    Scholarship:
                                                    <a href="{{ route('scholarships.show', $app->scholarship->slug) }}">
                                                        {{ $app->scholarship->title }}
                                                    </a>
                                                </p>
                                            @endif
                                        </div>

                                        <span class="badge rounded-pill px-3 py-2 align-self-start {{ $badgeClasses[$app->status] ?? 'bg-secondary' }}">
                                            {{ $statusLabels[$app->status] ?? ucfirst(str_replace('_', ' ', $app->status)) }}
                                        </span>
                                    </div>

                                    <ul class="courses-two__meta list-unstyled mt-4">
                                        <li>
                                            <div class="icon"><span class="icon-clock"></span></div>
                                            <p>Submitted {{ $app->submitted_at?->format('M d, Y') ?? 'Pending' }}</p>
                                        </li>
                                        <li>
                                            <div class="icon"><span class="icon-book"></span></div>
                                            <p>{{ $app->documents->count() }} Documents</p>
                                        </li>
                                        <li>
                                            <div class="icon"><span class="icon-location"></span></div>
                                                    <p>{{ $app->program->university->country?->name ?? 'Country' }}</p>
                                        </li>
                                    </ul>

                                    @if($app->counselor_notes)
                                        <div class="mt-4 rounded-3 bg-light p-4">
                                            <p class="mb-1 fw-semibold">Counselor Notes</p>
                                            <p class="mb-0">{{ $app->counselor_notes }}</p>
                                        </div>
                                    @endif

                                    @if($app->documents->isNotEmpty())
                                        <div class="mt-4">
                                            <p class="mb-2 fw-semibold">Uploaded Documents</p>
                                            <div class="d-flex flex-wrap gap-2">
                                                @foreach($app->documents as $document)
                                                    <span class="badge bg-light text-dark px-3 py-2">
                                                        {{ ucfirst(str_replace('_', ' ', $document->type)) }}
                                                    </span>
                                                @endforeach
                                            </div>
                                        </div>
                                    @endif

                                    <div class="d-flex flex-wrap gap-3 mt-4">
                                        <a href="{{ route('programs.show', $app->program) }}" class="thm-btn-two">
                                            <span>Program Details</span>
                                            <i class="icon-angles-right"></i>
                                        </a>
                                        @if($app->scholarship)
                                            <a href="{{ route('scholarships.show', $app->scholarship->slug) }}" class="thm-btn">
                                                <span>Scholarship</span>
                                            </a>
                                        @endif
                                        <a href="{{ route('portal.documents') }}" class="thm-btn-two">
                                            <span>Documents</span>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    @endforeach
                </div>
            @endif
        </div>
    </section>
@endsection
