<?php
namespace Mamad\Group_Product\Repository;

use Illuminate\Support\Str;
use Mamad\Category\Models\Category;
use Mamad\Group_Product\Models\GroupChildrenProduct;
use Mamad\PostMeta\Repository\PostMetaRepository;
use Mamad\Product\Models\Product;
use Mamad\Product\Models\ProductAttribute;
use Mamad\Product\Repository\ProductVariationRepository;
use Mamad\Product\Service\ProductService;
use Mamad\ProductAttr\Repository\ProductAttrRepository;

class GroupChildrenProductRepository
{
    public static function find($id)
    {
        return GroupChildrenProduct::findOrFail($id);
    }


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

    public static function limit($limit=10)
    {
        return GroupChildrenProduct::query()->limit($limit)->get();
    }
    public static function desc_order(){
       return GroupChildrenProduct::query()->where("product_type","group_product")->orderByDesc("created_at")->get();
    }

    public static function order_product($order="asc",$name=null)
    {
            $order=$order==null ? "asc" : $order;
        switch ($order){
            case "asc":
                $products = GroupChildrenProduct::query()->where("product_type","group_product")->orderBy("created_at");
                break;
            case "desc":
                $products = GroupChildrenProduct::query()->where("product_type","group_product")->orderByDesc("created_at");
                break;
            case "name":
                $products = GroupChildrenProduct::query()->where("product_type","group_product")->orderBy("title");
                break;
        }
        if($name){
            $products=$products->where("title","LIKE","%$name%");
        }
        $products=$products->paginate(10);
        return $products;
    }

    public static function create($data)
    {
        return GroupChildrenProduct::create([
            "title"=>$data->title,
            "group_product_id"=>$data->product_group,
            "media_id"=>$data->thumbnail,
        ]);
    }

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

        return $product->update([
            "title"=>$data->title,
            "group_product_id"=>$data->product_group,
            "media_id"=>$data->thumbnail,
        ]);
    }

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

    public static function set_price($product,$price,$offer_price)
    {
        $product->set_price($price,$offer_price);
    }


    public static function in_stock($product,$quantity=1,$variation=null){
        if(is_numeric($product)){
            $product=self::find($product);
        }
        if(!$variation){
            $product_manage_stock=$product->manage_stock;
            $product_stock_number=$product->stock_number;

        } elseif(is_object($variation)){
            if($product->id != $variation->product_id){
                return false;
            }
            $product_manage_stock=$variation->manage_stock;
            $product_stock_number=$variation->stock_number;
        } elseif (is_numeric($variation)){
            $variation=ProductVariationRepository::find_variation($variation);
            $product_manage_stock=$variation->manage_stock;
            $product_stock_number=$variation->stock_number;
        }
        else{
            return false;
        }

        if($product_manage_stock && $product_stock_number>=$quantity){
            return true;
        } elseif (!$product_manage_stock){
            return true;
        } else{
            return false;
        }

    }



    public static function decrease_stock_number($product,$quantity,$variation=null){

        if(is_numeric($product)){
            $product=self::find($product);
        }
        $product_manage_stock=$product->manage_stock;
        if($product_manage_stock){
            $product_stock_number=$product->stock_number;
            if(!$variation){
                PostMetaRepository::update($product, "stock_number", $product_stock_number - $quantity);
            } else{
                ProductVariationRepository::update_stock_number($variation,$product_stock_number - $quantity);
            }

        }

    }

}

