<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Models\HomeSection;
use App\Models\Menu;
use App\Models\Product;
use App\Models\Slider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;

class HomeController extends Controller
{
    public function home()
    {
        // আগের মতোই products, discounted_products, tranding রাখলাম
        $data['products'] = Product::paginate(12);

        $data['discounted_products'] = Product::where('discount_price', '>', 0)
            ->whereColumn('discount_price', '<', 'price')
            ->orderBy('discount_price', 'asc')
            ->get();

        $data['tranding'] = Product::latest()->get();

        // Slider গুলো
        $data['largeSliders'] = Slider::where('type', 'large')->get();
        $data['smallSliders'] = Slider::where('type', 'small')->get();

        // Just Now section
        $data['justNowProducts'] = Product::latest()->get();

        // Header + homepage section এর জন্য main menus
        $data['menus'] = Menu::whereNull('parent_id')
            ->with('submenus')
            ->orderBy('order')
            ->get();

        // HomeSection টেবিল থেকে section config – error safe ভাবে
        try {
            $query = HomeSection::with(['menu.submenus']);

            // Active status থাকলে শুধু active নেবো
            if (Schema::hasColumn('home_sections', 'is_active')) {
                $query->where('is_active', true);
            }

            // Order column অনুযায়ী সাজাবো (তোমার migration e name = order)
            if (Schema::hasColumn('home_sections', 'order')) {
                $query->orderBy('order');
            }

            $data['homeSections'] = $query->get();
        } catch (\Throwable $e) {
            // যদি কোনো কারণে error হয়, যেন পুরো সাইট 500 না দেয়
            Log::error('HomeSections load error: ' . $e->getMessage());
            $data['homeSections'] = collect();
        }

        return view('frontend.home')->with($data);
    }

    public function details(Product $product)
    {
        $product->load(['category', 'subcategory', 'galleries', 'tags', 'variants']);
        $tranding = Product::latest()->get();

        return view('frontend.details', ['product' => $product, 'tranding' => $tranding]);
    }

    public function trending(Product $product)
    {
        $product->load(['category', 'subcategory', 'galleries', 'tags', 'variants']);
        $tranding = Product::latest()->get();

        return view('frontend.details', ['product' => $product, 'tranding' => $tranding]);
    }

    public function cart()
    {
        return view('frontend.cart');
    }

    public function checkout()
    {
        return view('frontend.checkout');
    }

    public function discProduct()
    {
        $data['products'] = Product::paginate(10);
        $data['menus'] = Menu::whereNull('parent_id')
            ->with('submenus')
            ->orderBy('order')
            ->get();

        return view('frontend.home')->with($data);
    }
}
