<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class SubCategory extends Model
{
    use HasFactory;
    use SoftDeletes;
    protected $fillable =['name','category_id','status','store_detail_id','storeowner_id'];
    protected $casts = [
        'category_id'       => 'integer',
        'store_detail_id'          => 'integer',
        'storeowner_id'     => 'integer',
        'status'            => 'integer',
    ];

    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id', 'id');
    }
    public function store()
    {
        return $this->belongsTo(Store::class, 'store_detail_id', 'id');
    }
    public function storowner(){
        return $this->belongsTo(User::class, 'storeowner_id','id');
    }

    public function scopeMySubcategory($query){
        $user = auth()->user();
        if(in_array($user->user_type, ['admin'])) {
            return $query;
        }
        if($user->user_type == 'storeowner') {
            return $query->where('store_detail_id', $user->store_detail_id)->whereStatus(1);
        }
    }
}
