<?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\Group_Product\Repository\GroupProductRepository;
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\ProductRepository;
use Mamad\Product\Repository\ProductVariationRepository;
use Mamad\Product\Service\ProductVariationService;

class GroupChildrenProduct extends Model
{
    protected $fillable=["title","media_id","group_product_id"];
    public  $type_variable="variable";
    protected $table="products";

    public function post_meta(){
        return $this->morphMany(Post_meta::class, 'post_metaable');
    }
    public function group_product()
    {
        return $this->belongsTo(GroupProduct::class, 'group_product_id','id');
    }



    public function attributes()
    {
        return $this->hasMany(ProductAttribute::class,"product_id");
    }
    public function brand()
    {
        return $this->morphToMany(Brand::class, 'brandable');
    }
    public function media()
    {
        return $this->belongsTo(Media::class);
    }

    public function variations()
    {
        return $this->hasMany(ProductVariation::class,"product_id");
    }

    public function set_price($price,$offer_price){
        PostMetaRepository::update($this,"price",$price);
        PostMetaRepository::update($this,"offer_price",$offer_price);

    }
    public function set_parent_group_product($group_product){
        if(is_numeric($group_product)) {
            $group_product=GroupProductRepository::find($group_product);
        }
        $this->parent_group_product=$group_product;
    }

    public function getRegularPriceAttribute(){
        if($this->type == $this->type_variable){
            $biggest_variation=ProductVariationRepository::get_biggest_price_variation($this);
            if(is_object($biggest_variation)){
            return $biggest_variation->price;

            }
            return 0;
        }
    return PostMetaRepository::get_post_meta_value($this,"price");

}

     public function getOfferPriceAttribute(){
        $parentProduct=$this->parent_group_product;
    $offer_price=false;
    if($this->type == $this->type_variable){
        $biggest_variation=ProductVariationRepository::get_biggest_price_variation($this);

        if(is_object($biggest_variation)){
            $offer_price= $biggest_variation->offer_price;

        } else{
            return 0;

        }

    } else{
        $offer_price=PostMetaRepository::get_post_meta_value($this,"offer_price");
        return $offer_price;
    }

    $price=$this->regular_price;
    if($offer_price){
        $price=$offer_price;
    }

         if($price<=0) return null;
    if(is_object($parentProduct)) {
        if ($offer_category = ProductRepository::get_product_offered_category($parentProduct)) {
            $category_offer_percent = $offer_category->offer;
            $offer_price = (int)ceil(($category_offer_percent / 100) * $price);
            return $price - $offer_price;

        }

        if ($offer_brand = ProductRepository::get_product_offered_brand($parentProduct)) {
            $brand_offer_percent = $offer_brand->offer;
            $offer_price = (int)ceil(($brand_offer_percent / 100) * $price);
            return $price - $offer_price;

        }
    }
    return $price;

}


    public function getMinusOfferPriceAttribute(){
        $parentProduct=$this->parent_group_product;
        $offer_price=false;
        if($this->type == $this->type_variable){
            $biggest_variation=ProductVariationRepository::get_biggest_price_variation($this);

            if(is_object($biggest_variation)){
                $offer_price= $biggest_variation->offer_price;

            } else{
                return 0;

            }

        } else{
            $offer_price=PostMetaRepository::get_post_meta_value($this,"offer_price");
        }

        $price=$this->regular_price;
        if($offer_price){
            $price=$offer_price;
        }

        if($price<=0) return 0;
        if(is_object($parentProduct)) {

            if ($offer_category = ProductRepository::get_product_offered_category($parentProduct)) {

                $category_offer_percent = $offer_category->offer;
                $offer_price = (int)ceil(($category_offer_percent / 100) * $price);
                return  $offer_price;

            }

            if ($offer_brand = ProductRepository::get_product_offered_brand($parentProduct)) {
                $brand_offer_percent = $offer_brand->offer;
                $offer_price = (int)ceil(($brand_offer_percent / 100) * $price);
                return  $offer_price;

            }
        }
        return 0;

    }


    public function getSmallImage()
    {
        $file=$this->media->files["original"];
        if(isset($this->media->files["100"])) $file=$this->media->files["100"];

       return store_image_link().'/'.$file;
    }

    //stock

    public function getManageStockAttribute()
    {

        if($this->type == $this->type_variable){
            $biggest_variation=ProductVariationRepository::get_one_in_stock_variation($this);
            if(is_object($biggest_variation)){
                return $biggest_variation->manage_stock == "on";

            } return false;

        }
        return PostMetaRepository::get_post_meta_value($this,"manage_stock") == "on";
    }

    public function getStockNumberAttribute()
    {

        if($this->type == $this->type_variable){
            $biggest_variation=ProductVariationRepository::get_one_in_stock_variation($this);
            if(is_object($biggest_variation)){
                return $biggest_variation->stock_number ;

            } return null;

        }
        return PostMetaRepository::get_post_meta_value($this,"stock_number");
    }


    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 stock_number()
    {
        if ($this->manage_stock) {

           if($meta=$this->stock_number > 0){
               return $meta . " عدد  ";
        } else {
            return "در انبار موجود نیست";
        }
    } else{
            return  "مدیریت انبار خاموش است";
        }

    }

    public function product_price()
    {
        $price=$this->regular_price;
        if ($price >  0) {
            $offer_price=$this->offer_price;
            if(!$offer_price || $offer_price==$price){
                return format_price_with_currencySymbol($price);
            } else {
                return "<del>".format_price_with_currencySymbol($price)."</del>"."<ins class='d-block mb-2 ' style='text-decoration: none'>".format_price_with_currencySymbol($offer_price)."</ins>";
            }
        } else{
            return  "رایگان";
        }

    }

    public function product_price_with_quantity($quantity,$variation=null){
        $minus_price=$this->minus_offer_price;

        $price=$this->regular_price;
        if($this && $this->type == $this->type_variable){
            $variation=ProductVariationRepository::find_variation($variation);
            $price=$variation->price;

        }

        if ($price >  0) {
            $offer_price=$this->offer_price;
            if($variation && $this->type == $this->type_variable){
                $offer_price=$variation->offer_price;

            }
            if($variation){
                if(!$offer_price){
                    $price =$price * $quantity;
                    if($minus_price){
                        $price =$price - ($minus_price * $quantity);
                    }
                    $offer_1_min=$variation->offer_1_min;
                    $offer_1_percent=$variation->offer_1_percent;
                    $offer_2_min=$variation->offer_2_min;
                    $offer_2_percent=$variation->offer_2_percent;

                    if($price >=$offer_1_min && $price < $offer_2_min ){
                        $offer_price=($offer_1_percent / 100) * $price;
                        $price=$price - $offer_price;
                    }
                    if( $price > $offer_2_min ){
                        $offer_price=($offer_2_percent / 100) * $price;
                        $price=$price - $offer_price;
                    }

                    return format_price_with_currencySymbol($price);
                }
                else {
                    $offer_price=$offer_price * $quantity;
                    if($minus_price){
                        $offer_price =$offer_price - ($minus_price * $quantity);
                    }
                    $offer_1_min=$variation->offer_1_min;
                    $offer_1_percent=$variation->offer_1_percent;
                    $offer_2_min=$variation->offer_2_min;
                    $offer_2_percent=$variation->offer_2_percent;

                    if($price >=$offer_1_min && $price < $offer_2_min ){
                        $offer_price=($offer_1_percent / 100) * $price;
                        $price=$price - $offer_price;
                    }
                    if( $price > $offer_2_min ){
                        $offer_price=($offer_2_percent / 100) * $price;
                        $price=$price - $offer_price;
                    }
                    return format_price_with_currencySymbol($offer_price);
                }
            } else{
                return format_price_with_currencySymbol($offer_price);
            }
        } else{
            return  "رایگان";
        }
    }

    protected static function booted()
    {
        parent::boot();
        static::created(function ($product) {
            $product_attr=ProductAttribute::where("product_id",request()->product_nick_name);
           $product_attr->update(["product_id"=>$product->id]);
        });
        static::creating(function ($query) {
            $query->product_type ="group_product";
        });
    }

    public function getOfferPercentAttribute()
    {
        $price = $this->regular_price;
        $offer_price = $this->offer_price;

        if ($offer_price && $price > 0 && $offer_price < $price) {
            $discount = (($price - $offer_price) / $price) * 100;
            return round($discount) ;
        }

        return null;
    }


    public function get_variation_by_value($variation_id){
        if($variation_id){
            $varitaion_attribute_name=ProductVariationRepository::get_attribute_by_variation_id($variation_id);
            $varitaion_html=ProductVariationService::get_variations_name_html($varitaion_attribute_name);
            return $varitaion_html;
        }
        return null;
    }

    public function getTypeAttribute(){
        return PostMetaRepository::get_post_meta_value($this,"type");

    }

    public function getVariationOfferHtmlAttribute(){
        $first_variation=ProductVariationRepository::get_biggest_price_variation($this);
        $offer_1_min=$first_variation->offer_1_min;
        $offer_1_percent=$first_variation->offer_1_percent;
        $offer_2_min=$first_variation->offer_2_min;
        $offer_2_percent=$first_variation->offer_2_percent;

        $offer_html='';
        if($offer_1_min && $offer_1_percent){
            $offer_html='<span class="discount" data-toggle="tooltip" title="حد اقل سفارش : '.format_price_with_currencySymbol($offer_1_min).'" data-min-price="'.$offer_1_min.' "  data-percent="'.$offer_1_percent.'">'.$offer_1_percent.'٪</span>';
        }
        if($offer_2_min && $offer_2_percent){
            $offer_html.='<span class="discount" data-toggle="tooltip" title="حد اقل سفارش : '.format_price_with_currencySymbol($offer_2_min).'" data-min-price="'.$offer_2_min.' " data-percent="'.$offer_2_percent.'">'.$offer_2_percent.'٪</span>';
        }
        return $offer_html;


    }

    public function getAffiliatePercentAttribute(){
        if($meta=$this->post_meta()->where("meta_key","affiliate_percent")->first()){
            return $meta->meta_value;
        }
    }


    // داخل مدل Product یا هر مدلی که داری
    public function getLowStockAmountAttribute() {
        return PostMetaRepository::get_post_meta_value($this, "low_stock_amount");
    }

    public function getOffer1MinQtyAttribute() {
        return PostMetaRepository::get_post_meta_value($this, "offer_1_min_qty");
    }

    public function getOffer1PercentAttribute() {
        return PostMetaRepository::get_post_meta_value($this, "offer_1_percent");
    }

    public function getOffer2MinQtyAttribute() {
        return PostMetaRepository::get_post_meta_value($this, "offer_2_min_qty");
    }

    public function getOffer2PercentAttribute() {
        return PostMetaRepository::get_post_meta_value($this, "offer_2_percent");
    }

    public function getOffer3MinQtyAttribute() {
        return PostMetaRepository::get_post_meta_value($this, "offer_3_min_qty");
    }

    public function getOffer3PercentAttribute() {
        return PostMetaRepository::get_post_meta_value($this, "offer_3_percent");
    }


}
