<?php
namespace Mamad\Group_Product\Repository;

use Mamad\Brand\Models\Brand;
use Mamad\Category\Models\Category;
use Mamad\Group_Product\Models\GroupProduct;
use Mamad\PostMeta\Repository\PostMetaRepository;
use Mamad\Product\Models\Product;
use Mamad\Product\Repository\ProductVariationRepository;

class GroupProductRepository
{
    public static function find($id)
    {
        return GroupProduct::findOrFail($id);
    }

    public static function get()
    {
        return GroupProduct::query()->orderByDesc("created_at")->get();
    }


    public static function find_by_slug($slug)
    {
        return GroupProduct::query()->where("slug",$slug)->firstOrFail();
    }

    public static function pagination($num=12)
    {
        return GroupProduct::query()->paginate($num);
    }

    public static function limit($limit=10)
    {
        return GroupProduct::query()->limit($limit)->get();
    }
    public static function desc_order(){
       return GroupProduct::query()->orderByDesc("created_at")->get();
    }

    public static function order_product($order="asc", $name=null)
    {
        $order = $order == null ? "asc" : $order;

        switch ($order) {
            case "asc":
                $products = GroupProduct::with(['children_product.brand'])->orderBy("created_at");
                break;
            case "desc":
                $products = GroupProduct::with(['children_product.brand'])->orderByDesc("created_at");
                break;
            case "name":
                $products = GroupProduct::with(['children_product.brand'])->orderBy("title");
                break;
        }

        if ($name) {
            $products = $products->where("title", "LIKE", "%$name%");
        }

        return $products->paginate(10);
    }


    public static function create($data)
    {
        return GroupProduct::create([
            "title"=>$data->title,
            "content"=>$data->contents,
            "slug"=>$data->slug,
            "media_id"=>$data->thumbnail,
            "children_products"=>$data->children_product_select,
        ]);
    }

    public static function update($product,$data)
    {

        return $product->update([
            "title"=>$data->title,
            "content"=>$data->contents,
            "slug"=>$data->slug,
            "media_id"=>$data->thumbnail,
            "children_products"=>$data->children_product_select,
        ]);
    }

    public static function destroy($id)
    {
        GroupProduct::destroy($id);
    }

    public static function create_product_category($product,$category)
    {
        foreach ($category as $catId){

            $cat= Category::find($catId);
            if(is_object($cat)){
                $cat->group_products()->save($product);

            }

        }

    }
    public static function create_product_brand($product,$brands)
    {
        foreach ($brands as $brandId){

            $brand= Brand::find($brandId);
            if(is_object($brand)){
                $brand->group_products()->save($product);

            }

        }

    }

    public static function get_product_variation($product)
    {
        $variations = $product->variations()->whereNotNull("price")->orderBy("price")->orderBy("offer_price")->get()->toArray();

        foreach ($variations as $key => $variation) {
            foreach ($variation["variations"] as $keyItem => $value) {
                $variations[$key]["variations"][$keyItem] = [
                    "parentName" => ProductVariationRepository::find($keyItem)->only("name"),
                    ProductVariationRepository::find($value) ? ProductVariationRepository::find($value)->only("name", "id") : null
                ];
            }
        }

        return $variations;

    }

    public static function get_related_product_by_category($product){
        $catIds = $product->category->pluck('id')->toArray();

        $related_articles = GroupProduct::query()->whereHas('category', function ($query) use ($catIds) {
            return $query->whereIn('category.id', $catIds);
        })->where('id', '!=', $product->id)
            ->limit(10)
            ->get();

        return $related_articles;
    }
}

