<?php

use App\Http\Controllers\Auth\AddressController;
use App\Http\Controllers\Auth\LoginEmailController;
use App\Http\Controllers\Auth\LoginWaController;
use App\Http\Controllers\Auth\ProfileController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\CartController;
use App\Http\Controllers\CheckoutController;
use App\Http\Controllers\ComponentController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\LocaleController;
use App\Http\Controllers\LocationController;
use App\Http\Controllers\OrderController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\RoutingController;
use App\Http\Controllers\SearchController;
use App\Http\Controllers\TncController;
use App\Http\Controllers\HelpController;
use App\Http\Controllers\VoucherEventController;
use Illuminate\Support\Facades\Route;

Route::group(['prefix' => '/dummy'], function () {
    Route::get('', [RoutingController::class, 'index'])->name('root');
    Route::get('{first}/{second}/{third}', [RoutingController::class, 'thirdLevel'])->name('third');
    Route::get('{first}/{second}', [RoutingController::class, 'secondLevel'])->name('second');
    Route::get('{any}', [RoutingController::class, 'root'])->name('any');

});

// Location selection route
Route::post('/location/select', [LocationController::class, 'select'])->name('location.select');

// Language switching route
Route::post('/locale/switch', [LocaleController::class, 'switch'])->name('locale.switch');

Route::get('/', [HomeController::class, 'index'])->name('home');

Route::get('/products', [ProductController::class, 'index'])->name('product.index');
Route::get('/products/ajax', [ProductController::class, 'ajax'])->name('product.ajax');
Route::get('/products/ajax/populers', [ProductController::class, 'populers'])->name('product.ajax.populers');
Route::get('/products/ajax/populers-json', [ProductController::class, 'populersJson'])->name('product.ajax.populers-json');
Route::get('/products/ajax/highlights', [ProductController::class, 'highlights'])->name('product.ajax.highlights');
Route::get('/products/ajax/brands', [ProductController::class, 'brands'])->name('product.ajax.brands');
Route::get('/products/ajax/brands-with-images', [ProductController::class, 'brandsWithImages'])->name('product.ajax.brands-with-images');
Route::get('/products/ajax/categories', [ProductController::class, 'categories'])->name('product.ajax.categories');
Route::get('/products/ajax/genders', [ProductController::class, 'genders'])->name('product.ajax.genders');
Route::get('/products/ajax/announcements', [ProductController::class, 'announcements'])->name('product.ajax.announcements');
Route::get('/product/{slug}', [ProductController::class, 'detail'])->name('product.detail');

// Search routes
Route::get('/search', [SearchController::class, 'search'])->name('search.ajax');

// Component loading routes
Route::get('/components/{component}', [ComponentController::class, 'load'])->name('component.load');

Route::get('/register', [RegisterController::class, 'index'])->name('register');
Route::post('/register', [RegisterController::class, 'register'])->name('register');

Route::get('/login', [LoginWaController::class, 'index'])->name('login');
Route::group(['prefix' => '/login/phone'], function () {
    Route::get('/', [LoginWaController::class, 'index'])->name('login-phone');
    Route::post('/otp', [LoginWaController::class, 'otp'])->name('login-phone.otp');
    Route::get('/otp/{identity}', [LoginWaController::class, 'otpView'])->name('login-phone.otp.view');
    Route::post('/verify', [LoginWaController::class, 'verify'])->name('login-phone.verify');
});

Route::group(['prefix' => '/login/email'], function () {
    Route::get('/', [LoginEmailController::class, 'index'])->name('login-email');
    Route::post('/otp', [LoginEmailController::class, 'otp'])->name('login-email.otp');
    Route::get('/otp/{identity}', [LoginEmailController::class, 'otpView'])->name('login-email.otp.view');
    Route::post('/verify', [LoginEmailController::class, 'verify'])->name('login-email.verify');
});

// Google OAuth routes
Route::group(['prefix' => '/login/google'], function () {
    Route::get('/', [App\Http\Controllers\Auth\GoogleController::class, 'redirectToGoogle'])->name('login.google');
    Route::get('/callback', [App\Http\Controllers\Auth\GoogleController::class, 'handleGoogleCallback'])->name('login.google.callback');
});

Route::get('/profile', [ProfileController::class, 'index'])->name('profile');
Route::post('/profile', [ProfileController::class, 'update'])->name('profile.update');
Route::put('/profile/password', [ProfileController::class, 'updatePassword'])->name('profile.password.update');
Route::post('/profile/logout', [ProfileController::class, 'logout'])->name('profile.logout');

Route::middleware(['auth'])->prefix('/addresses')->group(function () {
    Route::get('/', [AddressController::class, 'index'])->name('addresses');
    Route::post('/', [AddressController::class, 'store'])->name('addresses.store');
    Route::put('/{id}', [AddressController::class, 'update'])->name('addresses.update');
    Route::delete('/{id}', [AddressController::class, 'destroy'])->name('addresses.destroy');
    Route::get('/provinces', [AddressController::class, 'provinces'])->name('addresses.provinces');
    Route::get('/cities/{provinceId}', [AddressController::class, 'cities'])->name('addresses.cities');
    Route::get('/districts/{provinceId}', [AddressController::class, 'districts'])->name('addresses.districts');
    Route::get('/villages/{districtId}', [AddressController::class, 'villages'])->name('addresses.villages');
});

Route::middleware(['auth'])->prefix('/cart')->group(function () {
    Route::get('/', [CartController::class, 'index'])->name('cart.index');
    Route::post('/', [CartController::class, 'add'])->name('cart.add');
    Route::put('/{id}', [CartController::class, 'update'])->name('cart.update');
    Route::delete('/clear', [CartController::class, 'clear'])->name('cart.clear');
    Route::delete('/{id}', [CartController::class, 'delete'])->name('cart.delete');
    Route::get('/count', [CartController::class, 'count'])->name('cart.count');
});

Route::middleware(['auth'])->prefix('/checkout')->group(function () {

    Route::get('/', [CheckoutController::class, 'index'])->name('checkout.delivery');
    Route::post('/', [CheckoutController::class, 'indexProcess'])->name('checkout.delivery.process');

    Route::get('/shipping', [CheckoutController::class, 'chooseShipping'])->name('checkout.shipping');
    Route::post('/shipping', [CheckoutController::class, 'chooseShippingProcess'])->name('checkout.shipping.process');
    Route::get('/payment', [CheckoutController::class, 'choosePayment'])->name('checkout.payment');

});

Route::middleware(['auth'])->prefix('/voucher-events')->group(function () {
    Route::post('/{voucherEvent}/redeem', [VoucherEventController::class, 'redeem'])->name('voucher-events.redeem');
});

Route::middleware(['auth'])->prefix('/orders')->group(function () {

    Route::get('/', [OrderController::class, 'index'])->name('orders');

});
Route::get('/terms-and-conditions', [TncController::class, 'index'])->name('terms-and-conditions');
Route::get('/help', [HelpController::class, 'index'])->name('help');
