<?php

use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\RideController;
use App\Http\Controllers\CaptainController;
use App\Http\Controllers\SellerController;  
use App\Http\Controllers\Seller\ItemController;

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

// Authenticated Routes (for all users)
Route::middleware(['auth', 'verified'])->group(function () {
    // Dashboard Routes
    // This single route now handles all role-based dashboards
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // Profile Routes (for all users)
    Route::get('/profile', [ProfileController::class, 'show'])->name('profile.show');
    Route::post('/profile', [ProfileController::class, 'updateProfile'])->name('profile.updateProfile');
    Route::post('/captain/location/update', [CaptainController::class, 'updateLocation'])->name('captain.location.update');
    Route::get('/ride/searching/{rideRequest}', [RideController::class, 'showSearchingPage'])->name('ride.searching');
    Route::post('/ride/request', [RideController::class, 'requestRide'])->name('ride.request');
    Route::get('/rides/pending', [RideController::class, 'getPendingRides'])->name('rides.pending');
    Route::get('/ride/passenger/{rideRequest}', [RideController::class, 'showPassengerOngoingRide'])->name('ride.ongoing-passenger');
    Route::get('/ride/ongoing/{rideRequest}', [RideController::class, 'showOngoingRide'])->name('ride.ongoing');
    // Captain Actions
    Route::get('/ride/{rideRequest}/status', [RideController::class, 'getRideStatusAjax'])
    ->name('ride.status.ajax');
    Route::get('/ride/{rideRequest}/review', [RideController::class, 'showReviewForm'])
    ->name('ride.review');
    
Route::post('/ride/{rideRequest}/review', [RideController::class, 'submitReview'])
    ->name('ride.submit-review');
    Route::get('/ride/status/{rideRequest}', [RideController::class, 'getRideStatus'])->name('ride.status');
    Route::post('/ride/{rideRequest}/reached', [RideController::class, 'captainReached'])->name('ride.reached');
    Route::post('/ride/{rideRequest}/start', [RideController::class, 'startRide'])->name('ride.start');
    Route::post('/ride/{rideRequest}/complete', [RideController::class, 'completeRide'])->name('ride.complete');
    Route::post('/ride/{rideRequest}/accept', [RideController::class, 'acceptRide'])->name('ride.accept');
    Route::post('/ride/{rideRequest}/decline', [RideController::class, 'declineRide'])->name('ride.decline');
    // Default Breeze profile routes - you can remove or keep these
    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::middleware(['auth', 'verified', 'seller'])->prefix('seller')->name('seller.')->group(function () {
    
    Route::get('/register-shop', [SellerController::class, 'showShopRegistrationForm'])
        ->name('shop.register');
    Route::post('/register-shop', [SellerController::class, 'storeShop'])
        ->name('shop.store');
     Route::get('/edit-shop', [SellerController::class, 'editShop'])->name('shop.edit');
    Route::put('/edit-shop', [SellerController::class, 'updateShop'])->name('shop.update'); 

    Route::get('/categories', [ItemController::class, 'indexCategories'])->name('categories.index');
    Route::post('/categories', [ItemController::class, 'storeCategory'])->name('categories.store');
    Route::put('/categories/{category}', [ItemController::class, 'updateCategory'])->name('categories.update');
    Route::delete('/categories/{category}', [ItemController::class, 'destroyCategory'])->name('categories.destroy');

    // --- ITEM ROUTES (Full Resource) ---
    Route::get('/items', [ItemController::class, 'indexItems'])->name('items.index');
    Route::get('/items/create', [ItemController::class, 'createItem'])->name('items.create');
    Route::post('/items', [ItemController::class, 'storeItem'])->name('items.store');
    Route::get('/items/{item}/edit', [ItemController::class, 'editItem'])->name('items.edit');
    Route::put('/items/{item}', [ItemController::class, 'updateItem'])->name('items.update');
    Route::delete('/items/{item}', [ItemController::class, 'destroyItem'])->name('items.destroy');



    Route::get('/dashboard', [SellerController::class, 'dashboard'])->name('dashboard');
    
    // Add other seller-specific routes here (e.g., /seller/products)
});

Route::middleware(['auth', 'admin'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/dashboard', [AdminController::class, 'dashboard'])->name('dashboard');
    
    // User Management
    Route::get('/users', [AdminController::class, 'users'])->name('users');
    
    // Ride Management
    Route::get('/rides', [AdminController::class, 'rides'])->name('rides');
    Route::get('/rides/{ride}', [AdminController::class, 'showRide'])->name('rides.show');
    
    // Vehicle Types
    Route::get('/vehicle-types', [AdminController::class, 'vehicleTypes'])->name('vehicle-types');
    Route::post('/vehicle-types', [AdminController::class, 'storeVehicleType'])->name('store-vehicle-type');
    Route::delete('/vehicle-types/{id}', [AdminController::class, 'destroyVehicleType'])->name('destroy-vehicle-type');

    // Captain Management
    Route::get('/captains', [AdminController::class, 'captains'])->name('captains');
    Route::post('/captains/{user}/approve', [AdminController::class, 'approveCaptain'])->name('captains.approve');
    Route::post('/captains/{user}/reject', [AdminController::class, 'rejectCaptain'])->name('captains.reject');
});

require __DIR__.'/auth.php';