<?php

use App\Http\Controllers\CustomerController;
use App\Http\Controllers\ServiceOrderController;
use App\Http\Controllers\VehicleController;
use Illuminate\Support\Facades\Route;
use Livewire\Volt\Volt;

Route::get('/', function () {
    if (auth()->check()) {
        $user = auth()->user();

        // Check if user is a customer
        if ($user->isCustomer()) {
            // Redirect customers to customer portal
            return redirect('/customer-portal');
        } else {
            // Redirect admin/staff to dashboard
            return redirect('/dashboard');
        }
    }

    // For guests, redirect to login page instead of showing welcome
    return redirect('/login');
})->name('home');

Route::view('dashboard', 'dashboard')
    ->middleware(['auth', 'verified', 'admin.only'])
    ->name('dashboard');

Route::middleware(['auth', 'admin.only'])->group(function () {
    // Settings routes
    Route::redirect('settings', 'settings/general');
    Volt::route('settings/profile', 'settings.profile')->name('settings.profile');
    Volt::route('settings/password', 'settings.password')->name('settings.password');

    // Application Settings Routes
    Route::prefix('settings')->name('settings.')->group(function () {
        Route::get('/general', [App\Http\Controllers\SettingsController::class, 'general'])->name('general');
        Route::put('/general', [App\Http\Controllers\SettingsController::class, 'updateGeneral'])->name('general.update');

        Route::get('/service', [App\Http\Controllers\SettingsController::class, 'service'])->name('service');
        Route::put('/service', [App\Http\Controllers\SettingsController::class, 'updateService'])->name('service.update');

        Route::get('/inventory', [App\Http\Controllers\SettingsController::class, 'inventory'])->name('inventory');
        Route::put('/inventory', [App\Http\Controllers\SettingsController::class, 'updateInventory'])->name('inventory.update');

        Route::get('/notifications', [App\Http\Controllers\SettingsController::class, 'notifications'])->name('notifications');
        Route::put('/notifications', [App\Http\Controllers\SettingsController::class, 'updateNotifications'])->name('notifications.update');

        Route::get('/security', [App\Http\Controllers\SettingsController::class, 'security'])->name('security');
        Route::put('/security', [App\Http\Controllers\SettingsController::class, 'updateSecurity'])->name('security.update');
    });
    Volt::route('settings/appearance', 'settings.appearance')->name('settings.appearance');

    // Car Repair System Routes
    Route::resource('customers', CustomerController::class)->middleware('permission:customers.view');
    Route::resource('vehicles', VehicleController::class)->middleware('permission:vehicles.view');
    Route::resource('service-orders', ServiceOrderController::class)->middleware('permission:service-orders.view');

    // Additional Service Order Routes
    Route::get('/service-orders/{serviceOrder}/invoice', [ServiceOrderController::class, 'invoice'])
        ->middleware('permission:service-orders.view')
        ->name('service-orders.invoice');

    // Livewire Component Routes
    Route::get('/customers-list', function () {
        return view('customers.index');
    })->middleware('permission:customers.view')->name('customers.list');

    Route::get('/service-orders-list', function () {
        return view('service-orders.index');
    })->middleware('permission:service-orders.view')->name('service-orders.list');

    Route::get('/vehicles', function () {
        return view('vehicles.index');
    })->middleware('permission:vehicles.view')->name('vehicles.index');

    Route::get('/appointments', function () {
        return view('appointments.index');
    })->middleware('permission:appointments.view')->name('appointments.index');

    Route::get('/service-items', function () {
        return view('service-items.index');
    })->middleware('permission:service-orders.view')->name('service-items.index');

    // Appointments Management Routes
    Route::prefix('appointments')->name('appointments.')->middleware('permission:appointments.view')->group(function () {
        Route::get('/', \App\Livewire\Appointments\Index::class)->name('index');
        Route::get('/create', \App\Livewire\Appointments\Create::class)->middleware('permission:appointments.create')->name('create');
        Route::get('/{appointment}/edit', \App\Livewire\Appointments\Form::class)->middleware('permission:appointments.update')->name('edit');
        Route::get('/calendar', \App\Livewire\Appointments\Calendar::class)->name('calendar');
    });

    Route::get('/inventory', [App\Http\Controllers\InventoryController::class, 'index'])
        ->middleware('permission:inventory.view')
        ->name('inventory.index');

    // Inventory Management Routes
    Route::prefix('inventory')->name('inventory.')->middleware('permission:inventory.view')->group(function () {
        Route::get('/dashboard', \App\Livewire\Inventory\Dashboard::class)->name('dashboard');

        // Parts Routes
        Route::prefix('parts')->name('parts.')->group(function () {
            Route::get('/', \App\Livewire\Inventory\Parts\Index::class)->name('index');
            Route::get('/create', \App\Livewire\Inventory\Parts\Create::class)->middleware('permission:inventory.create')->name('create');
            Route::get('/{part}', \App\Livewire\Inventory\Parts\Show::class)->name('show');
            Route::get('/{part}/edit', \App\Livewire\Inventory\Parts\Edit::class)->middleware('permission:inventory.update')->name('edit');
        });

        // Suppliers Routes
        Route::prefix('suppliers')->name('suppliers.')->group(function () {
            Route::get('/', \App\Livewire\Inventory\Suppliers\Index::class)->name('index');
            Route::get('/create', \App\Livewire\Inventory\Suppliers\Create::class)->middleware('permission:inventory.create')->name('create');
            Route::get('/{supplier}/edit', \App\Livewire\Inventory\Suppliers\Edit::class)->middleware('permission:inventory.update')->name('edit');
        });

        // Purchase Orders Routes
        Route::prefix('purchase-orders')->name('purchase-orders.')->middleware('permission:inventory.purchase-orders')->group(function () {
            Route::get('/', \App\Livewire\Inventory\PurchaseOrders\Index::class)->name('index');
            Route::get('/create', \App\Livewire\Inventory\PurchaseOrders\Create::class)->name('create');
            Route::get('/{purchaseOrder}/edit', \App\Livewire\Inventory\PurchaseOrders\Edit::class)->name('edit');
            Route::get('/{purchaseOrder}', \App\Livewire\Inventory\PurchaseOrders\Show::class)->name('show');
        });

        // Stock Movements Routes
        Route::prefix('stock-movements')->name('stock-movements.')->middleware('permission:inventory.stock-movements')->group(function () {
            Route::get('/', \App\Livewire\Inventory\StockMovements\Index::class)->name('index');
            Route::get('/create', \App\Livewire\Inventory\StockMovements\Create::class)->name('create');
        });
    });

    Route::get('/technicians', function () {
        return view('technicians.index');
    })->middleware('permission:technicians.view')->name('technicians.index');

    // Technician Management Routes
    Route::prefix('technician')->name('technician.')->middleware('permission:technicians.view')->group(function () {
        Route::get('/management', function () {
            return view('technician-management');
        })->middleware('permission:technicians.update')->name('management');

        Route::get('/skills', function () {
            return view('technician-skills');
        })->name('skills');

        Route::get('/reports', function () {
            return view('technician-reports');
        })->middleware('permission:technicians.view-performance')->name('reports');
    });

    // Job Card Management Routes
    Route::prefix('job-cards')->name('job-cards.')->middleware('permission:job-cards.view')->group(function () {
        Route::get('/', \App\Livewire\JobCards\Index::class)->name('index');
        Route::get('/test', function () {
            $component = new \App\Livewire\JobCards\Index;
            $component->mount();

            return $component->render();
        })->name('test');
        Route::get('/create', \App\Livewire\JobCards\Create::class)->middleware('permission:job-cards.create')->name('create');
        Route::get('/{jobCard}', \App\Livewire\JobCards\Show::class)->name('show');
        Route::get('/{jobCard}/edit', \App\Livewire\JobCards\Edit::class)->middleware('permission:job-cards.update')->name('edit');
        Route::get('/{jobCard}/workflow', \App\Livewire\JobCards\WorkflowStatus::class)->name('workflow');
    });

    // Diagnosis Routes
    Route::prefix('diagnosis')->name('diagnosis.')->middleware('permission:job-cards.view')->group(function () {
        Route::get('/', \App\Livewire\Diagnosis\Index::class)->name('index');
        Route::get('/create/{jobCard}', \App\Livewire\Diagnosis\Create::class)->middleware('permission:job-cards.update')->name('create');
        Route::get('/{diagnosis}', \App\Livewire\Diagnosis\Show::class)->name('show');
        Route::get('/{diagnosis}/edit', \App\Livewire\Diagnosis\Edit::class)->middleware('permission:job-cards.update')->name('edit');
    });

    // Estimates Routes
    Route::prefix('estimates')->name('estimates.')->middleware('permission:service-orders.view')->group(function () {
        Route::get('/', \App\Livewire\Estimates\Index::class)->name('index');
        Route::get('/create', \App\Livewire\Estimates\CreateStandalone::class)->middleware('permission:service-orders.create')->name('create-standalone');
        Route::get('/create/{diagnosis}', \App\Livewire\Estimates\Create::class)->middleware('permission:service-orders.create')->name('create');
        Route::get('/{estimate}', \App\Livewire\Estimates\Show::class)->name('show');
        Route::get('/{estimate}/edit', \App\Livewire\Estimates\Edit::class)->middleware('permission:service-orders.update')->name('edit');
        Route::get('/{estimate}/pdf', \App\Livewire\Estimates\PDF::class)->name('pdf');
    });

    // Work Orders Routes
    Route::prefix('work-orders')->name('work-orders.')->middleware('permission:service-orders.view')->group(function () {
        Route::get('/', \App\Livewire\WorkOrders\Index::class)->name('index');
        Route::get('/create/{estimate}', \App\Livewire\WorkOrders\Create::class)->middleware('permission:service-orders.create')->name('create');
        Route::get('/{workOrder}', \App\Livewire\WorkOrders\Show::class)->name('show');
        Route::get('/{workOrder}/edit', \App\Livewire\WorkOrders\Edit::class)->middleware('permission:service-orders.update')->name('edit');
    });

    // Inspections Routes
    Route::prefix('inspections')->name('inspections.')->middleware('permission:job-cards.view')->group(function () {
        Route::get('/', \App\Livewire\Inspections\Index::class)->name('index');
        Route::get('/create/{jobCard}/{type}', \App\Livewire\Inspections\Create::class)->middleware('permission:job-cards.update')->name('create');
        Route::get('/{inspection}', \App\Livewire\Inspections\Show::class)->name('show');
        Route::get('/{inspection}/print', \App\Livewire\Inspections\PrintView::class)->name('print');
        Route::get('/{inspection}/edit', \App\Livewire\Inspections\Edit::class)->middleware('permission:job-cards.update')->name('edit');
    });

    // Timesheets Routes
    Route::prefix('timesheets')->name('timesheets.')->middleware('permission:technicians.view')->group(function () {
        Route::get('/', \App\Livewire\Timesheets\Index::class)->name('index');
        Route::get('/create', \App\Livewire\Timesheets\Create::class)->name('create');
        Route::get('/{timesheet}', \App\Livewire\Timesheets\Show::class)->name('show');
        Route::get('/{timesheet}/edit', \App\Livewire\Timesheets\Edit::class)->name('edit');
    });

    // Reports Dashboard Route
    Route::view('reports', 'reports')->middleware(['auth', 'permission:reports.view'])->name('reports.index');

    // Branch Management Routes
    Route::prefix('branches')->name('branches.')->middleware('permission:branches.view')->group(function () {
        Route::get('/', \App\Livewire\Branches\Index::class)->name('index');
        Route::get('/create', \App\Livewire\Branches\Create::class)->middleware('permission:branches.create')->name('create');
        Route::get('/{branch}/edit', \App\Livewire\Branches\Edit::class)->middleware('permission:branches.edit')->name('edit');
    });

    // User Management Routes
    Route::prefix('users')->name('users.')->middleware('permission:users.view')->group(function () {
        Route::get('/', \App\Livewire\Users\Index::class)->name('index');
        Route::get('/create', \App\Livewire\Users\Create::class)->middleware('permission:users.create')->name('create');
        Route::get('/{user}', \App\Livewire\Users\Show::class)->name('show');
        Route::get('/{user}/edit', \App\Livewire\Users\Edit::class)->middleware('permission:users.update')->name('edit');
        Route::get('/{user}/manage-roles', \App\Livewire\Users\ManageRolesPermissions::class)->middleware('permission:users.manage-roles')->name('manage-roles');
    });

    // Legacy User Management Route (redirect to new structure)
    Route::get('/user-management', function () {
        return redirect()->route('users.index');
    })->middleware(['auth', 'permission:users.view'])->name('user-management');
});

// Customer Portal Routes (Only auth middleware, accessible to customers)
Route::prefix('customer-portal')->name('customer-portal.')->middleware(['auth'])->group(function () {
    Route::get('/', \App\Livewire\CustomerPortal\Dashboard::class)->name('dashboard');
    Route::get('/appointments', \App\Livewire\CustomerPortal\Appointments::class)->name('appointments');
    Route::get('/vehicles', \App\Livewire\CustomerPortal\Vehicles::class)->name('vehicles');
    Route::get('/work-orders', \App\Livewire\CustomerPortal\WorkOrders::class)->name('work-orders');
    Route::get('/estimates', \App\Livewire\CustomerPortal\Estimates::class)->name('estimates');
    Route::get('/invoices', \App\Livewire\CustomerPortal\Invoices::class)->name('invoices');
    Route::get('/status/{jobCard}', \App\Livewire\CustomerPortal\JobStatus::class)->name('status');
});

require __DIR__.'/auth.php';
