<?php

use Illuminate\Support\Facades\Route;

Route::get('login', [\App\Http\Controllers\Auth\AuthController::class, 'showLoginForm'])->name('login');
Route::post('login', [\App\Http\Controllers\Auth\AuthController::class, 'login']);
Route::post('logout', [\App\Http\Controllers\Auth\AuthController::class, 'logout'])->name('logout');

Route::middleware('auth')->group(function () {
    Route::get('/', function () {
        return view('dashboard');
    })->name('dashboard');

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

    Route::get('profile', [\App\Http\Controllers\ProfileController::class, 'index'])->name('profile.index');
    Route::post('profile', [\App\Http\Controllers\ProfileController::class, 'update'])->name('profile.update');

    Route::resource('companies', \App\Http\Controllers\CompanyController::class);
    Route::resource('factories', \App\Http\Controllers\FactoryController::class);
    Route::resource('factory-zones', \App\Http\Controllers\FactoryZoneController::class);
    Route::resource('production-lines', \App\Http\Controllers\ProductionLineController::class);
    Route::resource('branches', \App\Http\Controllers\BranchController::class);
    Route::resource('machines', \App\Http\Controllers\MachineController::class);
    Route::resource('products', \App\Http\Controllers\ProductController::class);
    Route::resource('raw-materials', \App\Http\Controllers\RawMaterialController::class);
    Route::resource('boms', \App\Http\Controllers\BomController::class); // Alias for Consumption
    Route::resource('work-orders', \App\Http\Controllers\WorkOrderController::class);
    Route::resource('warehouses', \App\Http\Controllers\WarehouseController::class);

    // Phase 3: Quality & Maintenance
    // Fleet Management
    // Fleet Management
    Route::resource('fleet', \App\Http\Controllers\FleetController::class)->names('fleet')->except(['show']);
    Route::resource('fleet-loading', \App\Http\Controllers\FleetLoadingController::class)->names('fleet.loading')->except(['show']);

    Route::resource('quality-checks', \App\Http\Controllers\QualityController::class);
    Route::resource('maintenance', \App\Http\Controllers\MaintenanceController::class);

    // Phase 3: Supply Chain
    Route::resource('partners', \App\Http\Controllers\PartnerController::class);
    Route::resource('purchase-orders', \App\Http\Controllers\PurchaseOrderController::class);
    Route::resource('sales-orders', \App\Http\Controllers\SalesOrderController::class);

    Route::get('inventory', [\App\Http\Controllers\InventoryController::class, 'index'])->name('inventory.index');
    Route::get('inventory/adjust', [\App\Http\Controllers\InventoryController::class, 'adjust'])->name('inventory.adjust');
    Route::post('inventory/adjust', [\App\Http\Controllers\InventoryController::class, 'storeAdjustment'])->name('inventory.store-adjustment');
    Route::get('inventory/movements', [\App\Http\Controllers\InventoryController::class, 'movements'])->name('inventory.movements');

    Route::get('hr', [\App\Http\Controllers\HrController::class, 'index'])->name('hr.index');
    Route::get('analytics', [\App\Http\Controllers\AnalyticsController::class, 'index'])->name('analytics.index');
    Route::get('analytics/historical', [\App\Http\Controllers\AnalyticsController::class, 'historical'])->name('analytics.historical');

    Route::get('settings', [\App\Http\Controllers\SettingsController::class, 'index'])->name('settings.index');
    Route::post('settings', [\App\Http\Controllers\SettingsController::class, 'update'])->name('settings.update');

    // Reports Module
    Route::prefix('reports')->name('reports.')->group(function () {
        Route::get('/', [\App\Http\Controllers\ReportController::class, 'index'])->name('index');
        Route::get('/production', [\App\Http\Controllers\ReportController::class, 'production'])->name('production');
        Route::get('/inventory', [\App\Http\Controllers\ReportController::class, 'inventory'])->name('inventory');
        Route::post('/inventory/audit/{warehouse}', [\App\Http\Controllers\ReportController::class, 'audit'])->name('inventory.audit');
        Route::get('/sales', [\App\Http\Controllers\ReportController::class, 'sales'])->name('sales');
        Route::get('/maintenance', [\App\Http\Controllers\ReportController::class, 'maintenance'])->name('maintenance');
        Route::get('/quality', [\App\Http\Controllers\ReportController::class, 'quality'])->name('quality');
    });

    // AI Intelligence Module
    Route::prefix('intelligence')->name('intelligence.')->group(function () {
        Route::get('/', [\App\Http\Controllers\IntelligenceController::class, 'dashboard'])->name('dashboard');
        Route::get('/machines', [\App\Http\Controllers\IntelligenceController::class, 'machines'])->name('machines');
        Route::get('/risks', [\App\Http\Controllers\IntelligenceController::class, 'risks'])->name('risks');
        Route::get('/forecast', [\App\Http\Controllers\IntelligenceController::class, 'forecast'])->name('forecast');
        Route::post('/recalculate', [\App\Http\Controllers\IntelligenceController::class, 'recalculate'])->name('recalculate');
        Route::post('/alerts/{alert}/read', [\App\Http\Controllers\IntelligenceController::class, 'markAsRead'])->name('alerts.read');
    });

    // AI Chat Agent
    Route::post('/ai/ask', [\App\Http\Controllers\AiChatController::class, 'ask'])->name('ai.ask');

    Route::get('lang/{locale}', function ($locale) {
        if (in_array($locale, ['en', 'ar'])) {
            session(['locale' => $locale]);
        }
        return redirect()->back();
    })->name('lang.switch');
});
