<?php

namespace App\Models;

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

class VendorSubCategory extends Model
{
    use HasFactory;

    protected $table = 'vendor_sub_categories';

    protected $fillable = [
        'name',
        'description',
        'track_id',
        'category_id',   // foreign key to vendor_categories
        'created_by',
        'status',
    ];

    /**
     * Auto-generate track_id if not provided.
     */
    protected static function boot()
    {
        parent::boot();

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

    /**
     * Relationship: belongs to a Vendor Category
     */
    public function category()
    {
        return $this->belongsTo(VendorCategory::class, 'category_id');
    }

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