<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class ProductAttributeChoice extends Model
{
    public $timestamps = false;

    protected $fillable = [
        'image',
        'count',
        'choice_id',
        'product_attribute_id'
    ];
    public function choice()
    {
        return $this->belongsTo(Choice::class,'choice_id')->withTrashed();
    }
    public function attribute()
    {
        return $this->belongsTo(ProductAttribute::class,'product_attribute_id');
    }
    
    public function statusChoice()
    {
        $attr = ProductAttribute::where('product_attribute_choice_id',$this->id)->first();
        if(!$attr)
        {
            return 'one';
        }
        return 'multi';
    }
    public function hasChoices()
    {
        $status = false;
        $count = 0;
        $attr = ProductAttribute::where('product_attribute_choice_id',$this->id)->first();
        if(!$attr)
        {
            return  [
                'count' => $count,
                'status' =>$status
                ];
        }
        $subs = Self::where('product_attribute_id',$attr->id)->get();
        foreach($subs as $sub)
        {
            if(!is_null($sub))
            {
                $count += $sub->count;
                $status = true;
            }
        }
        return  [
            'count'=>$count,
            'status' => $status
            ];
    }
    public function countChoices()
    {
        $attr = ProductAttribute::where('product_attribute_choice_id',$this->id)->first();
        if(!$attr)
        {
            return 0;
        }
        $count = Self::where('product_attribute_id',$attr->id)->sum('count');

        return  $count;
    }

}
