<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\TelecallingDashboardController;
use App\Http\Controllers\AssetDashboardController;
use App\Http\Controllers\TodoDashboardController;
use App\Http\Controllers\Admin\TodoUserController;
use App\Http\Controllers\Todo\ActivationController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\DepartmentController;
use App\Http\Controllers\Admin\TaskController;
use App\Http\Controllers\Admin\TaskAssignmentController;
use App\Http\Controllers\Todo\MyTasksController;
use App\Http\Controllers\Tele\DashboardController as TeleDashboardController;
use App\Http\Controllers\Tele\RecordController as TeleRecordController;
use App\Http\Controllers\Asset\EmpDepController;
use App\Http\Controllers\Asset\EmployeeController;
use App\Http\Controllers\Asset\RepairController;
// Auth

use App\Http\Controllers\Asset\ScrapController;

Route::get('/', [AuthController::class, 'showLogin'])->name('login');
Route::get('/login', [AuthController::class, 'showLogin']);
Route::post('/login', [AuthController::class, 'login'])->name('login.post');
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

// Admin area (only Admin)
Route::middleware(['auth','role:admin'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    Route::get('/todo-users', [TodoUserController::class, 'index'])->name('todo-users.index');
    Route::get('/todo-users/create', [TodoUserController::class, 'create'])->name('todo-users.create');
    Route::post('/todo-users', [TodoUserController::class, 'store'])->name('todo-users.store');
    Route::post('/todo-users/{user}/resend-invite', [TodoUserController::class, 'resend'])->name('todo-users.resend');

    Route::patch('/todo-users/{user}/deactivate', [TodoUserController::class, 'deactivate'])->name('todo-users.deactivate');
    Route::patch('/todo-users/{user}/activate',   [TodoUserController::class, 'activate'])->name('todo-users.activate');
    Route::delete('/todo-users/{user}',           [TodoUserController::class, 'destroy'])->name('todo-users.destroy');
    Route::post('/todo-users/{id}/restore',       [TodoUserController::class, 'restore'])->name('todo-users.restore');

    Route::resource('departments', DepartmentController::class)->except(['show']);
    Route::resource('tasks', TaskController::class);

    // Assign/unassign (bulk or single)
    Route::post('tasks/{task}/assign', [TaskAssignmentController::class, 'assign'])->name('tasks.assign');
    Route::delete('tasks/{task}/assignees/{user}', [TaskAssignmentController::class, 'unassign'])->name('tasks.unassign');



});

// To-Do activation links (no auth)
Route::get('/todo/set-password/{token}', [ActivationController::class, 'show'])->name('todo.activate.show');
Route::post('/todo/set-password/{token}', [ActivationController::class, 'store'])->name('todo.activate.store');

// Telecalling module (current canonical routes)
Route::middleware(['auth','role:telecalling'])->prefix('tele')->name('tele.')->group(function () {
    Route::get('/dashboard', [TeleDashboardController::class, 'index'])->name('dashboard');
    Route::resource('records', TeleRecordController::class)->except(['show']);
        // NEW: stream (inline play) and download
    Route::get('records/{record}/audio',    [TeleRecordController::class, 'audio'])->name('records.audio');
    Route::get('records/{record}/download', [TeleRecordController::class, 'download'])->name('records.download');
});

Route::middleware(['auth','role:asset'])->prefix('asset')->name('asset.')->group(function () {
    Route::get('/dashboard', [AssetDashboardController::class, 'index'])->name('dashboard');

    // Employee Departments
    Route::get('/empdeps', [EmpDepController::class, 'index'])->name('empdeps.index');
    Route::post('/empdeps', [EmpDepController::class, 'store'])->name('empdeps.store');
    Route::get('/empdeps/{id}/toggle', [EmpDepController::class, 'toggle'])->name('empdeps.toggle');
    Route::delete('/empdeps/{id}', [EmpDepController::class, 'destroy'])->name('empdeps.destroy');

    // 👇 NEW: Employees
    Route::get('/employees', [EmployeeController::class, 'index'])->name('employees.index');
    Route::post('/employees', [EmployeeController::class, 'store'])->name('employees.store');
    Route::get('/employees/{id}/toggle', [EmployeeController::class, 'toggle'])->name('employees.toggle');
    Route::delete('/employees/{id}', [EmployeeController::class, 'destroy'])->name('employees.destroy');
    Route::get('/employees/{id}/edit', [EmployeeController::class, 'edit'])->name('employees.edit');
    Route::put('/employees/{id}', [EmployeeController::class, 'update'])->name('employees.update');

    Route::resource('assets', \App\Http\Controllers\Asset\AssetController::class);

    Route::get('assignments', [App\Http\Controllers\Asset\AssetAssignmentController::class,'index'])->name('assignments.index');
    Route::post('assignments', [App\Http\Controllers\Asset\AssetAssignmentController::class,'store'])->name('assignments.store');
    Route::post('assignments/{id}/return', [App\Http\Controllers\Asset\AssetAssignmentController::class,'returnAsset'])->name('assignments.return');
    Route::delete('assignments/{id}', [App\Http\Controllers\Asset\AssetAssignmentController::class,'destroy'])->name('assignments.destroy');
    Route::get('assignments/history/{asset_id}', [App\Http\Controllers\Asset\AssetAssignmentController::class, 'history']);

    Route::get('repairs', [RepairController::class, 'index'])->name('repairs.index');
    Route::post('repairs', [RepairController::class, 'store'])->name('repairs.store');
    Route::post('repairs/{id}/return', [RepairController::class, 'markReturned'])->name('repairs.return');
    Route::delete('repairs/{id}', [RepairController::class, 'destroy'])->name('repairs.destroy');

    // AJAX: assets by employee
    Route::get('repairs/assets/{employeeId}', [RepairController::class, 'assetsByEmployee'])
        ->name('repairs.assetsByEmployee');

    // ✅ Invoice routes (note: NO extra 'asset.' here)
    Route::get('repairs/{id}/invoice',  [RepairController::class, 'viewInvoice'])->name('repairs.invoice.view');
    Route::get('repairs/{id}/download', [RepairController::class, 'downloadInvoice'])->name('repairs.invoice.download');

    // ============= SCRAPS =============
    Route::get('scraps', [ScrapController::class, 'index'])->name('scraps.index');
    Route::post('scraps', [ScrapController::class, 'store'])->name('scraps.store');
    Route::post('scraps/{id}/restore', [ScrapController::class, 'restore'])->name('scraps.restore');
    Route::delete('scraps/{id}', [ScrapController::class, 'destroy'])->name('scraps.destroy');

    // certificate view/download
    Route::get('scraps/{id}/certificate', [ScrapController::class, 'viewCertificate'])->name('scraps.certificate.view');
    Route::get('scraps/{id}/download',    [ScrapController::class, 'downloadCertificate'])->name('scraps.certificate.download');

    Route::get('assets-overview', [\App\Http\Controllers\Asset\AssetOverviewController::class, 'index'])
        ->name('assets.overview');

        // assignment history already exists (JSON): GET /asset/assignments/history/{asset_id}
    Route::get('repairs/history/{assetId}', [\App\Http\Controllers\Asset\RepairController::class, 'historyByAsset'])
        ->name('repairs.history');

        // routes/web.php  (inside Route::middleware(['auth','role:asset'])->prefix('asset')->name('asset.')->group(function () { ... })
    Route::get('dashboard/export', [\App\Http\Controllers\AssetDashboardController::class, 'export'])
        ->name('dashboard.export');



    
});

// To-Do module
Route::middleware(['auth','role:todo'])->prefix('todo')->name('todo.')->group(function () {
    Route::get('/dashboard', [TodoDashboardController::class, 'index'])->name('dashboard');
    // My tasks (To-Do user)
    Route::get('/my-tasks', [MyTasksController::class, 'index'])->name('my-tasks');
    Route::patch('/my-tasks/{task}/status', [MyTasksController::class, 'updateStatus'])->name('my-tasks.update-status');
});

/*
|--------------------------------------------------------------------------
| Backward-compatibility aliases (optional, keeps old telecalling.* names working)
|--------------------------------------------------------------------------
*/
Route::middleware(['auth','role:telecalling'])->prefix('telecalling')->name('telecalling.')->group(function () {
    // Old: telecalling.dashboard
    Route::get('/dashboard', [TeleDashboardController::class, 'index'])->name('dashboard');

    // Old: telecalling.records.*
    Route::resource('records', TeleRecordController::class)->except(['show'])->names([
        'index'   => 'records.index',
        'create'  => 'records.create',
        'store'   => 'records.store',
        'edit'    => 'records.edit',
        'update'  => 'records.update',
        'destroy' => 'records.destroy',
    ]);
});
