<?php

namespace Modules\Category\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Modules\Category\Models\CategoryDescription;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Modules\Product\Models\Product;
use Modules\Project\Models\Project;
use Modules\Type\Models\Type;

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

    protected $fillable = [
        'parent_id',
        'show_footer',
        'max_count_order',
        'type_id',
        'show_home',
    ];


    public function sub() :HasMany
    {
        return $this->hasMany(Self::class,'parent_id');
    }
    public function main() :BelongsTo
    {
        return $this->belongsTo(Self::class,'parent_id')->withTrashed();
    }

    public function products() :HasMany
    {
        return $this->hasMany(Product::class,'category_id');
    }

    public function subProducts() :HasMany
    {
        return $this->hasMany(Product::class,'sub_category_id');
    }

    public function projects() :HasMany
    {
        return $this->hasMany(Project::class,'category_id');
    }

    public function category() :BelongsTo
    {
        return $this->belongsTo(Self::class,'parent_id')->withTrashed();
    }

    public function type() :BelongsTo
    {
        return $this->belongsTo(Type::class)->withTrashed();
    }

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

}
