<?php

use App\Http\Controllers\ProfileController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\Siswa\ActivityController;
use App\Http\Controllers\Siswa\RecommendationController;
use App\Http\Controllers\Siswa\VisualizationController;
use App\Http\Controllers\Siswa\ParentConnectionController;
use App\Http\Controllers\OrangTua\ChildVisualizationController;
use App\Http\Controllers\OrangTua\ChildrenController;
use App\Http\Controllers\Auth\ForgotPasswordController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegisterController;
use Illuminate\Support\Facades\Route;


// Redirect root ke halaman login
Route::get('/', function () {
    return redirect()->route('login');
});

// Guest Routes
Route::middleware('guest')->group(function () {
    // Login
    Route::get('login', [LoginController::class, 'showLoginForm'])->name('login');
    Route::post('login', [LoginController::class, 'login']);
    
    // Register
    Route::get('register', [RegisterController::class, 'showRegistrationForm'])->name('register');
    Route::post('register', [RegisterController::class, 'register']);
    
    // Password Reset (OTP)
    Route::prefix('password')->name('password.')->group(function () {
        Route::get('/forgot', [ForgotPasswordController::class, 'showForgotForm'])->name('request');
        Route::post('/forgot', [ForgotPasswordController::class, 'sendOtp'])->name('send');
        Route::get('/verify', [ForgotPasswordController::class, 'showVerifyForm'])->name('verify.form');
        Route::post('/verify', [ForgotPasswordController::class, 'verifyOtp'])->name('verify');
        Route::get('/reset', [ForgotPasswordController::class, 'showResetForm'])->name('reset.form');
        Route::post('/reset', [ForgotPasswordController::class, 'resetPassword'])->name('update');
    });
});

// Logout
Route::post('/logout', [LoginController::class, 'logout'])->name('logout');

// Authenticated Routes
Route::middleware(['auth', 'verified'])->group(function () {
    
    // Dashboard
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // Siswa Routes
    Route::middleware(['role:siswa'])->prefix('siswa')->name('siswa.')->group(function () {
        Route::get('/input', [ActivityController::class, 'create'])->name('input');
        Route::post('/input', [ActivityController::class, 'store'])->name('input.store');
        Route::get('/input/edit-hari-ini', [ActivityController::class, 'editToday'])->name('input.edit-today');
        Route::patch('/input/edit-hari-ini', [ActivityController::class, 'updateToday'])->name('input.update-today');
        Route::get('/history', [ActivityController::class, 'history'])->name('history');
        Route::get('/rekomendasi', [RecommendationController::class, 'index'])->name('recommendations');
        Route::get('/rekomendasi/{id}', [RecommendationController::class, 'show'])->name('recommendation.detail');
        Route::get('/visualisasi', [VisualizationController::class, 'index'])->name('visualization');
        Route::get('/koneksi-ortu', [ParentConnectionController::class, 'index'])->name('parent-connection');
        Route::post('/koneksi-ortu/clear', [ParentConnectionController::class, 'clearCode'])->name('parent-connection.clear');
        Route::post('/koneksi-ortu/generate', [ParentConnectionController::class, 'generateCode'])->name('parent-connection.generate');
    });

    // Orang Tua Routes
    Route::middleware(['role:orang_tua'])->prefix('orangtua')->name('orangtua.')->group(function () {
        Route::get('/anak', [ChildrenController::class, 'index'])->name('children');
        Route::get('/anak/{id}', [ChildrenController::class, 'detail'])->name('child.detail');
        Route::post('/connect', [ChildrenController::class, 'connect'])->name('connect');
        Route::delete('/anak/{id}/disconnect', [ChildrenController::class, 'disconnect'])->name('child.disconnect');
        Route::get('/anak/{childId}/visualisasi', [ChildVisualizationController::class, 'index'])->name('child.visualization');
    });

    // Profile Routes
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::post('/profile/avatar', [ProfileController::class, 'updateAvatar'])->name('profile.avatar');
    Route::post('/profile/password', [ProfileController::class, 'updatePassword'])->name('profile.password');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});
