<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\OrderController;
use App\Http\Controllers\PaymentsController;
use App\Http\Controllers\Api\MerchantEmployeeController;
use App\Http\Controllers\Api\PermissionsController;
use App\Http\Controllers\VoucherMasterController;
use App\Http\Controllers\RestaurantRegisterController;


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

Route::post('/handle-auth-error', [App\Http\Controllers\Auth\LoginController::class, 'handleAuthError'])->name('auth.handleError');
Route::get('lang/change', [App\Http\Controllers\LangController::class, 'change'])->name('changeLang');
Route::post('setToken', [App\Http\Controllers\Auth\AjaxController::class, 'setToken'])->name('setToken');
Route::post('mysql-login', [App\Http\Controllers\Auth\AjaxController::class, 'mysqlLogin'])->name('mysql.login');
Route::get('register', function () { return view('auth.register'); })->name('register');
Route::get('register/phone', function () { return view('auth.phone_register'); })->name('register.phone');


Auth::routes(['login' => false, 'register' => false]); // Exclude default login route
Route::get('login', [App\Http\Controllers\Auth\LoginController::class, 'showLoginForm'])->name('login')->middleware('web');

Route::middleware(['auth'])->group(function () {
Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index']);
Route::get('/dashboard', [App\Http\Controllers\HomeController::class, 'index'])->name('dashboard');
Route::get('/api/dashboard', [App\Http\Controllers\HomeController::class, 'apiDashboard'])->name('api.dashboard');


Route::get('/foods', [App\Http\Controllers\FoodController::class, 'index'])->name('foods');
Route::get('/foods/create', [App\Http\Controllers\FoodController::class, 'create'])->name('foods.create');
Route::get('/foods/edit/{id}', [App\Http\Controllers\FoodController::class, 'edit'])->name('foods.edit');

// Foods API (PostgreSQL-backed, replace Firebase JS SDK calls)
Route::post('/api/foods',                [App\Http\Controllers\FoodController::class, 'apiStore'])->name('api.foods.store');
Route::post('/api/foods/upload-photo',   [App\Http\Controllers\FoodController::class, 'apiUploadPhoto'])->name('api.foods.upload-photo');
Route::get('/api/foods/{id}',            [App\Http\Controllers\FoodController::class, 'apiGet'])->name('api.foods.get');
Route::patch('/api/foods/{id}',          [App\Http\Controllers\FoodController::class, 'apiUpdate'])->name('api.foods.update');
Route::patch('/api/foods/{id}/publish',  [App\Http\Controllers\FoodController::class, 'apiTogglePublish'])->name('api.foods.toggle-publish');
Route::delete('/api/foods/{id}',         [App\Http\Controllers\FoodController::class, 'apiDelete'])->name('api.foods.delete');
Route::post('/api/foods/order',          [App\Http\Controllers\FoodController::class, 'apiUpdateOrder'])->name('api.foods.order');
Route::post('/api/foods/{id}/category',  [App\Http\Controllers\FoodController::class, 'apiChangeCategory'])->name('api.foods.category');

Route::get('/category', [App\Http\Controllers\CategoryController::class, 'index'])->name('category');
Route::get('/category/create', [App\Http\Controllers\CategoryController::class, 'create'])->name('category.create');
Route::get('/category/edit/{id}', [App\Http\Controllers\CategoryController::class, 'edit'])->name('category.edit');

// Category API (PostgreSQL-backed, replace Firebase JS SDK calls)
Route::post('/api/categories',               [App\Http\Controllers\CategoryController::class, 'apiStore'])->name('api.categories.store');
Route::patch('/api/categories/{id}',         [App\Http\Controllers\CategoryController::class, 'apiUpdate'])->name('api.categories.update');
Route::patch('/api/categories/{id}/publish', [App\Http\Controllers\CategoryController::class, 'apiTogglePublish'])->name('api.categories.toggle-publish');
Route::delete('/api/categories/{id}',        [App\Http\Controllers\CategoryController::class, 'apiDelete'])->name('api.categories.delete');
Route::post('/api/categories/order',         [App\Http\Controllers\CategoryController::class, 'apiUpdateOrder'])->name('api.categories.order');

Route::get('/orders/edit/{id}', [App\Http\Controllers\OrderController::class, 'edit'])->name('orders.edit');
Route::get('/orders/status_screen', [App\Http\Controllers\OrderController::class, 'statusScreen'])->name('orders.status_screen');
Route::get('/orders', [App\Http\Controllers\OrderController::class, 'index'])->name('orders');
Route::get('/api/orders', [App\Http\Controllers\OrderController::class, 'apiOrders'])->name('orders.api');
Route::get('/api/orders/{id}', [App\Http\Controllers\OrderController::class, 'apiShow'])->name('orders.api.show');
Route::post('/api/orders/{id}/status', [App\Http\Controllers\OrderController::class, 'apiUpdateStatus'])->name('orders.api.updateStatus');
Route::get('/status-screen', [App\Http\Controllers\OrderController::class, 'statusScreen'])->name('statusScreen');
Route::post('orders/save-override', [App\Http\Controllers\OrderController::class, 'saveOrderOverride'])->name('orders.save-override');
Route::post('order-status-notification', [App\Http\Controllers\OrderController::class, 'sendNotification'])->name('order-status-notification');
Route::get('/live-orders', [App\Http\Controllers\OrderController::class, 'liveOrders'])->name('liveOrders');
Route::get('/api/live-orders', [App\Http\Controllers\OrderController::class, 'apiLiveOrders'])->name('orders.api.live');
Route::post('/api/live-orders/{id}/sub-status', [App\Http\Controllers\OrderController::class, 'apiLiveOrderUpdateSubStatus'])->name('orders.api.live.subStatus');
Route::post('/api/live-orders/{id}/finish', [App\Http\Controllers\OrderController::class, 'apiLiveOrderFinish'])->name('orders.api.live.finish');
Route::post('/orders/send-finished-email', [App\Http\Controllers\OrderController::class, 'sendFinishedOrderEmail'])->name('orders.sendFinishedEmail');



Route::get('/voucher_master', [App\Http\Controllers\VoucherMasterController::class, 'index'])->name('voucher_master');
Route::get('/voucher/create', [App\Http\Controllers\VoucherMasterController::class, 'create'])->name('voucher.create');
Route::post('/voucher/store', [App\Http\Controllers\VoucherMasterController::class, 'store'])->name('voucher.store');
Route::get('/voucher/edit/{id}', [App\Http\Controllers\VoucherMasterController::class, 'edit'])->name('voucher.edit');
Route::post('/voucher/update/{id}', [App\Http\Controllers\VoucherMasterController::class, 'update'])->name('voucher.update');

});

Route::get('/users/payment_details', [App\Http\Controllers\UserController::class, 'paymentDetails'])->name('user.payment_details');
Route::get('/users/manage_profile', [App\Http\Controllers\UserController::class, 'manageProfile'])->name('user.manage_profile');
Route::get('/users/restaurant', [App\Http\Controllers\UserController::class, 'restaurant'])->name('user.restaurant');
Route::get('/users/profile', [App\Http\Controllers\UserController::class, 'profile'])->name('user.profile');
Route::get('/users/manage_users', [App\Http\Controllers\UserController::class, 'manageUsers'])->name('user.manage_users');
Route::get('/api/vendor', [App\Http\Controllers\UserController::class, 'vendorShow']);
Route::get('/api/vendor/status', [App\Http\Controllers\UserController::class, 'vendorGetStatus'])->name('api.vendor.status');
Route::put('/api/vendor/status', [App\Http\Controllers\UserController::class, 'vendorSetStatus'])->name('api.vendor.setStatus');
Route::put('/api/vendor/details', [App\Http\Controllers\UserController::class, 'vendorSaveDetails']);
Route::put('/api/vendor/company', [App\Http\Controllers\UserController::class, 'vendorSaveCompany']);
Route::put('/api/vendor/filters', [App\Http\Controllers\UserController::class, 'vendorSaveFilters']);
Route::put('/api/vendor/working-hours',[App\Http\Controllers\UserController::class, 'vendorSaveWorkingHours']);
Route::post('/api/vendor/photo', [App\Http\Controllers\UserController::class, 'vendorUploadPhoto']);
Route::delete('/api/vendor/photo', [App\Http\Controllers\UserController::class, 'vendorRemovePhoto']);
Route::get('/api/profile', [App\Http\Controllers\UserController::class, 'profileShow']);
Route::put('/api/profile', [App\Http\Controllers\UserController::class, 'profileSave']);
Route::post('/api/profile/photo', [App\Http\Controllers\UserController::class, 'profileUploadPhoto']);
Route::post('/api/profile/change-password', [App\Http\Controllers\UserController::class, 'profileChangePassword']);
Route::post('/api/request-account-removal', [App\Http\Controllers\UserController::class, 'requestAccountRemoval'])->name('user.request_account_removal');


Route::get('/api/vendor-users', [App\Http\Controllers\Api\MerchantEmployeeController::class, 'index']);
Route::post('/api/vendor-users', [App\Http\Controllers\Api\MerchantEmployeeController::class, 'store']);
Route::put('/api/vendor-users/{merchantEmployee}', [App\Http\Controllers\Api\MerchantEmployeeController::class, 'update']);
Route::delete('/api/vendor-users/{merchantEmployee}', [App\Http\Controllers\Api\MerchantEmployeeController::class, 'destroy']);
Route::put('/api/vendor-users/{merchantEmployee}/permissions', [App\Http\Controllers\Api\MerchantEmployeeController::class, 'updatePermissions']);
Route::post('/api/vendor-users/{merchantEmployee}/sync-firebase', [App\Http\Controllers\Api\MerchantEmployeeController::class, 'syncFirebase']);
Route::get('/api/vendor-users/{merchantEmployee}/qr-code', [App\Http\Controllers\Api\MerchantEmployeeController::class, 'getQrCode']);
Route::get('/api/vendor-users/{merchantEmployee}/qr-card-pdf', [App\Http\Controllers\Api\MerchantEmployeeController::class, 'generateQrCardPdf']);
Route::put('/api/employee/profile', [App\Http\Controllers\Api\MerchantEmployeeController::class, 'updateProfile']);
Route::post('/api/employee/profile-picture', [App\Http\Controllers\Api\MerchantEmployeeController::class, 'uploadProfilePicture']);
Route::post('/api/employee/change-password', [App\Http\Controllers\Api\MerchantEmployeeController::class, 'changePassword']);
Route::post('/api/employee/clean-phone', [App\Http\Controllers\Api\MerchantEmployeeController::class, 'cleanPhoneData']);

//register routes
Route::get('/register/countries',    [App\Http\Controllers\RestaurantRegisterController::class, 'countries']   )->name('register.countries');
Route::get('/register/cuisines',     [App\Http\Controllers\RestaurantRegisterController::class, 'cuisines'])->name('register.cuisines');
Route::get('/register/settings',     [App\Http\Controllers\RestaurantRegisterController::class, 'settings']    )->name('register.settings');
Route::post('/register/upload-image',[App\Http\Controllers\RestaurantRegisterController::class, 'uploadImage'] )->name('register.upload-image');
Route::post('/register/delete-image',[App\Http\Controllers\RestaurantRegisterController::class, 'deleteImage'] )->name('register.delete-image');
Route::post('/register/restaurant',  [App\Http\Controllers\RestaurantRegisterController::class, 'register']    )->name('register.restaurant');
Route::post('/api/register/phone',   [App\Http\Controllers\RestaurantRegisterController::class, 'phoneStore']  )->name('register.phone.store');

// Settings API (no auth required — used by login/register pages)
Route::get('/api/settings/global',    [App\Http\Controllers\SettingsController::class, 'global']   )->name('api.settings.global');
Route::get('/api/settings/languages', [App\Http\Controllers\SettingsController::class, 'languages'])->name('api.settings.languages');

// Forgot password API (no auth required)
Route::post('/api/forgot-password', [App\Http\Controllers\Auth\LoginController::class, 'forgotPasswordApi'])->name('api.forgot-password');

// Transactions API (authenticated)
Route::get('/api/transactions', [App\Http\Controllers\UserController::class, 'apiTransactions'])->name('api.transactions');

// Order print API (authenticated)
Route::get('/api/orders/{id}/print', [App\Http\Controllers\OrderController::class, 'apiPrint'])->name('api.orders.print');

Route::get('/booktable/edit/{id}', [App\Http\Controllers\BookTableController::class, 'edit'])->name('booktable.edit');

Route::post('/api/support-ticket', [App\Http\Controllers\SupportTicketController::class, 'create'])
    ->middleware('throttle:10,60'); // Max 10 tickets per hour


// Stripe Connect Onboarding
Route::get('/stripe/onboarding/start', [App\Http\Controllers\StripeConnectController::class, 'showOnboardingModal'])->name('stripe.onboarding.start');
Route::post('/stripe/onboarding/start', [App\Http\Controllers\StripeConnectController::class, 'startOnboarding'])->name('stripe.onboarding.start.post');
Route::get('/stripe/onboarding/complete', [App\Http\Controllers\StripeConnectController::class, 'onboardingComplete'])->name('stripe.onboarding.complete');
Route::get('/stripe/onboarding/refresh', [App\Http\Controllers\StripeConnectController::class, 'onboardingRefresh'])->name('stripe.onboarding.refresh');
Route::get('/stripe/dashboard', [App\Http\Controllers\StripeConnectController::class, 'getDashboardLink'])->name('stripe.dashboard');
Route::get('/stripe/dashboard-url', [App\Http\Controllers\StripeConnectController::class, 'getDashboardUrl'])->name('stripe.dashboard.url');


Route::middleware('guest')->group(function () {
});