<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Auth;

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

Route::post('/partner-register', [App\Http\Controllers\PartnerRegistrationController::class, 'store'])->name('partner.register');

// صفحة الدعوة - للمستخدمين الذين يفتحون رابط QR أو مشاركة الدعوة
Route::get('/invite', function () {
    $code = request()->query('code', '');
    return view('invite', ['referralCode' => $code]);
})->name('invite');

// صفحة سياسة الخصوصية (للتطبيق و Google Play)
Route::get('/privacy-policy', function () {
    return response()->file(public_path('privacy-policy.html'));
})->name('privacy-policy');
Route::get('/privacy-policy.html', function () {
    return response()->file(public_path('privacy-policy.html'));
});

// مسار اختبار بسيط
Route::get('/test', function () {
    return response()->json([
        'message' => 'التطبيق يعمل بشكل صحيح',
        'auth_check' => Auth::check(),
        'user' => Auth::user() ? [
            'id' => Auth::user()->id,
            'name' => Auth::user()->name,
            'email' => Auth::user()->email,
            'user_type' => Auth::user()->user_type,
            'status' => Auth::user()->status
        ] : null
    ]);
})->name('test');

// مسارات لوحة التحكم للمدير
Route::prefix('admin')->middleware(['auth', 'admin'])->group(function () {

    // لوحة التحكم الرئيسية
    Route::get('/', [AdminController::class, 'dashboard'])->name('admin.dashboard');

    // نشر storage/app/public → public/storage (رابط أو نسخ)
    Route::post('/storage/sync-to-public', [App\Http\Controllers\Admin\StorageSyncController::class, 'syncToPublic'])
        ->name('admin.storage.sync-to-public');

    // إدارة المستخدمين
    Route::get('/users', [AdminController::class, 'users'])->name('admin.users');
    Route::get('/users/create', [AdminController::class, 'createUser'])->name('admin.users.create');
    Route::post('/users', [AdminController::class, 'storeUser'])->name('admin.users.store');
    Route::get('/users/{id}', [AdminController::class, 'showUser'])->name('admin.users.show');
    Route::get('/users/{id}/edit', [AdminController::class, 'editUser'])->name('admin.users.edit');
    Route::put('/users/{id}', [AdminController::class, 'updateUser'])->name('admin.users.update');
    Route::delete('/users/{id}', [AdminController::class, 'deleteUser'])->name('admin.users.delete');
    Route::patch('/users/{id}/status', [AdminController::class, 'updateUserStatus'])->name('admin.users.status');

    // صفحة اختبار النماذج
    Route::get('/test-form', function() {
        return view('admin.test-form');
    })->name('admin.test-form');

    // إدارة النقاط والدعوات
    Route::prefix('points')->group(function () {
        Route::get('/', [App\Http\Controllers\Admin\PointsManagementController::class, 'dashboard'])->name('admin.points.dashboard');
        Route::get('/management', [App\Http\Controllers\Admin\PointsManagementController::class, 'pointsManagement'])->name('admin.points.management');
        Route::post('/adjust', [App\Http\Controllers\Admin\PointsManagementController::class, 'adjustPoints'])->name('admin.points.adjust');
        Route::get('/rewards', [App\Http\Controllers\Admin\PointsManagementController::class, 'rewardsManagement'])->name('admin.points.rewards');
        Route::post('/rewards/save', [App\Http\Controllers\Admin\PointsManagementController::class, 'saveReward'])->name('admin.points.rewards.save');
        Route::get('/redemptions', [App\Http\Controllers\Admin\PointsManagementController::class, 'redemptionsManagement'])->name('admin.points.redemptions');
        Route::post('/redemptions/process', [App\Http\Controllers\Admin\PointsManagementController::class, 'processRedemption'])->name('admin.points.redemptions.process');
        Route::get('/referrals', [App\Http\Controllers\Admin\PointsManagementController::class, 'referralsManagement'])->name('admin.points.referrals');
        Route::get('/earnings', [App\Http\Controllers\Admin\PointsManagementController::class, 'earningsManagement'])->name('admin.points.earnings');
        Route::post('/earnings/{id}/approve', [App\Http\Controllers\Admin\PointsManagementController::class, 'approveEarning'])->name('admin.points.earnings.approve');
        Route::post('/earnings/{id}/pay', [App\Http\Controllers\Admin\PointsManagementController::class, 'payEarning'])->name('admin.points.earnings.pay');
        Route::post('/earnings/{id}/cancel', [App\Http\Controllers\Admin\PointsManagementController::class, 'cancelEarning'])->name('admin.points.earnings.cancel');
        Route::get('/earnings/{id}/details', [App\Http\Controllers\Admin\PointsManagementController::class, 'earningDetails'])->name('admin.points.earnings.details');
        Route::get('/transactions', [App\Http\Controllers\Admin\PointsManagementController::class, 'transactionsManagement'])->name('admin.points.transactions');
        Route::get('/settings', [App\Http\Controllers\Admin\PointsManagementController::class, 'settings'])->name('admin.points.settings');
        Route::post('/settings', [App\Http\Controllers\Admin\PointsManagementController::class, 'saveSettings'])->name('admin.points.settings.save');
        Route::get('/reports', [App\Http\Controllers\Admin\PointsManagementController::class, 'reports'])->name('admin.points.reports');
        Route::get('/user-report/{userId}', [App\Http\Controllers\Admin\PointsManagementController::class, 'userReport'])->name('admin.points.user-report');
    });

    // إدارة السائقين
    Route::get('/drivers', [AdminController::class, 'drivers'])->name('admin.drivers');
    Route::get('/drivers/{id}', [AdminController::class, 'showDriver'])->name('admin.drivers.show');
    Route::get('/drivers/{id}/edit', [AdminController::class, 'editDriver'])->name('admin.drivers.edit');
    Route::put('/drivers/{id}', [AdminController::class, 'updateDriver'])->name('admin.drivers.update');
    Route::patch('/drivers/{id}/toggle-availability', [AdminController::class, 'toggleDriverAvailability'])->name('admin.drivers.toggle-availability');
    Route::patch('/drivers/{id}/verification', [AdminController::class, 'toggleDriverVerification'])->name('admin.drivers.verification');
    Route::patch('/drivers/{id}/status', [AdminController::class, 'updateDriverStatus'])->name('admin.drivers.status');

    // إدارة الركاب
    Route::get('/passengers', [AdminController::class, 'passengers'])->name('admin.passengers');
    Route::patch('/passengers/{id}/toggle-verification', [AdminController::class, 'togglePassengerVerification'])->name('admin.passengers.toggle-verification');

    // إدارة المحافظات والمدن
    Route::prefix('governorates')->name('admin.governorates.')->group(function () {
        Route::get('/', [App\Http\Controllers\Admin\GovernorateController::class, 'index'])->name('index');
        Route::get('/create', [App\Http\Controllers\Admin\GovernorateController::class, 'create'])->name('create');
        Route::post('/', [App\Http\Controllers\Admin\GovernorateController::class, 'store'])->name('store');
        Route::get('/{governorate}', [App\Http\Controllers\Admin\GovernorateController::class, 'show'])->name('show');
        Route::get('/{governorate}/edit', [App\Http\Controllers\Admin\GovernorateController::class, 'edit'])->name('edit');
        Route::put('/{governorate}', [App\Http\Controllers\Admin\GovernorateController::class, 'update'])->name('update');
        Route::delete('/{governorate}', [App\Http\Controllers\Admin\GovernorateController::class, 'destroy'])->name('destroy');
        Route::patch('/{governorate}/toggle-status', [App\Http\Controllers\Admin\GovernorateController::class, 'toggleStatus'])->name('toggle-status');
    });
    
    Route::prefix('cities')->name('admin.cities.')->group(function () {
        Route::get('/', [App\Http\Controllers\Admin\CityController::class, 'index'])->name('index');
        Route::get('/create', [App\Http\Controllers\Admin\CityController::class, 'create'])->name('create');
        Route::post('/', [App\Http\Controllers\Admin\CityController::class, 'store'])->name('store');
        Route::get('/by-governorate', [App\Http\Controllers\Admin\CityController::class, 'getCitiesByGovernorate'])->name('by-governorate');
        Route::get('/{city}', [App\Http\Controllers\Admin\CityController::class, 'show'])->name('show');
        Route::get('/{city}/edit', [App\Http\Controllers\Admin\CityController::class, 'edit'])->name('edit');
        Route::put('/{city}', [App\Http\Controllers\Admin\CityController::class, 'update'])->name('update');
        Route::delete('/{city}', [App\Http\Controllers\Admin\CityController::class, 'destroy'])->name('destroy');
        Route::patch('/{city}/toggle-status', [App\Http\Controllers\Admin\CityController::class, 'toggleStatus'])->name('toggle-status');
    });
    
    // Route test للتأكد من عمل المدن
    Route::get('/test-cities', function() {
        $cities = \App\Models\City::with('governorate')->get();
        return response()->json($cities);
    });
    Route::patch('/passengers/{id}/toggle-active', [AdminController::class, 'togglePassengerActive'])->name('admin.passengers.toggle-active');

    // مسارات إدارة المطاعم
    Route::patch('/restaurants/{id}/toggle-active', [AdminController::class, 'toggleRestaurantActive'])->name('admin.restaurants.toggle-active');
    Route::patch('/restaurants/{id}/toggle-open', [AdminController::class, 'toggleRestaurantOpen'])->name('admin.restaurants.toggle-open');

    // إدارة الرحلات
    Route::get('/trips', [AdminController::class, 'trips'])->name('admin.trips');
    Route::get('/trips/{id}', [AdminController::class, 'showTrip'])->name('admin.trips.show');
    Route::post('/trips/{id}/status', [AdminController::class, 'updateTripStatus'])->name('admin.trips.status');
    Route::get('/trips/{id}/details', [AdminController::class, 'getTripDetails'])->name('admin.trips.details');
    Route::get('/trips/{id}/history', [AdminController::class, 'getTripHistory'])->name('admin.trips.history');

    // إدارة طلبات الرحلات
    Route::get('/trip-requests', [AdminController::class, 'tripRequests'])->name('admin.trip-requests');

    // إدارة المطاعم
    Route::get('/restaurants', [AdminController::class, 'restaurants'])->name('admin.restaurants');
    Route::get('/restaurants/create', [AdminController::class, 'createRestaurant'])->name('admin.restaurants.create');
    Route::post('/restaurants', [AdminController::class, 'storeRestaurant'])->name('admin.restaurants.store');
    Route::get('/restaurants/{id}', [AdminController::class, 'showRestaurant'])->name('admin.restaurants.show');
    Route::get('/restaurants/{id}/edit', [AdminController::class, 'editRestaurant'])->name('admin.restaurants.edit');
    Route::put('/restaurants/{id}', [AdminController::class, 'updateRestaurant'])->name('admin.restaurants.update');
    Route::delete('/restaurants/{id}', [AdminController::class, 'deleteRestaurant'])->name('admin.restaurants.delete');
    
    // إدارة طلبات المطاعم
    Route::prefix('restaurants/{restaurantId}/orders')->name('admin.restaurants.orders.')->group(function () {
        Route::get('/', [App\Http\Controllers\Admin\RestaurantOrderController::class, 'index'])->name('index');
        Route::get('/{orderId}', [App\Http\Controllers\Admin\RestaurantOrderController::class, 'show'])->name('show');
        Route::put('/{orderId}/status', [App\Http\Controllers\Admin\RestaurantOrderController::class, 'updateStatus'])->name('update-status');
    });

    // إدارة مكاتب الغاز
    Route::prefix('gas-offices')->name('admin.gas-offices.')->group(function () {
        Route::get('/', [App\Http\Controllers\Admin\GasOfficeController::class, 'index'])->name('index');
        Route::get('/create', [App\Http\Controllers\Admin\GasOfficeController::class, 'create'])->name('create');
        Route::post('/', [App\Http\Controllers\Admin\GasOfficeController::class, 'store'])->name('store');
        Route::get('/stats', [App\Http\Controllers\Admin\GasOfficeController::class, 'stats'])->name('stats');
        Route::get('/{gasOffice}', [App\Http\Controllers\Admin\GasOfficeController::class, 'show'])->name('show');
        Route::get('/{gasOffice}/edit', [App\Http\Controllers\Admin\GasOfficeController::class, 'edit'])->name('edit');
        Route::put('/{gasOffice}', [App\Http\Controllers\Admin\GasOfficeController::class, 'update'])->name('update');
        Route::delete('/{gasOffice}', [App\Http\Controllers\Admin\GasOfficeController::class, 'destroy'])->name('destroy');
        Route::patch('/{gasOffice}/toggle-status', [App\Http\Controllers\Admin\GasOfficeController::class, 'toggleStatus'])->name('toggle-status');
        Route::patch('/{gasOffice}/toggle-open', [App\Http\Controllers\Admin\GasOfficeController::class, 'toggleOpen'])->name('toggle-open');
        Route::get('/{gasOffice}/cylinders', [App\Http\Controllers\Admin\GasOfficeController::class, 'cylinders'])->name('cylinders');
        Route::get('/{gasOffice}/orders', [App\Http\Controllers\Admin\GasOfficeController::class, 'orders'])->name('orders');
        Route::patch('/cylinders/{gasCylinder}/update-stock', [App\Http\Controllers\Admin\GasOfficeController::class, 'updateStock'])->name('cylinders.update-stock');
        Route::patch('/orders/{gasOrder}/update-status', [App\Http\Controllers\Admin\GasOfficeController::class, 'updateOrderStatus'])->name('orders.update-status');
    });

    // إدارة الغاز (القديمة - للقناني)
    Route::get('/gas', [AdminController::class, 'gas'])->name('admin.gas');
    Route::get('/gas/create', [AdminController::class, 'createGas'])->name('admin.gas.create');
    Route::post('/gas', [AdminController::class, 'storeGas'])->name('admin.gas.store');
    Route::get('/gas/{id}', [AdminController::class, 'showGas'])->name('admin.gas.show');
    Route::get('/gas/{id}/edit', [AdminController::class, 'editGas'])->name('admin.gas.edit');
    Route::put('/gas/{id}', [AdminController::class, 'updateGas'])->name('admin.gas.update');
    Route::delete('/gas/{id}', [AdminController::class, 'deleteGas'])->name('admin.gas.delete');

    // إدارة المدفوعات
    Route::get('/payments', [AdminController::class, 'payments'])->name('admin.payments');
    Route::get('/payments/{id}', [AdminController::class, 'showPayment'])->name('admin.payments.show');
    Route::post('/payments/{id}/status', [AdminController::class, 'updatePaymentStatus'])->name('admin.payments.status');
    Route::get('/payments/{id}/details', [AdminController::class, 'getPaymentDetails'])->name('admin.payments.details');
    Route::post('/payments/{id}/refund', [AdminController::class, 'requestPaymentRefund'])->name('admin.payments.refund');

    // إدارة المركبات
    Route::get('/vehicles', [AdminController::class, 'vehicles'])->name('admin.vehicles');
    Route::get('/vehicles/create', [AdminController::class, 'createVehicle'])->name('admin.vehicles.create');
    Route::post('/vehicles', [AdminController::class, 'storeVehicle'])->name('admin.vehicles.store');
    Route::get('/vehicles/{id}', [AdminController::class, 'showVehicle'])->name('admin.vehicles.show');
    Route::post('/vehicles/{id}/status', [AdminController::class, 'updateVehicleStatus'])->name('admin.vehicles.status');
    Route::post('/vehicles/{id}/availability', [AdminController::class, 'toggleVehicleAvailability'])->name('admin.vehicles.availability');
    Route::get('/vehicles/{id}/details', [AdminController::class, 'getVehicleDetails'])->name('admin.vehicles.details');

    // التقارير
    Route::get('/reports', [AdminController::class, 'reports'])->name('admin.reports');

    // الإعدادات
    Route::get('/settings', [App\Http\Controllers\Admin\AppSettingController::class, 'index'])->name('admin.settings.index');
    Route::put('/settings', [App\Http\Controllers\Admin\AppSettingController::class, 'update'])->name('admin.settings.update');

    Route::prefix('passenger-extra-services')->name('admin.passenger-extra-services.')->group(function () {
        Route::get('/', [App\Http\Controllers\Admin\PassengerExtraServiceController::class, 'index'])->name('index');
        Route::get('/create', [App\Http\Controllers\Admin\PassengerExtraServiceController::class, 'create'])->name('create');
        Route::post('/', [App\Http\Controllers\Admin\PassengerExtraServiceController::class, 'store'])->name('store');
        Route::get('/{id}/edit', [App\Http\Controllers\Admin\PassengerExtraServiceController::class, 'edit'])->name('edit');
        Route::put('/{id}', [App\Http\Controllers\Admin\PassengerExtraServiceController::class, 'update'])->name('update');
        Route::delete('/{id}', [App\Http\Controllers\Admin\PassengerExtraServiceController::class, 'destroy'])->name('destroy');
    });

    Route::prefix('passenger-extra-service-requests')->name('admin.passenger-extra-service-requests.')->group(function () {
        Route::get('/', [App\Http\Controllers\Admin\PassengerExtraServiceRequestController::class, 'index'])->name('index');
        Route::post('/{id}/status', [App\Http\Controllers\Admin\PassengerExtraServiceRequestController::class, 'updateStatus'])->name('status');
    });

    // إدارة طلبات شحن المحفظة (المطعم / مكتب الغاز)
    Route::prefix('wallet-topup')->group(function () {
        Route::get('/', [App\Http\Controllers\Admin\PartnerWalletTopupController::class, 'index'])->name('admin.wallet-topup.index');
        Route::post('/{id}/approve', [App\Http\Controllers\Admin\PartnerWalletTopupController::class, 'approve'])->name('admin.wallet-topup.approve');
        Route::post('/{id}/reject', [App\Http\Controllers\Admin\PartnerWalletTopupController::class, 'reject'])->name('admin.wallet-topup.reject');
    });

    // إدارة الخرائط
    Route::prefix('maps')->group(function () {
        Route::get('/', [App\Http\Controllers\Admin\MapManagementController::class, 'index'])->name('admin.maps.index');
        Route::post('/working-area', [App\Http\Controllers\Admin\MapManagementController::class, 'storeWorkingArea'])->name('admin.maps.working-area.store');
        Route::post('/service-points', [App\Http\Controllers\Admin\MapManagementController::class, 'storeServicePoint'])->name('admin.maps.service-points.store');
        Route::put('/service-points/{id}', [App\Http\Controllers\Admin\MapManagementController::class, 'updateServicePoint'])->name('admin.maps.service-points.update');
        Route::delete('/service-points/{id}', [App\Http\Controllers\Admin\MapManagementController::class, 'deleteServicePoint'])->name('admin.maps.service-points.delete');
        Route::post('/map-settings', [App\Http\Controllers\Admin\MapManagementController::class, 'storeMapSettings'])->name('admin.maps.settings.store');
        Route::get('/map-data', [App\Http\Controllers\Admin\MapManagementController::class, 'getMapData'])->name('admin.maps.data');
    });

    // إدارة أجور التطبيق
    // إدارة العروض والإعلانات
    Route::prefix('offers-ads')->name('admin.offers-ads.')->group(function () {
        Route::get('/', [App\Http\Controllers\Admin\OffersAndAdsController::class, 'index'])->name('index');
        
        // العروض
        Route::get('/offers/create', [App\Http\Controllers\Admin\OffersAndAdsController::class, 'createOffer'])->name('offers.create');
        Route::post('/offers', [App\Http\Controllers\Admin\OffersAndAdsController::class, 'storeOffer'])->name('offers.store');
        Route::get('/offers/{id}/edit', [App\Http\Controllers\Admin\OffersAndAdsController::class, 'editOffer'])->name('offers.edit');
        Route::put('/offers/{id}', [App\Http\Controllers\Admin\OffersAndAdsController::class, 'updateOffer'])->name('offers.update');
        Route::delete('/offers/{id}', [App\Http\Controllers\Admin\OffersAndAdsController::class, 'destroyOffer'])->name('offers.destroy');
        
        // الإعلانات
        Route::get('/ads/create', [App\Http\Controllers\Admin\OffersAndAdsController::class, 'createAd'])->name('ads.create');
        Route::post('/ads', [App\Http\Controllers\Admin\OffersAndAdsController::class, 'storeAd'])->name('ads.store');
        Route::get('/ads/{id}/edit', [App\Http\Controllers\Admin\OffersAndAdsController::class, 'editAd'])->name('ads.edit');
        Route::put('/ads/{id}', [App\Http\Controllers\Admin\OffersAndAdsController::class, 'updateAd'])->name('ads.update');
        Route::delete('/ads/{id}', [App\Http\Controllers\Admin\OffersAndAdsController::class, 'destroyAd'])->name('ads.destroy');
    });

    Route::prefix('platform-fees')->name('admin.platform-fees.')->group(function () {
        Route::get('/', [App\Http\Controllers\Admin\PlatformFeesController::class, 'index'])->name('index');
        Route::get('/{type}/{id}', [App\Http\Controllers\Admin\PlatformFeesController::class, 'show'])->name('show');
    });
});

// مسارات المصادقة
Route::get('/login', [App\Http\Controllers\AuthController::class, 'showLoginForm'])->name('login');
Route::post('/login', [App\Http\Controllers\AuthController::class, 'login'])->name('login.post');
Route::post('/logout', [App\Http\Controllers\AuthController::class, 'logout'])->name('logout');

// مسار تغيير كلمة المرور (يحتاج تسجيل دخول)
Route::middleware(['auth', 'admin'])->group(function () {
    Route::get('/change-password', [App\Http\Controllers\AuthController::class, 'showChangePasswordForm'])->name('change.password');
    Route::post('/change-password', [App\Http\Controllers\AuthController::class, 'changePassword'])->name('change.password.post');
});

// مسار البروفايل (يحتاج تسجيل دخول)
Route::middleware(['auth'])->group(function () {
    Route::get('/profile', [App\Http\Controllers\ProfileController::class, 'show'])->name('profile');
    Route::get('/profile/edit', [App\Http\Controllers\ProfileController::class, 'edit'])->name('profile.edit');
    Route::put('/profile', [App\Http\Controllers\ProfileController::class, 'update'])->name('profile.update');
});
