<?php

namespace App\Models;

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

class CarModel extends Model
{
    protected $fillable = ['car_make_id', 'name', 'slug', 'is_active'];

    protected $casts = ['is_active' => 'boolean'];

    protected static function boot()
    {
        parent::boot();
        static::creating(function ($model) {
            if (empty($model->slug)) {
                $model->slug = Str::slug($model->name) . '-' . Str::random(4);
            }
        });
    }

    public function make()
    {
        return $this->belongsTo(CarMake::class, 'car_make_id');
    }

    public function years()
    {
        return $this->hasMany(CarYear::class);
    }

    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }
}
