<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\DB;

class Uom extends Model
{
    use HasFactory;

    // Table name (optional if it follows convention “uoms”)
    protected $table = 'uoms';

    // Mass-assignable fields
    protected $fillable = [
        'code',
        'description',
        'uom_category_id',
    ];

    /**
     * Relationship: each UOM belongs to one category.
     * Make sure App\Models\UomCategory exists with primary key id.
     */
    public function category()
    {
        return $this->belongsTo(UomCategory::class, 'uom_category_id');
    }

    /**
     * Static update method (optional raw query).
     */
    public static function updateUom($id, $code, $description, $uom_category_id)
    {
        return DB::update(
            "UPDATE uoms SET code = ?, description = ?, uom_category_id = ? WHERE id = ?",
            [$code, $description, $uom_category_id, $id]
        );
    }

    /**
     * Static delete method (optional raw query).
     */
    public static function deleteUom($id)
    {
        return DB::delete(
            "DELETE FROM uoms WHERE id = ?",
            [$id]
        );
    }
}
