<?php

use App\Models\Message;
use App\Models\Category;
use App\Models\PostTag;
use App\Models\PostCategory;
use App\Models\Order;
use App\Models\Wishlist;
use App\Models\Shipping;
use App\Models\Cart;
use Illuminate\Support\Facades\Auth;

// use Auth;
class Helper
{
    public static function messageList()
    {
        return Message::whereNull('read_at')->orderBy('created_at', 'desc')->get();
    }
    public static function getAllCategory()
    {
        $category = new Category();
        $menu = $category->getAllParentWithChild();
        return $menu;
    }

public static function getHeaderCategory()
    {
        // Stationery categories
        $stationery = Category::where('is_parent', 1)
            ->where('status', 'active')
            ->where('category_type', 'stationery')
            ->with('child_cat')
            ->get();

        // Cleaning categories
        $cleaning = Category::where('is_parent', 1)
            ->where('status', 'active')
            ->where('category_type', 'cleaning product')
            ->with('child_cat')
            ->get();

        if ($stationery->count() == 0 && $cleaning->count() == 0) {
            return;
        }
?>

        <li class="mega-menu">
            <a href="javascript:void(0);">
                Categories <i class="ti-angle-down"></i>
            </a>

            <ul class="dropdown mega-dropdown border-0 shadow" style="width: 420px; ">
                <li>
                    <div class="row no-gutters">

                        <!-- ================= STATIONERY ================= -->
                        <div class="col-md-6 mega-col">
                            <h6 class="mega-title">Stationery</h6>

                            <?php foreach ($stationery as $parent) { ?>
                                <div class="mega-item">

                                    <!-- PARENT -->
                                    <a class="parent-link"
                                        href="<?php echo route('product-cat', $parent->slug); ?>">
                                        <?php echo $parent->title; ?>

                                        <?php if ($parent->child_cat->count() > 0) { ?>
                                            <i class="ti-angle-down dropdown-icon"></i>
                                        <?php } ?>
                                    </a>


                                    <!-- CHILD -->
                                    <?php if ($parent->child_cat->count() > 0) { ?>
                                        <ul class="mega-sub">
                                            <?php foreach ($parent->child_cat as $child) { ?>
                                                <li>
                                                    <a href="<?php echo route(
                                                                    'product-sub-cat',
                                                                    [$parent->slug, $child->slug]
                                                                ); ?>">
                                                        <?php echo $child->title; ?>
                                                    </a>
                                                </li>
                                            <?php } ?>
                                        </ul>
                                    <?php } ?>

                                </div>
                            <?php } ?>
                        </div>

                        <!-- ================= CLEANING ================= -->
                        <div class="col-md-6 mega-col bg-light">
                            <h6 class="mega-title">Cleaning Products</h6>

                            <?php foreach ($cleaning as $parent) { ?>
                                <div class="mega-item">

                                    <!-- PARENT -->
                                    <a class="parent-link"
                                        href="<?php echo route('product-cat', $parent->slug); ?>">
                                        <?php echo $parent->title; ?>
                                    </a>

                                    <!-- CHILD -->
                                    <?php if ($parent->child_cat->count() > 0) { ?>
                                        <ul class="mega-sub">
                                            <?php foreach ($parent->child_cat as $child) { ?>
                                                <li>
                                                    <a href="<?php echo route(
                                                                    'product-sub-cat',
                                                                    [$parent->slug, $child->slug]
                                                                ); ?>">
                                                        <?php echo $child->title; ?>
                                                    </a>
                                                </li>
                                            <?php } ?>
                                        </ul>
                                    <?php } ?>

                                </div>
                            <?php } ?>
                        </div>

                    </div>
                </li>
            </ul>
        </li>

<?php
    }







    /*public static function getHeaderCategory()
    {
        $category = new Category();

        // Fetch separately using category_type
        $stationery = Category::where('is_parent', 1)
            ->where('status', 'active')
            ->where('category_type', 'stationery')
            ->with('child_cat')
            ->get();

        $cleaning = Category::where('is_parent', 1)
            ->where('status', 'active')
            ->where('category_type', 'cleaning product')
            ->with('child_cat')
            ->get();

        if ($stationery->count() > 0 || $cleaning->count() > 0) {
?>
            <li class="mega-menu">
                <a href="javascript:void(0);">
                    Category <i class="ti-angle-down"></i>
                </a>

                <ul class="dropdown mega-dropdown border-0 shadow" style="width: 600px;">
                    <li>
                        <div class="row no-gutters">

                            <!--LEFT COLUMN : STATIONERY -->
                            <div class="col-md-6 mega-col">
                                <h6 class="mega-title">Stationery</h6>

                                <?php foreach ($stationery as $cat_info) { ?>

                                    <div class="mega-item">
                                        <a href="<?php echo route('product-cat', $cat_info->slug); ?>">
                                            <?php echo $cat_info->title; ?>
                                        </a>

                                        <?php if ($cat_info->child_cat->count() > 0) { ?>
                                            <ul>
                                                <?php foreach ($cat_info->child_cat as $sub_menu) { ?>
                                                    <li>
                                                        <a href="<?php echo route(
                                                                        'product-sub-cat',
                                                                        [$cat_info->slug, $sub_menu->slug]
                                                                    ); ?>">
                                                            <?php echo $sub_menu->title; ?>
                                                        </a>
                                                    </li>
                                                <?php } ?>
                                            </ul>
                                        <?php } ?>

                                    </div>

                                <?php } ?>
                            </div>

                            <!-- 🔹 RIGHT COLUMN : CLEANING -->
                            <div class="col-md-6 mega-col bg-light">
                                <h6 class="mega-title">Cleaning Products</h6>

                                <?php foreach ($cleaning as $cat_info) { ?>

                                    <div class="mega-item">
                                        <a href="<?php echo route('product-cat', $cat_info->slug); ?>">
                                            <?php echo $cat_info->title; ?>
                                        </a>

                                        <?php if ($cat_info->child_cat->count() > 0) { ?>
                                            <ul>
                                                <?php foreach ($cat_info->child_cat as $sub_menu) { ?>
                                                    <li>
                                                        <a href="<?php echo route(
                                                                        'product-sub-cat',
                                                                        [$cat_info->slug, $sub_menu->slug]
                                                                    ); ?>">
                                                            <?php echo $sub_menu->title; ?>
                                                        </a>
                                                    </li>
                                                <?php } ?>
                                            </ul>
                                        <?php } ?>

                                    </div>

                                <?php } ?>
                            </div>

                        </div>
                    </li>
                </ul>
            </li>
<?php
        }
    }*/










    public static function cartHasSubscription()
    {
        return \App\Models\Cart::where('user_id', auth()->id())
            ->where('order_id', null)
            ->where('item_type', 'subscription')
            ->exists();
    }

    public static function cartHasProduct()
{
    return Cart::where('user_id', auth()->id())
        ->whereNull('order_id')
        ->where(function ($q) {
            $q->where('item_type', 'product')
              ->orWhereNull('item_type');
        })
        ->exists();
}


    public static function productCategoryList($option = 'all')
    {
        if ($option == 'all') {
            return Category::orderBy('id', 'DESC')->get();
        }
        return Category::has('products')->orderBy('id', 'DESC')->get();
    }

    public static function subscriptionTotal()
{
    return Cart::where('user_id', auth()->id())
        ->where('item_type', 'subscription')
        ->sum('price');
}

    public static function hasSubscriptionInCart()
{
    return Cart::where('user_id', auth()->id())
        ->where('item_type', 'subscription')
        ->exists();
}
    public static function postTagList($option = 'all')
    {
        if ($option == 'all') {
            return PostTag::orderBy('id', 'desc')->get();
        }
        return PostTag::has('posts')->orderBy('id', 'desc')->get();
    }

    public static function postCategoryList($option = "all")
    {
        if ($option == 'all') {
            return PostCategory::orderBy('id', 'DESC')->get();
        }
        return PostCategory::has('posts')->orderBy('id', 'DESC')->get();
    }
    // Cart Count
    public static function cartCount($user_id = '')
    {

        if (Auth::check()) {
            if ($user_id == "") $user_id = auth()->user()->id;
            return Cart::where('user_id', $user_id)->where('order_id', null)->sum('quantity');
        } else {
            return 0;
        }
    }
    // relationship cart with product
    public function product()
    {
        return $this->hasOne('App\Models\Product', 'id', 'product_id');
    }

    public static function getAllProductFromCart($user_id = '')
{
    if (Auth::check()) {
        if ($user_id == "") $user_id = auth()->id();

        return Cart::with(['product', 'subscription'])
            ->where('user_id', $user_id)
            ->whereNull('order_id')
            ->get();
    }
    return collect();
}

    // Total amount cart
    public static function totalCartPrice($user_id = '')
    {
        if (Auth::check()) {
            if ($user_id == "") $user_id = auth()->user()->id;
            return Cart::where('user_id', $user_id)->where('order_id', null)->sum('amount');
        } else {
            return 0;
        }
    }
    // Wishlist Count
    public static function wishlistCount($user_id = '')
    {

        if (Auth::check()) {
            if ($user_id == "") $user_id = auth()->user()->id;
            return Wishlist::where('user_id', $user_id)->where('cart_id', null)->sum('quantity');
        } else {
            return 0;
        }
    }
    public static function getAllProductFromWishlist($user_id = '')
    {
        if (Auth::check()) {
            if ($user_id == "") $user_id = auth()->user()->id;
            return Wishlist::with('product')->where('user_id', $user_id)->where('cart_id', null)->get();
        } else {
            return 0;
        }
    }
    public static function totalWishlistPrice($user_id = '')
    {
        if (Auth::check()) {
            if ($user_id == "") $user_id = auth()->user()->id;
            return Wishlist::where('user_id', $user_id)->where('cart_id', null)->sum('amount');
        } else {
            return 0;
        }
    }

    // Total price with shipping and coupon
    public static function grandPrice($id, $user_id)
    {
        $order = Order::find($id);
        dd($id);
        if ($order) {
            $shipping_price = (float)$order->shipping->price;
            $order_price = self::orderPrice($id, $user_id);
            return number_format((float)($order_price + $shipping_price), 2, '.', '');
        } else {
            return 0;
        }
    }


    // Admin home
    public static function earningPerMonth()
    {
        $month_data = Order::where('status', 'delivered')->get();
        // return $month_data;
        $price = 0;
        foreach ($month_data as $data) {
            $price = $data->cart_info->sum('price');
        }
        return number_format((float)($price), 2, '.', '');
    }

    public static function shipping()
    {
        return Shipping::orderBy('id', 'DESC')->get();
    }
}



?>


