<?php

namespace App\Models;

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

class CarModel extends Model
{
    use HasFactory;

    protected $fillable = [
        'make_id',
        'name',
        'year',
        'status',
        'created_by',
    ];

    protected $casts = [
        'status' => 'string',
        'year' => 'integer',
    ];

    /**
     * Relationships
     */
    public function make()
    {
        return $this->belongsTo(CarMake::class, 'make_id');
    }

    public function creator()
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function products()
    {
        return $this->hasMany(Product::class, 'model_id');
    }

    /**
     * Scope for active models
     */
    public function scopeActive($query)
    {
        return $query->where('status', 'active');
    }

    /**
     * Scope for models by make
     */
    public function scopeByMake($query, $makeId)
    {
        return $query->where('make_id', $makeId);
    }
}