<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;

use App\Http\Controllers\Siswa\DashboardController;
use App\Http\Controllers\Siswa\LessonController;
use App\Http\Controllers\Siswa\QuizController;
use App\Http\Controllers\Siswa\TaskSubmissionController;
use App\Http\Controllers\Siswa\CourseController;
use App\Http\Controllers\Siswa\PasswordController;
use App\Http\Controllers\Siswa\ProfileController;
use Illuminate\Support\Facades\Http;


/*
|--------------------------------------------------------------------------
| LOGOUT (TOKEN BASED)
|--------------------------------------------------------------------------
*/
Route::post('/logout', function () {
    session()->forget(['token', 'role']);
    session()->invalidate();
    session()->regenerateToken();

    return redirect(config('services.login.url'));
})->name('logout');

/*
|--------------------------------------------------------------------------
| AUTH HANDOVER (DARI BACKEND)
|--------------------------------------------------------------------------
*/
Route::get('/auth/consume-token', function (Request $request) {
    if (! $request->token) {
        abort(403, 'Token tidak ditemukan');
    }

    session([
        'token' => $request->token,
        'role'  => $request->role ?? 'siswa',
    ]);

    return redirect()->route('siswa.dashboard');
});

/*
|--------------------------------------------------------------------------
| ROOT
|--------------------------------------------------------------------------
*/
Route::get('/', function () {
    return redirect(config('services.login.url'));
});

/*
|--------------------------------------------------------------------------
| SISWA (TOKEN AUTH)
|--------------------------------------------------------------------------
*/
Route::middleware(['token.auth'])
    ->prefix('siswa')
    ->name('siswa.')
    ->group(function () {

        /* =====================
         | DASHBOARD
         ===================== */
        Route::get('/dashboard', [DashboardController::class, 'index'])
            ->name('dashboard');

        /* =====================
         | KURSUS (🔥 YANG HILANG)
         ===================== */
        Route::get('/kursus', [CourseController::class, 'index'])
            ->name('kursus');

        /* =====================
         | LESSON / MODUL
         ===================== */
        Route::get('/modul/{lesson}', [LessonController::class, 'show'])
            ->name('modul.show');

        Route::post('/modul/{lesson}/selesai', [LessonController::class, 'markAsCompleted'])
            ->name('modul.selesai');

        Route::post('/lesson/{lesson}/duration', [LessonController::class, 'storeDuration'])
            ->name('lesson.duration');

        /* =====================
         | QUIZ
         ===================== */
        Route::get('/quiz/{quiz}', [QuizController::class, 'intro'])
            ->name('quiz.intro');

        Route::get('/quiz/{quiz}/start', [QuizController::class, 'start'])
            ->name('quiz.start');

        Route::post('/quiz/{quiz}/submit', [QuizController::class, 'submit'])
            ->name('quiz.submit');

        Route::get('/quiz/{quiz}/result', [QuizController::class, 'result'])
            ->name('quiz.result');

        /* =====================
         | TASK
         ===================== */
        Route::get('/task/{task}', [TaskSubmissionController::class, 'show'])
            ->name('task.show');

        Route::post('/task/{task}/submit', [TaskSubmissionController::class, 'store'])
            ->name('task.submit');

        /* =====================
         | PROFILE
         ===================== */

        Route::get('/profile', [ProfileController::class, 'show'])
            ->name('profile.show');
        Route::get('/password', [PasswordController::class, 'edit'])
            ->name('password.edit');

        Route::post('/password', [PasswordController::class, 'update'])
            ->name('password.update');
            
            
// AJAX Email OTP
Route::post('/email/send-otp', function (Request $request) {
    $response = Http::withToken(session('token'))
        ->post(
            config('services.lms.api').'/siswa/email/send-otp',
            $request->only('email')
        );

    if ($response->status() === 401) {
        session()->forget(['token', 'role']);
        return response()->json(['message' => 'Session expired'], 401);
    }

    if ($response->failed()) {
        return response()->json([
            'message' => $response->json('message') ?? 'Gagal mengirim OTP'
        ], $response->status());
    }

    return response()->json($response->json());
})->name('email.send');

// AJAX Verify OTP
Route::post('/email/verify-otp', function (Request $request) {
    $response = Http::withToken(session('token'))
        ->post(
            config('services.lms.api').'/siswa/email/verify-otp',
            $request->only('otp')
        );

    if ($response->status() === 401) {
        session()->forget(['token', 'role']);
        return response()->json(['message' => 'Session expired'], 401);
    }

    if ($response->failed()) {
        return response()->json([
            'message' => $response->json('message') ?? 'OTP tidak valid'
        ], $response->status());
    }

    return response()->json($response->json());
})->name('email.verify');
    });
