<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\WebUserController;
use App\Http\Controllers\Admin\WebLoadController;
use App\Http\Controllers\Admin\WebBillController;
use App\Http\Controllers\Admin\WebDriverController;
use App\Http\Controllers\Admin\WebReportController;
use App\Http\Controllers\Admin\WebAppVersionController;
use App\Http\Controllers\Admin\DriverManagementController;
use App\Http\Controllers\Admin\SystemSettingsController;
use App\Http\Controllers\Admin\WebserviceDocsController;
use App\Http\Controllers\Admin\AdminManagementController;
use App\Http\Controllers\Admin\InternalWaybillController;
use App\Http\Controllers\Admin\MasterDataController;
use App\Http\Controllers\PublicWaybillController;

/*
|--------------------------------------------------------------------------
| 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!
|
*/

// PWA صفحه اصلی
Route::get('/', function () {
    return view('pwa.index');
});

// دستورالعمل برای ورود مستقیم به ادمین
Route::get('/admin', function () {
    return redirect()->route('admin.login');
});

/*
|--------------------------------------------------------------------------
| Admin Panel Routes
|--------------------------------------------------------------------------
|
| These routes are for the admin panel functionality.
| All routes require authentication and admin role.
|
*/

// Admin Authentication Routes
Route::prefix('admin')->name('admin.')->group(function () {
    Route::get('/login', [App\Http\Controllers\Admin\AuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [App\Http\Controllers\Admin\AuthController::class, 'login'])->name('login.post');
    Route::post('/logout', [App\Http\Controllers\Admin\AuthController::class, 'logout'])->name('logout');
});

// Protected Admin Routes
Route::prefix('admin')->middleware(['auth', 'admin', 'activity.log'])->name('admin.')->group(function () {
    // Dashboard
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // User Management
    Route::resource('users', WebUserController::class);
    Route::post('/users/{id}/toggle-status', [WebUserController::class, 'toggleStatus'])->name('users.toggle-status');

    // Load Management
    Route::resource('loads', WebLoadController::class)->only(['index', 'show', 'destroy']);
    Route::post('/loads/{id}/approve', [WebLoadController::class, 'approve'])->name('loads.approve');
    Route::post('/loads/{id}/assign-driver', [WebLoadController::class, 'assignDriver'])->name('loads.assign-driver');
    Route::post('/loads/{id}/update-status', [WebLoadController::class, 'updateStatus'])->name('loads.update-status');

    // Bill Management
    Route::resource('bills', WebBillController::class)->only(['index', 'show']);
    Route::get('/bills/create-internal', [InternalWaybillController::class, 'create'])->name('bills.create-internal');
    Route::post('/bills/store-internal', [InternalWaybillController::class, 'store'])->name('bills.store-internal');

    // Driver Management
    Route::resource('drivers', WebDriverController::class)->only(['index', 'show']);
    Route::get('/drivers/{id}/details', [WebDriverController::class, 'details'])->name('drivers.details');

    // Reports
    Route::prefix('reports')->name('reports.')->group(function () {
        Route::get('/', [WebReportController::class, 'index'])->name('index');
        Route::get('/generate', [WebReportController::class, 'generate'])->name('generate');
        Route::get('/financial', [WebReportController::class, 'financial'])->name('financial');
        Route::get('/accounting', [WebReportController::class, 'accounting'])->name('accounting');
        Route::get('/loads', [WebReportController::class, 'loads'])->name('loads');
        Route::get('/drivers', [WebReportController::class, 'drivers'])->name('drivers');
    });

    // Master Data and Financial Configuration
    Route::prefix('master-data')->name('master-data.')->group(function () {
        Route::get('/', [MasterDataController::class, 'index'])->name('index');

        Route::post('/cities', [MasterDataController::class, 'storeCity'])->name('cities.store');
        Route::post('/cities/{city}', [MasterDataController::class, 'updateCity'])->name('cities.update');
        Route::post('/cities/{city}/delete', [MasterDataController::class, 'destroyCity'])->name('cities.destroy');

        Route::post('/cargo-items', [MasterDataController::class, 'storeCargoItem'])->name('cargo-items.store');
        Route::post('/cargo-items/{cargoItem}', [MasterDataController::class, 'updateCargoItem'])->name('cargo-items.update');
        Route::post('/cargo-items/{cargoItem}/delete', [MasterDataController::class, 'destroyCargoItem'])->name('cargo-items.destroy');

        Route::post('/fleet-types', [MasterDataController::class, 'storeFleetType'])->name('fleet-types.store');
        Route::post('/fleet-types/{fleetType}', [MasterDataController::class, 'updateFleetType'])->name('fleet-types.update');
        Route::post('/fleet-types/{fleetType}/delete', [MasterDataController::class, 'destroyFleetType'])->name('fleet-types.destroy');

        Route::post('/route-types', [MasterDataController::class, 'storeRouteType'])->name('route-types.store');
        Route::post('/route-types/{routeType}', [MasterDataController::class, 'updateRouteType'])->name('route-types.update');
        Route::post('/route-types/{routeType}/delete', [MasterDataController::class, 'destroyRouteType'])->name('route-types.destroy');

        Route::post('/transport-routes', [MasterDataController::class, 'storeTransportRoute'])->name('transport-routes.store');
        Route::post('/transport-routes/{transportRoute}', [MasterDataController::class, 'updateTransportRoute'])->name('transport-routes.update');
        Route::post('/transport-routes/{transportRoute}/delete', [MasterDataController::class, 'destroyTransportRoute'])->name('transport-routes.destroy');

        Route::post('/financial-coefficients', [MasterDataController::class, 'storeFinancialCoefficient'])->name('financial-coefficients.store');
        Route::post('/financial-coefficients/{financialCoefficient}', [MasterDataController::class, 'updateFinancialCoefficient'])->name('financial-coefficients.update');
        Route::post('/financial-coefficients/{financialCoefficient}/delete', [MasterDataController::class, 'destroyFinancialCoefficient'])->name('financial-coefficients.destroy');

        Route::post('/financial-items', [MasterDataController::class, 'storeFinancialItem'])->name('financial-items.store');
        Route::post('/financial-items/{financialItem}', [MasterDataController::class, 'updateFinancialItem'])->name('financial-items.update');
        Route::post('/financial-items/{financialItem}/delete', [MasterDataController::class, 'destroyFinancialItem'])->name('financial-items.destroy');
    });

    // App Version Management
    Route::resource('app-versions', WebAppVersionController::class);
    Route::post('/app-versions/{id}/set-active', [WebAppVersionController::class, 'setActive'])->name('app-versions.set-active');

    // Map
    Route::get('/map', [DriverManagementController::class, 'map'])->name('map');
    Route::get('/map/refresh', [DriverManagementController::class, 'mapRefresh'])->name('map.refresh');

    // RMTO Management
    Route::prefix('rmto')->name('rmto.')->group(function () {
        Route::get('/', [App\Http\Controllers\Admin\RMTOController::class, 'index'])->name('index');
        Route::get('/settings', [App\Http\Controllers\Admin\RMTOController::class, 'getSettings'])->name('settings');
        Route::post('/settings', [App\Http\Controllers\Admin\RMTOController::class, 'updateSettings'])->name('settings.update');
        Route::post('/test-connection', [App\Http\Controllers\Admin\RMTOController::class, 'testConnection'])->name('test-connection');
        Route::post('/test-bill', [App\Http\Controllers\Admin\RMTOController::class, 'testBillOfLading'])->name('test-bill');
        Route::post('/driver-info', [App\Http\Controllers\Admin\RMTOController::class, 'getDriverInfo'])->name('driver-info');
        Route::post('/freighter-info', [App\Http\Controllers\Admin\RMTOController::class, 'getFreighterInfo'])->name('freighter-info');
        Route::post('/send-otp', [App\Http\Controllers\Admin\RMTOController::class, 'sendOTP'])->name('send-otp');
        Route::get('/current-date', [App\Http\Controllers\Admin\RMTOController::class, 'getCurrentDate'])->name('current-date');
        Route::post('/inquiry/real-person', [App\Http\Controllers\Admin\RMTOController::class, 'inquiryRealPerson'])->name('inquiry.real-person');
        Route::post('/inquiry/postal-code', [App\Http\Controllers\Admin\RMTOController::class, 'inquiryPostalCode'])->name('inquiry.postal-code');
    });

    // Webservice Documentation
    Route::prefix('webservice-docs')->name('webservice-docs.')->group(function () {
        Route::get('/', [WebserviceDocsController::class, 'index'])->name('index');
        Route::get('/swagger-yaml', [WebserviceDocsController::class, 'swaggerYaml'])->name('yaml');
    });

    // System Settings
    Route::prefix('system-settings')->name('system-settings.')->group(function () {
        Route::get('/', [SystemSettingsController::class, 'index'])->name('index');
        Route::post('/', [SystemSettingsController::class, 'update'])->name('update');
        Route::post('/test-sms', [SystemSettingsController::class, 'testSmsPattern'])->name('test-sms');
        Route::post('/reset', [SystemSettingsController::class, 'resetSystem'])->name('reset');
    });

    // Admin Management
    Route::prefix('admins')->name('admins.')->group(function () {
        Route::get('/', [AdminManagementController::class, 'index'])->name('index');
        Route::post('/', [AdminManagementController::class, 'store'])->name('store');
        Route::post('/{id}/password', [AdminManagementController::class, 'updatePassword'])->name('password.update');
        Route::post('/my-password', [AdminManagementController::class, 'updateMyPassword'])->name('my-password.update');
    });

    // Freight Company Management
    Route::resource('freight-companies', App\Http\Controllers\Admin\FreightCompanyController::class);
    Route::post('/freight-companies/{freightCompany}/approve', [App\Http\Controllers\Admin\FreightCompanyController::class, 'approve'])->name('freight-companies.approve');
    Route::post('/freight-companies/{freightCompany}/reject', [App\Http\Controllers\Admin\FreightCompanyController::class, 'reject'])->name('freight-companies.reject');
});

Auth::routes();

Route::get('/home', function() {
    if (Auth::check() && Auth::user()->isAdmin()) {
        return redirect()->route('admin.dashboard');
    }
    return view('home');
})->middleware('auth')->name('home');

Route::get('/waybill/print/{trackingCode}', [PublicWaybillController::class, 'showPrint'])
    ->name('waybill.print');
