<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Traits\Auditable;

class InventoryCategory extends Model
{
    use HasFactory, SoftDeletes;
    use Auditable;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'inventory_categories';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'description',
        'parent_id',
        'icon',
        'is_active'
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'is_active' => 'boolean',
    ];

    /**
     * Get the parent category.
     */
    public function parent()
    {
        return $this->belongsTo(InventoryCategory::class, 'parent_id');
    }

    /**
     * Get the child categories.
     */
    public function children()
    {
        return $this->hasMany(InventoryCategory::class, 'parent_id');
    }

    /**
     * Get all inventory items for this category.
     */
    public function items()
    {
        return $this->hasMany(Inventory::class, 'category_id');
    }

    /**
     * Get all active categories.
     */
    public static function getActive()
    {
        return self::where('is_active', true)->get();
    }

    /**
     * Get all parent categories.
     */
    public static function getParentCategories()
    {
        return self::whereNull('parent_id')->get();
    }
} 