<?php

namespace App\Models;

use App\Models\BaseModel;
use Illuminate\Database\Eloquent\SoftDeletes;

class Warehouse extends BaseModel
{
    use SoftDeletes;

    protected $fillable = [
        'company_id','parent_id','code','name','type',
        'address','is_active','depth','path'
    ];

    protected $casts = ['is_active' => 'boolean'];

    public function company()  { return $this->belongsTo(Company::class); }
    public function parent()   { return $this->belongsTo(Warehouse::class, 'parent_id'); }
    public function children() { return $this->hasMany(Warehouse::class, 'parent_id'); }
    public function items()    { return $this->hasMany(Item::class, 'default_warehouse_id'); }

    public function scopeActive($q) { return $q->where('is_active', true); }

    public function isLeaf(): bool
    {
        return $this->children()->count() === 0;
    }
}
