<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Str;

class VendorCategory extends Model
{
    use HasFactory;

    protected $table = 'vendor_categories';

    protected $fillable = [
        'name',
        'description',
        'track_id',
        'created_by',
        'status',
    ];

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($category) {
            if (empty($category->track_id)) {
                $category->track_id = 'CAT_' . strtoupper(Str::random(6));
            }
        });
    }

    /**
     * Relationship: created by an admin
     */
    public function creator()
    {
        return $this->belongsTo(Admin::class, 'created_by');
    }

    /**
     * Relationship: has many vendor sub-categories
     */
    public function subCategories()
    {
        return $this->hasMany(VendorSubCategory::class, 'category_id');
    }
}
