<?php

namespace Yajny\Controller;

use App\Http\Controllers\Controller;
use Yajny\Models\Category;
use Yajny\Models\CategoryCoupon;

class CategoryCtrl extends Controller
{
    /**
     * @param array $category_ids
     * @return mixed
     */
    public static function getCategoriesById(array $category_ids)
    {
        if (count($category_ids) == 0) return collect([]);
        return Category::whereIn('id', $category_ids)->orderByRaw('FIELD(id,' . implode($category_ids) . ')')->get();
    }

    /**
     * @param string $category_slug
     * @return mixed
     */
    public static function getCategoryBySlug(string $category_slug)
    {
        $category_slug = ($category_slug) ?: 'all';
        return Category::where('slug', $category_slug)->first();
    }

    /**
     * @param false $useColSelect
     * @return mixed
     */
    public static function getAllCategory($useColSelect = false)
    {
        $query = Category::with([
            'children' => function ($q) use ($useColSelect) {
                if ($useColSelect)
                    $q->selectRaw(Category::colSelect());
            }
        ])
            ->where('parent_id', NULL)
            ->orderBy('name', 'asc');

        if ($useColSelect)
            $query->selectRaw(Category::colSelect());

        return $query->get();
    }

    /**
     * @param false $useColSelect
     * @return mixed
     */
    public static function getParentCategories($useColSelect = false)
    {
        $query = Category::where('parent_id', NULL)
            ->orderBy('name', 'asc');

        if ($useColSelect)
            $query->selectRaw(Category::colSelect());

        return $query->get();
    }
    /**
     * @param int $limit
     * @param false $useColSelect
     * @return mixed
     */
    public static function getRandomParentCategories($limit=6, $useColSelect = false)
    {
        $query = Category::where('parent_id', NULL)
            ->inRandomOrder()
            ->orderBy('id', 'ASC')
            ->limit($limit);

        return $query->get();
    }
    /**
     * @param $parent_id
     * @param false $useColSelect
     * @return mixed
     */
    public static function getChildCategories($parent_id, $useColSelect = false)
    {
        $query = Category::where('parent_id', $parent_id)
            ->orderBy('name', 'asc');

        if ($useColSelect)
            $query->selectRaw(Category::colSelect());

        return $query->get();
    }

    /**
     * @param false $useColSelect
     * @return mixed
     */
    public static function getTopParentCategories($useColSelect = false)
    {
        $query = Category::where('parent_id', NULL)
            ->where('is_recommended', true)
            ->orderBy('name', 'asc');

        if ($useColSelect)
            $query->selectRaw(Category::colSelect());

        return $query->get();
    }

    /**
     * @param array $category_ids
     * @param false $useColSelect
     * @return mixed
     */
    public static function getParentCategoriesExceptIds(array $category_ids, $useColSelect = false)
    {
        $query = Category::where('parent_id', NULL)
            ->orderBy('name', 'asc');

        if (count($category_ids) > 0)
            $query->whereNotIn('id', $category_ids);

        if ($useColSelect)
            $query->selectRaw(Category::colSelect());

        return $query->get();
    }

    /** ------------------- Coupon Category Methods ------------------- **/
    public static function getCouponCategory()
    {
        return CategoryCoupon::with('children')->where('parent_id', NULL)->orderBy('name', 'asc')->get();
    }

    /**
     * @param array $category_ids
     * @return mixed
     */
    public static function getCouponCategoriesById(array $category_ids)
    {
        return CategoryCoupon::whereIn('id', $category_ids)->get();
    }
    /**
     * @param array $category_ids
     * @return mixed
     */

    public static function getCouponCategoryBySlug(string $category_slug)
    {
        $category_slug = ($category_slug) ?: 'all';
        return CategoryCoupon::where('slug', $category_slug)->first();
    }
    /**
     * @param $categoryIdOrSlug
     * @return mixed
     */
    public static function getInfo($categoryIdOrSlug)
    {
        return Category::where('id', $categoryIdOrSlug)
            ->orWhere('slug', $categoryIdOrSlug)
            ->selectRaw(Category::colSelect())->first();
    }
}
