<?php

use App\Http\Controllers\StaffProductivityExportController;
use App\Http\Controllers\WorkCatalogExportController;
use App\Http\Controllers\WorkFileDownloadController;
use App\Http\Controllers\WorkObservedIncidentsExportController;
use App\Http\Controllers\WorkOperationalEfficiencyExportController;
use App\Livewire\Dashboard\DashboardIndex;
use App\Livewire\Departments\DepartmentIndex;
use App\Livewire\Districts\DistrictIndex;
use App\Livewire\Projects\ProjectIndex;
use App\Livewire\Provinces\ProvinceIndex;
use App\Livewire\Sites\SiteIndex;
use App\Livewire\Users\UserIndex;
use App\Livewire\Works\CoordinatorAssignmentsIndex;
use App\Livewire\Works\NocTrackingIndex;
use App\Livewire\Works\TechnicianAssignmentsIndex;
use App\Livewire\Works\WorkCatalogIndex;
use App\Livewire\Works\WorkCreate;
use App\Livewire\Works\WorkShow;
use App\Livewire\WorkTypes\WorkTypeIndex;
use App\Models\Work;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Route;

Route::view('/', 'welcome')->name('home');

Route::get('storage-link', function () {
    Artisan::call('storage:link', ['--force' => true]);

    return response(trim(Artisan::output()) ?: 'Listo.', 200, [
        'Content-Type' => 'text/plain; charset=UTF-8',
    ]);
})->name('storage.link');

Route::middleware(['auth'])->group(function () {
    Route::get('dashboard', DashboardIndex::class)->name('dashboard');
    Route::get('adjuntos/{file}', WorkFileDownloadController::class)->name('work-files.download');
});

Route::middleware(['auth', 'operations'])->group(function () {
    Route::get('proyectos', ProjectIndex::class)->name('projects.index');
    Route::get('sitios', SiteIndex::class)->name('sites.index');
    Route::get('tipos-trabajo', WorkTypeIndex::class)->name('work-types.index');
    Route::get('trabajos', WorkCatalogIndex::class)->name('works.index');
    Route::get('trabajos/exportar', WorkCatalogExportController::class)->name('works.export');
    Route::get('trabajos/exportar/eficiencia-operativa', WorkOperationalEfficiencyExportController::class)->name('works.export.operational');
    Route::get('trabajos/exportar/incidencias-observado', WorkObservedIncidentsExportController::class)->name('works.export.observed-incidents');
    Route::get('trabajos/nuevo', WorkCreate::class)->name('works.create');
    Route::get('trabajos/asignaciones', CoordinatorAssignmentsIndex::class)->name('works.assignments');
});

/*
 * Detalle de OT: fuera del middleware "operations" para que técnico/NOC puedan abrirla con la misma URL
 * (/trabajos/{id}) sin 403; el acceso real lo define WorkPolicy::view en WorkShow::mount.
 */
Route::middleware(['auth'])->group(function () {
    Route::get('trabajos/{work}', WorkShow::class)->name('works.show');
});

Route::middleware(['auth', 'technician'])->group(function () {
    Route::get('asignaciones', TechnicianAssignmentsIndex::class)->name('assignments.index');
    Route::get('asignaciones/{work}', fn (Work $work) => redirect()->route('works.show', $work))->name('assignments.show');
});

Route::middleware(['auth', 'noc'])->group(function () {
    Route::get('noc/ots-pendientes-cierre', NocTrackingIndex::class)->name('noc.tracking.open');
    Route::get('noc/ots-cerradas-revision', NocTrackingIndex::class)->name('noc.tracking.review');
});

Route::middleware(['auth', 'admin'])->group(function () {
    Route::get('usuarios/exportar/productividad', StaffProductivityExportController::class)->name('users.export.productivity');
    Route::get('usuarios', UserIndex::class)->name('users.index');
    Route::get('departamentos', DepartmentIndex::class)->name('departments.index');
    Route::get('provincias', ProvinceIndex::class)->name('provinces.index');
    Route::get('distritos', DistrictIndex::class)->name('districts.index');
});

require __DIR__.'/settings.php';
