<?php

namespace App\Modules\Inventory\Models;

use App\Modules\Admin\Tenancy\Traits\BelongsToTenant;
use App\Modules\Inventory\Support\InventoryUserStamp;
use App\Modules\Inventory\Support\LogsInventoryCatalogActivity;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class ItemCategory extends Model
{
    use BelongsToTenant;
    use InventoryUserStamp;
    use LogsInventoryCatalogActivity;

    protected $table = 'inventory_categories';

    protected $fillable = [
        'tenant_id',
        'parent_id',
        'code',
        'name',
        'sort_order',
        'is_active',
        'deactivated_at',
        'created_by',
        'updated_by',
    ];

    protected function casts(): array
    {
        return [
            'is_active' => 'boolean',
            'deactivated_at' => 'datetime',
        ];
    }

    public function parent(): BelongsTo
    {
        return $this->belongsTo(ItemCategory::class, 'parent_id');
    }

    public function children(): HasMany
    {
        return $this->hasMany(ItemCategory::class, 'parent_id');
    }

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