<?php

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Schema;
use Inertia\Middleware;

class HandleInertiaRequests extends Middleware
{
    /**
     * The root template that's loaded on the first page visit.
     *
     * @see https://inertiajs.com/server-side-setup#root-template
     *
     * @var string
     */
    protected $rootView = 'app';

    /**
     * Determines the current asset version.
     *
     * @see https://inertiajs.com/asset-versioning
     */
    public function version(Request $request): ?string
    {
        return parent::version($request);
    }

    /**
     * Define the props that are shared by default.
     *
     * @see https://inertiajs.com/shared-data
     *
     * @return array<string, mixed>
     */
    public function share(Request $request): array
    {
        return [
            ...parent::share($request),
            //

            //session
            'session' => [
                'status' => fn() => $request->session()->get('status'),
                'success' => fn() => $request->session()->get('success'),
                'error' => fn() => $request->session()->get('error'),
                'new_session_id' => fn() => $request->session()->get('new_session_id'),
                'new_session_title' => fn() => $request->session()->get('new_session_title'),
                'new_exam_id' => fn() => $request->session()->get('new_exam_id'),
                'new_exam_title' => fn() => $request->session()->get('new_exam_title'),
            ],
            //user authenticated
            'auth' => [
                'user' => auth()->user() ? auth()->user() : null,
                'student' => auth()->guard('student')->user() ? auth()->guard('student')->user() : null,
            ],

            'panel' => fn () => $this->resolvePanel($request),

            //app globally configured settings (name & logo)
            'app_setting' => Schema::hasTable('settings') ? \App\Models\Setting::first() : null,

            //sidebar badge counts
            'sidebar_counts' => fn () => $this->resolveSidebarCounts($request),
        ];
    }

    protected function resolvePanel(Request $request): array
    {
        $user = $request->user();

        if (!$user) {
            return [
                'role' => null,
                'prefix' => '/admin',
                'name' => 'Admin',
            ];
        }

        return [
            'role' => $user->role,
            'prefix' => $user->isTeacher() ? '/guru' : '/admin',
            'name' => $user->isTeacher() ? 'Guru' : 'Admin',
        ];
    }

    protected function resolveSidebarCounts(Request $request): ?array
    {
        $user = $request->user();

        if (!$user) {
            return null;
        }

        if ($user->isTeacher()) {
            // Guru: scope counts to their own lessons
            $lessonIds = $user->lessons()->pluck('lessons.id');
            return [
                'questions' => \App\Models\Question::whereIn('lesson_id', $lessonIds)->count(),
                'exams' => \App\Models\Exam::whereIn('lesson_id', $lessonIds)->count(),
                'sessions' => \App\Models\ExamSession::count(),
            ];
        }

        return [
            'questions' => \App\Models\Question::count(),
            'exams' => \App\Models\Exam::count(),
            'sessions' => \App\Models\ExamSession::count(),
            'students' => \App\Models\Student::count(),
        ];
    }
}
