<?php

namespace App\Models;

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

class Packages extends Model
{
    use HasFactory;

    protected $fillable = [
        'track_id',
        'vendor_id',
        'sub_category_id',
        'name',
        'description',
        'images',
        'price',
        'quantity',
        'status',
    ];

    protected $casts = [
        'images' => 'array', // Automatically handle JSON array
    ];

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

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