<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DiagramController;
use Illuminate\Support\Facades\Artisan;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/link-storage', function () {
    Artisan::call('storage:link');
    dd('Storage symbolic link created successfully.');
});

Route::get('/', function () {
    return redirect()->route('admin.root');
});
Route::get('/home', function () {
    return redirect()->route('admin.root');
});
Route::get('/staff', function () {
    return redirect()->route('admin.templates.create');
})->name('staff.root');
// Auth Routes
Route::get('login', [\App\Http\Controllers\AuthController::class, 'showLogin'])->name('login')->middleware('guest');
Route::post('login', [\App\Http\Controllers\AuthController::class, 'login'])->name('login.post')->middleware('guest');
Route::get('register', [\App\Http\Controllers\AuthController::class, 'showRegister'])->name('register')->middleware('guest');
Route::post('register', [\App\Http\Controllers\AuthController::class, 'register'])->name('register.post')->middleware('guest');
Route::post('logout', [\App\Http\Controllers\AuthController::class, 'logout'])->name('logout')->middleware('auth');
Route::post('logout-inactive', [\App\Http\Controllers\AuthController::class, 'logoutInactive'])->name('logout.inactive')->middleware('auth');
Route::post('log-activity', [\App\Http\Controllers\ActivityLogController::class, 'store'])->name('log.activity')->middleware('auth');

Route::middleware('auth')->prefix('admin')->as('admin.')->group(function () {
    // Route::get('/', [DiagramController::class, 'diagramCheck'])->name('root');
Route::get('/', function () {
    return redirect()->route('admin.templates.index');
})->name('root');

    Route::get('profile', [\App\Http\Controllers\AuthController::class, 'profile'])->name('profile');
    Route::post('profile', [\App\Http\Controllers\AuthController::class, 'updateProfile'])->name('profile.update');
    Route::post('profile/password', [\App\Http\Controllers\AuthController::class, 'updatePassword'])->name('profile.password.update');
    
    // Protected Category Routes
    Route::get('categories/manage', [\App\Http\Controllers\CategoryController::class, 'manage'])->name('categories.manage');
    Route::apiResource('categories', \App\Http\Controllers\CategoryController::class);

    // Common Diagram Routes (Admin prefixed but for all auth users)
    Route::resource('diagram', DiagramController::class);
    Route::get('mind-map', [DiagramController::class, 'diagram'])->name('diagram-m');
    Route::get('mind-map-check', [DiagramController::class, 'diagramCheck'])->name('diagram-check');
    
    // Template Routes
    Route::get('templates/list-all', [\App\Http\Controllers\TemplateController::class, 'listAll'])->name('templates.list-all');
    Route::post('templates/{template}/import', [\App\Http\Controllers\TemplateController::class, 'importToCategory'])->name('templates.import');
    Route::resource('templates', \App\Http\Controllers\TemplateController::class);

    // Admin Only Management Routes
    Route::middleware('role:admin')->group(function () {
        Route::get('users', [\App\Http\Controllers\AdminController::class, 'users'])->name('users');
        Route::get('users/{user}', [\App\Http\Controllers\AdminController::class, 'showUser'])->name('users.show');
        Route::post('users', [\App\Http\Controllers\AdminController::class, 'storeUser'])->name('users.store');
        Route::post('users/{user}/update', [\App\Http\Controllers\AdminController::class, 'updateUser'])->name('users.update');
        Route::delete('users/{user}', [\App\Http\Controllers\AdminController::class, 'deleteUser'])->name('users.delete');
        
        Route::get('logs', [\App\Http\Controllers\AdminController::class, 'logs'])->name('logs');
        
        Route::get('roles', [\App\Http\Controllers\AdminController::class, 'roles'])->name('roles');
        Route::post('roles', [\App\Http\Controllers\AdminController::class, 'storeRole'])->name('roles.store');
        Route::delete('roles/{role}', [\App\Http\Controllers\AdminController::class, 'deleteRole'])->name('roles.delete');
        Route::post('roles/{role}/permissions', [\App\Http\Controllers\AdminController::class, 'updateRolePermissions'])->name('roles.permissions');
        
        Route::post('permissions', [\App\Http\Controllers\AdminController::class, 'storePermission'])->name('permissions.store');
        Route::delete('permissions/{permission}', [\App\Http\Controllers\AdminController::class, 'deletePermission'])->name('permissions.delete');
    });
});

    