<?php

use App\Http\Controllers\Auth\ForgotPasswordController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\MobileVerificationController;
use App\Http\Controllers\Auth\OtpLoginController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\Auth\ResetPasswordController;
use App\Http\Controllers\BiataBotController;
use App\Http\Controllers\BlogController;
use App\Http\Controllers\CartController;
use App\Http\Controllers\CheckoutController;
use App\Http\Controllers\AmazingController;
use App\Http\Controllers\ShopController;
use App\Http\Controllers\SitemapController;
use App\Http\Controllers\SiteController;
use App\Models\Product;
use Illuminate\Support\Facades\Route;

Route::get('/', [SiteController::class, 'index'])->name('home');
Route::get('shop', [ShopController::class, 'index'])->name('shop');
Route::get('brand', [ShopController::class, 'brandsIndex'])->name('brands.index');
Route::get('brand/{brand}', [ShopController::class, 'brand'])->name('shop.brand');
Route::get('category', [ShopController::class, 'categoriesIndex'])->name('categories.index');
Route::get('category/{category}', [ShopController::class, 'category'])->name('shop.category');
Route::get('پیشنهاد-شگفت-انگیز', [AmazingController::class, 'index'])->name('amazing');
Route::get('sitemap.xml', [SitemapController::class, 'index'])->name('sitemap');
Route::get('sitemap-pages.xml', [SitemapController::class, 'pages'])->name('sitemap.pages');
Route::get('sitemap-products.xml', [SitemapController::class, 'products'])->name('sitemap.products');
Route::get('sitemap-categories.xml', [SitemapController::class, 'categories'])->name('sitemap.categories');
Route::get('sitemap-brands.xml', [SitemapController::class, 'brands'])->name('sitemap.brands');
Route::get('sitemap-blog.xml', [SitemapController::class, 'blog'])->name('sitemap.blog');
Route::get('robots.txt', [SitemapController::class, 'robots'])->name('robots');
Route::get('search/ajax', [SiteController::class, 'ajaxSearch'])->name('search.ajax');
Route::get('تماس-با-ما', [SiteController::class, 'contactUs'])->name('contact');
Route::get('درباره-ما', [SiteController::class, 'aboutUs'])->name('about');
Route::get('قوانین-و-مقررات', [SiteController::class, 'terms'])->name('terms');
Route::get('product/{slug}', [SiteController::class, 'product'])->name('product.show');
Route::post('product/{product}/review', [SiteController::class, 'submitReview'])->middleware('auth')->name('product.review');

// Public JSON API
Route::get('api/categories', [\App\Http\Controllers\Api\CategoryController::class, 'index'])->name('api.categories');
Route::get('api/products',   [\App\Http\Controllers\Api\ProductController::class, 'index'])->name('api.products');
Route::get('api/contact',    [\App\Http\Controllers\Api\ContactController::class, 'index'])->name('api.contact');
Route::get('api/installment',[\App\Http\Controllers\Api\InstallmentController::class, 'index'])->name('api.installment');

// Torob catalog API v3
Route::post('torob_api/v3/products', [\App\Http\Controllers\Api\TorobApiController::class, 'products'])
    ->middleware('torob.jwt')
    ->name('torob.api.v3.products');

// Biata Bot chat
Route::get('biata-bot/history',            [BiataBotController::class, 'history'])->name('biata-bot.history');
Route::post('biata-bot/chat',               [BiataBotController::class, 'chat'])->name('biata-bot.chat');

// Blog (public)
Route::get('blog',                          [BlogController::class, 'index'])->name('blog.index');
Route::get('blog/tags/{tag}',               [BlogController::class, 'tag'])->name('blog.tag');
Route::get('blog/{category}',               [BlogController::class, 'category'])->name('blog.category');
Route::get('blog/{category}/{post}',        [BlogController::class, 'show'])->name('blog.show');

// Cart
Route::get('cart', [CartController::class, 'index'])->name('cart.index');
Route::post('cart/add', [CartController::class, 'add'])->name('cart.add');
Route::post('cart/update/{key}', [CartController::class, 'update'])->name('cart.update');
Route::post('cart/remove/{key}', [CartController::class, 'remove'])->name('cart.remove');

// Checkout
Route::get('checkout', [CheckoutController::class, 'index'])->name('checkout.index');
Route::middleware('persian.digits')->group(function () {
    Route::post('checkout/login', [CheckoutController::class, 'ajaxLogin'])->name('checkout.login');
    Route::post('checkout/register', [CheckoutController::class, 'ajaxRegister'])->name('checkout.register');
    Route::post('checkout/otp-verify', [CheckoutController::class, 'ajaxOtpVerify'])->name('checkout.otp.verify');
    Route::post('checkout/address', [CheckoutController::class, 'ajaxAddAddress'])->name('checkout.address');
});
Route::post('checkout/otp-resend', [CheckoutController::class, 'ajaxOtpResend'])->name('checkout.otp.resend');
Route::post('checkout/shipping-cost', [CheckoutController::class, 'ajaxShippingCost'])->name('checkout.shipping-cost');
Route::post('checkout/apply-coupon', [CheckoutController::class, 'applyCoupon'])->name('checkout.apply-coupon');
Route::post('checkout/pay', [CheckoutController::class, 'placeOrder'])->name('checkout.pay');
Route::get('checkout/callback/zibal', [CheckoutController::class, 'zibalCallback'])->name('checkout.callback.zibal');
Route::match(['get', 'post'], 'checkout/callback/torobpay', [CheckoutController::class, 'torobpayCallback'])->name('checkout.callback.torobpay');
Route::match(['get', 'post'], 'checkout/callback/digipay', [CheckoutController::class, 'digipayCallback'])->name('checkout.callback.digipay');
Route::get('checkout/success/{order}', [CheckoutController::class, 'paymentSuccess'])->middleware('auth')->name('checkout.success');
Route::get('checkout/failed', [CheckoutController::class, 'paymentFailed'])->name('checkout.failed');

// Login
Route::get('login', [LoginController::class, 'showLoginForm'])->name('login');
Route::post('login', [LoginController::class, 'login'])->middleware('persian.digits');
Route::post('logout', [LoginController::class, 'logout'])->name('logout');

// Register
Route::get('register', [RegisterController::class, 'showRegistrationForm'])->name('register');
Route::post('register', [RegisterController::class, 'register'])->middleware('persian.digits');

// OTP Login
Route::get('otp-login', [OtpLoginController::class, 'showMobileForm'])->name('otp.login');
Route::middleware('persian.digits')->group(function () {
    Route::post('otp-login', [OtpLoginController::class, 'sendCode'])->name('otp.send');
    Route::post('otp-verify', [OtpLoginController::class, 'verifyCode'])->name('otp.verify');
});
Route::get('otp-verify', [OtpLoginController::class, 'showCodeForm'])->name('otp.verify.form');
Route::post('otp-resend', [OtpLoginController::class, 'resend'])->name('otp.resend');

// Mobile Verification
Route::get('verification', [MobileVerificationController::class, 'showVerifyForm'])->name('mobile.verify');
Route::post('verification', [MobileVerificationController::class, 'verify'])->middleware('persian.digits')->name('mobile.verify.submit');
Route::post('verification/resend', [MobileVerificationController::class, 'resend'])->name('mobile.resend');

// Password Reset
Route::get('password/reset', [ForgotPasswordController::class, 'showLinkRequestForm'])->name('password.request');
Route::post('password/email', [ForgotPasswordController::class, 'sendResetLinkEmail'])->name('password.email');
Route::get('password/reset/{token}', [ResetPasswordController::class, 'showResetForm'])->name('password.reset');
Route::post('password/reset', [ResetPasswordController::class, 'reset'])->name('password.update');
