<?php

namespace App\Models;

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

class Unit extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'symbol',
        'is_active',
    ];

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

    // Scope for active units
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    // Relationship to products
    public function products(): HasMany
    {
        return $this->hasMany(Product::class);
    }

    // Formatted display
    public function getFormattedNameAttribute(): string
    {
        return "{$this->name} ({$this->symbol})";
    }
}
