<?php

namespace Mamad\Group_Product\Models;

use Illuminate\Database\Eloquent\Model;
use Mamad\Brand\Models\Brand;
use Mamad\Category\Models\Category;
use Mamad\Comment\Models\Comment;
use Mamad\Media\Models\Media;
use Mamad\Media\Service\MediaService;
use Mamad\PostMeta\Models\Post_meta;
use Mamad\PostMeta\Repository\PostMetaRepository;
use Mamad\Product\Models\ProductAttribute;
use Mamad\Product\Models\ProductVariation;
use Mamad\Product\Repository\ProductVariationRepository;
use Mamad\Product\Service\ProductVariationService;

class GroupProduct extends Model
{
    protected $fillable=["title","slug","children_products","content","media_id"];
    protected $casts=["children_products"=>"json"];
    protected $table="group_products";

    public function category()
    {
        return $this->morphToMany(Category::class, 'categoryable');
    }
    public function children_product()
    {
        return $this->hasMany(GroupChildrenProduct::class, 'group_product_id',"id");
    }
    public function brand()
    {
        return $this->morphToMany(Brand::class, 'brandable');
    }
    public function post_meta(){
        return $this->morphMany(Post_meta::class, 'post_metaable');
    }
    public function getPostMetaArrayAttribute(){
        return $this->post_meta->pluck("meta_value","meta_key");
    }
    public function media()
    {
        return $this->belongsTo(Media::class);
    }

    public function post_meta_where($postMeta)
    {
        if($meta=$this->post_meta()->where("meta_key",$postMeta)->first()){
            return $meta;
        }else{
            return (object) ["meta_value"=>null];
        }
    }

    public function getUrlAttribute()
    {
        return route("group_product.index",["slug"=>$this->slug]);
    }


}
