<?php

use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\Auth\ForgotPasswordController;

use App\Http\Controllers\Backend\ProductController;

use App\Http\Controllers\Frontend\HomeController;
use App\Http\Controllers\Frontend\CartController;
use App\Http\Controllers\Frontend\OrderController;
use App\Http\Controllers\Frontend\MessageController;
use App\Http\Controllers\Frontend\UserAccountController;
use App\Http\Controllers\Frontend\FaqController;
use App\Http\Controllers\Frontend\FooterMenuController;
use App\Http\Controllers\Frontend\PageController;

use App\Http\Controllers\Feeds\FacebookCatalogFeedController;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Artisan;

use App\Http\Controllers\SitemapController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/

// =======================================================
// Maintenance routes – শুধু local env এ allow
// =======================================================
if (app()->environment('local')) {

    // Cache clear (only for logged-in user in local)
    Route::middleware(['auth'])->get('/clear-cache', function () {
        Artisan::call('optimize:clear');
        Artisan::call('config:clear');
        Artisan::call('cache:clear');
        Artisan::call('view:clear');
        Artisan::call('route:clear');

        return 'Cache cleared successfully!';
    });

    // Storage link
    Route::middleware(['auth'])->get('/run-storage-link', function () {
        Artisan::call('storage:link');
        return 'Storage link created successfully!';
    });
}


// =======================================================
// Auth
// =======================================================
Route::get('register', [RegisterController::class, 'create'])->name('register');
Route::post('register/store', [RegisterController::class, 'store'])->name('register.store');

Route::get('login', [LoginController::class, 'create'])->name('login');
Route::post('login/store', [LoginController::class, 'store'])->name('login.store');

Route::get('logout', [LoginController::class, 'logout'])->name('logout');

// =======================================================
// Sitemap Route (later add)
// =======================================================

Route::get('sitemap.xml', [SitemapController::class, 'index'])->name('sitemap.xml');


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


// =======================================================
// Facebook Commerce Catalog Feed (CSV)
// =======================================================
Route::get('/feeds/facebook/products.csv', [FacebookCatalogFeedController::class, 'productsCsv'])
    ->name('feeds.facebook.products');


// =======================================================
// Product Details
// =======================================================
Route::get('product/{product:slug}', [HomeController::class, 'details'])->name('details');
Route::get('trending-product/{product:slug}', [HomeController::class, 'trending'])->name('trending');


// =======================================================
// Cart & Checkout
// =======================================================
Route::get('cart', [HomeController::class, 'cart'])->name('cart');
Route::get('cart/{id}', [CartController::class, 'index']); // cart product fetch (AJAX/Direct)

Route::get('checkout', [OrderController::class, 'checkout'])->name('checkout');


// =======================================================
// Order
// =======================================================
Route::get('order-create', [OrderController::class, 'create'])->name('order.create');
Route::post('order', [OrderController::class, 'store'])->name('order.store');
Route::get('confirm-order/{id}', [OrderController::class, 'confirmOrder'])->name('confirm-order');


// =======================================================
// Search
// =======================================================
Route::get('search', [ProductController::class, 'searchr'])->name('search');

// old searchbox (jodi kono JS autocomplete use kore)
Route::get('searchbox', [ProductController::class, 'searchr']);


// =======================================================
// User Account
// =======================================================
Route::get('user-account', [UserAccountController::class, 'webUser'])->name('web-account');
Route::get('edit-account', [UserAccountController::class, 'editWebUser'])->name('edit-web-user');
Route::post('edit-account/{id}', [UserAccountController::class, 'updateWebUser'])->name('update-web-user');

Route::get('order-history', [UserAccountController::class, 'userOrderHistory'])->name('user-order-history');

Route::get('user-change-password', [UserAccountController::class, 'userChangePassword'])->name('user-change-password');
Route::put('user-update-password', [UserAccountController::class, 'userUpdatePassword'])->name('user-update-password');


// =======================================================
// Customer Message
// =======================================================
Route::post('message', [MessageController::class, 'store'])->name('message.store');


// =======================================================
// FAQ
// =======================================================
Route::get('faq', [FaqController::class, 'frequentlyAskQ'])->name('faq');


// =======================================================
// Forgot Password
// =======================================================
Route::get('forgot-password', [ForgotPasswordController::class, 'showForgotPasswordForm'])->name('password.request');
Route::post('forgot-password', [ForgotPasswordController::class, 'sendResetLink'])->name('password.email');
Route::get('reset-password/{token}', [ForgotPasswordController::class, 'showResetForm'])->name('reset.password');
Route::post('update-password', [ForgotPasswordController::class, 'resetPassword'])->name('update.password');


// =======================================================
// Category Pages
// =======================================================
Route::get('category/{slug}', [PageController::class, 'showProductsByMainMenu'])->name('main-menu-products');
Route::get('category/{name}/{slug}', [PageController::class, 'showProductsByMenu'])->name('menu.products');


// =======================================================
// Footer / Static Pages
// =======================================================
Route::get('privacy', [FooterMenuController::class, 'privacyPolicy'])->name('privacy-policy');
Route::get('about-us', [FooterMenuController::class, 'aboutUs'])->name('about-us');
Route::get('contact-us', [FooterMenuController::class, 'contactUs'])->name('contact-us');

Route::get('terms-and-condition', [FooterMenuController::class, 'termsAndCindition'])->name('terms-and-condition');
Route::get('delivary-policy', [FooterMenuController::class, 'deliveryPolicy'])->name('delivary-policy');
Route::get('payment-policy', [FooterMenuController::class, 'paymentPolicy'])->name('payment-policy');
Route::get('exchange-return', [FooterMenuController::class, 'exchangeReturn'])->name('exchange-return');

Route::get('size', [FooterMenuController::class, 'size'])->name('size');
Route::get('loyality-program', [FooterMenuController::class, 'loyalityProgram'])->name('loyality-program');
Route::get('store-location', [FooterMenuController::class, 'storeLocation'])->name('store-location');



