<?php

use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ImportController;
use App\Http\Controllers\ImportRequestController;
use App\Http\Controllers\ImportRequestDetailController;

Route::get('/', function () {
    return view('welcome');
});

Route::get('/dashboard', [App\Http\Controllers\DashboardController::class, 'index'])
    ->middleware(['auth', 'verified'])
    ->name('dashboard');

Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'index'])->name('profile');
    Route::get('/profile/edit', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
    Route::get('/profile/change-password', [ProfileController::class, 'showChangePasswordForm'])->name('profile.change-password');
    Route::post('/profile/change-password', [ProfileController::class, 'updatePassword'])->name('profile.update-password');
});

Route::post('/logout', function () {
    Auth::logout();
    request()->session()->invalidate();
    request()->session()->regenerateToken();
    return redirect('/login'); // redirect to login page
})->name('logout');

Route::middleware(['auth'])->group(function () {
    Route::get('/import/supplier_index', [ImportController::class, 'supplier_index'])->name('import.supplier_index');
    Route::get('/import/create_supplier', [ImportController::class, 'create_supplier'])->name('import.create_supplier');
    Route::post('/import/store_supplier', [ImportController::class, 'store_supplier'])->name('import.store_supplier');
    Route::get('/import/supplier_show/{import}', [ImportController::class, 'supplier_show'])->name('import.supplier_show');
    Route::get('/import/supplier_edit/{id}', [ImportController::class, 'edit_supplier'])->name('import.edit_supplier');
    Route::post('/import/supplier_update/{id}', [ImportController::class, 'update_supplier'])->name('import.update_supplier');

});

Route::middleware(['auth'])->group(function () {

    // Import Request Details Routes
    Route::get('/import_request_details/{id}/edit', [App\Http\Controllers\ImportRequestDetailController::class, 'edit'])->name('import_requests.details.edit')->middleware('role:ImPex|Admin');
    Route::put('/import_request_details/{id}', [App\Http\Controllers\ImportRequestDetailController::class, 'update'])->name('import_requests.details.update')->middleware('role:ImPex|Admin');
    Route::get('/import_request_details/{id}/download_other_attachment', [App\Http\Controllers\ImportRequestDetailController::class, 'downloadOtherAttachment'])->name('import_requests.download_other_attachment');

    Route::get('/export_request_details/{id}/edit', [App\Http\Controllers\ExportRequestDetailController::class, 'edit'])->name('export_request_details.edit')->middleware('role:ImPex|Admin');
    Route::put('/export_request_details/{id}', [App\Http\Controllers\ExportRequestDetailController::class, 'update'])->name('export_request_details.update')->middleware('role:ImPex|Admin');

    Route::get('import_requests/files/{id}', [ImportRequestController::class, 'downloadAttachment'])
        ->name('import_requests.download_attachment');

    Route::get('import_requests/other_files/{id}', [ImportRequestController::class, 'downloadOtherAttachment'])
        ->name('import_requests.download_other_attachment');

    Route::resource('import_requests', ImportRequestController::class)->except(['destroy']);
    Route::delete('import_requests/{import_request}', [ImportRequestController::class, 'destroy'])->name('import_requests.destroy')->middleware('role:ImPex|Admin');
    Route::resource('users', App\Http\Controllers\UserController::class)->middleware('role:admin|Admin');
    Route::resource('roles', App\Http\Controllers\RoleController::class)->middleware('role:admin|Admin');
    Route::get('activity-logs', [App\Http\Controllers\ActivityLogController::class, 'index'])->name('activity_logs.index')->middleware('role:admin|Admin');

    // Export Ship To Address
    Route::resource('export/ship_to_address', App\Http\Controllers\ExportShipToAddressController::class, ['names' => 'export.ship_to_address']);

    // Export Requests
    Route::resource('export_requests', App\Http\Controllers\ExportRequestController::class)->except(['destroy']);
    Route::delete('export_requests/{export_request}', [App\Http\Controllers\ExportRequestController::class, 'destroy'])->name('export_requests.destroy')->middleware('role:ImPex|Admin');
    Route::get('export_requests/{id}/download_attachment', [App\Http\Controllers\ExportRequestController::class, 'downloadAttachment'])->name('export_requests.download_attachment');
    Route::get('export_requests/{id}/download_other_attachment', [App\Http\Controllers\ExportRequestDetailController::class, 'downloadOtherAttachment'])->name('export_requests.download_other_attachment');
    Route::get('export_requests/{id}/clone', [App\Http\Controllers\ExportRequestController::class, 'clone'])->name('export_requests.clone');
    Route::get('export_requests/{id}/dhl-csv', [App\Http\Controllers\ExportRequestController::class, 'downloadDhlCsv'])->name('export_requests.dhl_csv')->middleware('role:ImPex|Admin');
    Route::get('export_requests/{id}/fedex-excel', [App\Http\Controllers\ExportRequestController::class, 'downloadFedExExcel'])->name('export_requests.fedex_excel')->middleware('role:ImPex|Admin');

    // Export Reports
    Route::prefix('export_reports')->name('export_reports.')->group(function () {
        Route::get('/', [App\Http\Controllers\ExportReportController::class, 'index'])->name('index');
        Route::get('/by_address', [App\Http\Controllers\ExportReportController::class, 'byAddress'])->name('by_address');
        Route::get('/by_courier', [App\Http\Controllers\ExportReportController::class, 'byCourier'])->name('by_courier');
        Route::get('/by_submitter', [App\Http\Controllers\ExportReportController::class, 'bySubmitter'])->name('by_submitter');
        Route::get('/by_date', [App\Http\Controllers\ExportReportController::class, 'byDate'])->name('by_date');
        Route::get('/pending_awb', [App\Http\Controllers\ExportReportController::class, 'pendingAwb'])->name('pending_awb');
    });
    // Notifications
    Route::get('notifications/{id}/read', [App\Http\Controllers\NotificationController::class, 'markAsRead'])->name('notifications.read');
    Route::get('notifications/fetch-unread', [App\Http\Controllers\NotificationController::class, 'fetchUnread'])->name('notifications.fetch');
    Route::get('notifications/read-all', [App\Http\Controllers\NotificationController::class, 'markAllAsRead'])->name('notifications.readAll');

});

require __DIR__.'/auth.php';
