<?php

use App\Http\Controllers\CompanyController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\DocumentController;
use App\Http\Controllers\FolderController;
use App\Http\Controllers\PermissionController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\ProjectController;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\UserController;
use App\Livewire\ProjectShow;
use Illuminate\Support\Facades\Route;
use Livewire\Volt\Volt;

Route::get('/', function () {
    return view('welcome');
})->name('home');

/*Route::view('dashboard', 'dashboard')
    ->middleware(['auth', 'verified'])
    ->name('dashboard');

Route::middleware(['auth'])->group(function () {
    Route::redirect('settings', 'settings/profile');

    Volt::route('settings/profile', 'settings.profile')->name('settings.profile');
    Volt::route('settings/password', 'settings.password')->name('settings.password');
    Volt::route('settings/appearance', 'settings.appearance')->name('settings.appearance');
});*/

require __DIR__.'/auth.php';

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

    // Usuarios:
    Route::get('users/{user}/edit', [UserController::class, 'edit'])->name('users.edit');
    Route::get('/users/create', [UserController::class, 'create'])->name('users.create')->middleware('can:create-users');
    Route::post('/users', [UserController::class, 'store'])->name('users.store')->middleware('can:create-users');
    Route::put('/usuarios/{user}', [UserController::class, 'update'])->name('users.update')->middleware('can:update,user'); // Opcional: si usas políticas
    Route::post('/users', [UserController::class, 'store'])->name('users.store');
    Route::put('users/{user}', [UserController::class, 'update'])->name('users.update');
    Route::patch('users/{user}', [UserController::class, 'update']);
    Route::delete('users/{user}', [UserController::class, 'destroy'])->name('users.destroy');
    Route::get('users/{user}', [UserController::class, 'show'])->name('users.show');

    // compañías:
    // Gestión de empresas
    Route::resource('companies', CompanyController::class)->middleware('auth'); // Si necesitas autenticación
    
    // Proyectos
    Route::resource('projects', ProjectController::class)->name('projects.index', 'projects');
    //Route::get('/projects/{project}', ProjectController::class)->name('projects.show');
    //Route::get('/projects/{project}', ProjectController::class)->name('projects.show')->middleware('can:view,project'); // Opcional: política de acceso
    Route::get('/projects/{project}', ProjectShow::class)->name('projects.show');
    
    
    // Documentos
    Route::resource('documents', DocumentController::class);
    Route::post('/documents/{document}', [DocumentController::class, 'show'])->name('documents.show');
    //Route::post('/documents/{document}/approve', [DocumentController::class, 'approve'])->name('documents.approve');
    //Route::post('/documents/{document}/comment', [DocumentController::class, 'addComment'])->name('documents.comment');
    
    // Carpetas
    Route::prefix('folders')->group(function () {
        Route::put('/{folder}/move', [FolderController::class, 'move']);
        Route::post('/', [FolderController::class, 'store']);
        Route::put('/{folder}', [FolderController::class, 'update']);
        Route::delete('/{folder}', [FolderController::class, 'destroy']);
    });
    
    // Gestión de Usuarios
    Route::resource('users', UserController::class)->except('show');
    Route::put('/users/{user}/password', [UserController::class, 'updatePassword'])->name('users.password.update');
    
    // Permisos y Roles
    // Rutas para gestión de roles
    Route::prefix('roles')->middleware(['auth', 'can:view roles'])->group(function () {
        Route::get('/', [RoleController::class, 'index']);
        Route::post('/', [RoleController::class, 'store'])->middleware('can:create roles');
        Route::resource('roles', \App\Http\Controllers\RoleController::class)->except(['show'])->middleware(['auth', 'verified']);
    });
    
    // Perfil de usuario
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::get('/profile', [ProfileController::class, 'show'])->name('profile.show');

    // Ruta para el perfil de usuario
    Route::redirect('settings', 'settings/profile');
    Volt::route('settings/profile', 'settings.profile')->name('settings.profile');
    Volt::route('settings/password', 'settings.password')->name('settings.password');
    Volt::route('settings/appearance', 'settings.appearance')->name('settings.appearance');
});

// API (para integraciones futuras)
Route::prefix('api')->middleware('auth:sanctum')->group(function () {
    Route::get('/projects', [ProjectController::class, 'apiIndex']);
    Route::post('/documents/upload', [DocumentController::class, 'apiStore']);
});
