<?php

namespace Modules\Offer\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Modules\Category\Models\Category;
use Modules\Product\Models\Product;
use Modules\Store\Models\Store;

class Offer extends Model
{
    //use soft delete
    use SoftDeletes;

    protected $fillable =
    [
        'status','start_date','end_date','sub_category_id','category_id','percentage'
    ];

    /**
     * Get current description of the the Room
     * @return HasOne
     */
    public function currentDescription() :HasOne
    {
        return $this->hasOne(OfferDescription::class)->where('language_id', currentLanguage()->id);
    }

    public function store()
    {
        return $this->belongsTo(Store::class,'store_id')->withTrashed();
    }

    public function category()
    {
        return $this->belongsTo(Category::class,'category_id')->withTrashed();
    }

    public function subCategory()
    {
        return $this->belongsTo(Category::class,'sub_category_id')->withTrashed();
    }

    public function products()
    {
        return $this->belongsToMany(Product::class,'offer_products');
    }

    public function getCountProductsAttribute()
    {
        $count = count($this->products);
//        if (!$count)
//        {
//            return count($this->subCategory->subProducts);
//        }
        return $count;
    }
}
