<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TicketController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\CompanyController;
use App\Http\Controllers\InvitationController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\ProjectController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\SettingsController;
use App\Http\Controllers\HelpController;
use App\Http\Controllers\KnowledgeBaseController;

/*
|--------------------------------------------------------------------------
| 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('/', function () {
    if (auth()->check()) {
        return redirect()->route('dashboard');
    }
    return view('welcome');
})->name('home');

// Authentication Routes (Guest only)
Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [AuthController::class, 'login']);
    Route::get('/register', [AuthController::class, 'showRegisterForm'])->name('register');
    Route::post('/register', [AuthController::class, 'register']);
    Route::get('/forgot-password', [AuthController::class, 'showForgotPasswordForm'])->name('forgot-password');
    Route::post('/forgot-password', [AuthController::class, 'sendResetLinkEmail'])->name('forgot-password.send');
    Route::get('/reset-password/{token}', [AuthController::class, 'showResetPasswordForm'])->name('password.reset');
    Route::post('/reset-password', [AuthController::class, 'resetPassword'])->name('password.update');
    
    // Invitation acceptance (public access)
    Route::get('/invitation/{token}', [InvitationController::class, 'show'])->name('invitation.show');
    Route::post('/invitation/{token}/accept', [InvitationController::class, 'accept'])->name('invitation.accept');
    
    // User activation (public access)
    Route::get('/activate/{token}', [UserController::class, 'showActivationForm'])->name('users.activate.show');
    Route::post('/activate/{token}', [UserController::class, 'activate'])->name('users.activate.store');
});

// Logout (Auth only)
Route::post('/logout', [AuthController::class, 'logout'])->name('logout')->middleware('auth');

// Protected Routes
Route::middleware(['auth'])->group(function () {
    
    // Dashboard
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    
    // Profile, Settings, Help
    Route::get('/profile/edit', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::put('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::get('/settings', [SettingsController::class, 'index'])->name('settings.index');
    Route::get('/help', [HelpController::class, 'index'])->name('help.index');
    
    // Knowledge Base
    Route::resource('knowledge-base', KnowledgeBaseController::class);
    
    // Tickets Routes
    Route::resource('tickets', TicketController::class);
    Route::post('tickets/{ticket}/comments', [TicketController::class, 'addComment'])->name('tickets.comments.store');
    Route::patch('tickets/{ticket}/reopen', [TicketController::class, 'reopen'])->name('tickets.reopen');
    Route::patch('tickets/{ticket}/close', [TicketController::class, 'close'])->name('tickets.close');
    
    // Work Sessions (Time tracking)
    Route::post('tickets/{ticket}/work-session/end', [TicketController::class, 'endWorkSession'])->name('tickets.work-session.end');
    Route::post('tickets/{ticket}/work-session/heartbeat', [TicketController::class, 'workSessionHeartbeat'])->name('tickets.work-session.heartbeat');
    
    // Companies Routes (Admin only)
    Route::middleware(['role:administrador'])->group(function () {
        Route::resource('companies', CompanyController::class);
        Route::post('companies/{company}/projects/{project}/assign-technicians', 
            [CompanyController::class, 'assignTechnicians'])->name('companies.projects.assign-technicians');
    });
    
    // Invitations (Admin only)
    Route::middleware(['role:administrador'])->group(function () {
        Route::get('/invitations', [InvitationController::class, 'index'])->name('invitations.index');
        Route::post('/invitations/generate', [InvitationController::class, 'generate'])->name('invitations.generate');
        Route::post('/invitations/{user}/resend', [InvitationController::class, 'resend'])->name('invitations.resend');
        Route::delete('/invitations/{user}', [InvitationController::class, 'cancel'])->name('invitations.cancel');
    });
    
    // Users Management (Admin only)
    Route::middleware(['role:administrador'])->group(function () {
        Route::resource('users', UserController::class);
        Route::post('users/{user}/resend-activation', [UserController::class, 'resendActivation'])->name('users.resend-activation');
        Route::get('users/{user}/assign-projects', [UserController::class, 'assignProjects'])->name('users.assign-projects');
        Route::put('users/{user}/update-projects', [UserController::class, 'updateProjects'])->name('users.update-projects');
    });
    
    // Projects Routes
    Route::resource('projects', ProjectController::class);
    
    // API auxiliar para obtener proyectos por empresa (solo admin)
    Route::middleware(['role:administrador'])->group(function () {
        Route::get('/api/companies/{company}/projects', [CompanyController::class, 'getProjects'])->name('api.companies.projects');
    });
    
    // API Documentation (Admin only)
    Route::middleware(['role:administrador'])->group(function () {
        Route::get('/api-documentation', function () {
            return view('api.documentation');
        })->name('api.documentation');
    });
    
    // Reports Routes (Supervisor/Admin only)
    Route::middleware(['role:supervisor,administrador'])->group(function () {
        Route::get('/reports', [App\Http\Controllers\ReportController::class, 'index'])->name('reports.index');
    });
    
});