<?php

namespace App\Models;

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

class UomConversion extends Model
{
    use HasFactory;

    protected $table = 'uom_conversions';

    protected $fillable = [
        'uom_category_id',
        'from_uom_id',
        'to_uom_id',
        'factor',
    ];

    /**
     * Relationship: conversion belongs to a category.
     */
    public function category()
    {
        return $this->belongsTo(UomCategory::class, 'uom_category_id');
    }

    /**
     * Relationship: the 'from' unit.
     */
    public function fromUnit()
    {
        return $this->belongsTo(Uom::class, 'from_uom_id');
    }

    /**
     * Relationship: the 'to' unit.
     */
    public function toUnit()
    {
        return $this->belongsTo(Uom::class, 'to_uom_id');
    }

    /**
     * Static update method (raw query).
     */
    public static function updateConversion($id, $uom_category_id, $from_uom_id, $to_uom_id, $factor)
    {
        return DB::update(
            "UPDATE uom_conversions
             SET uom_category_id = ?, from_uom_id = ?, to_uom_id = ?, factor = ?
             WHERE id = ?",
            [$uom_category_id, $from_uom_id, $to_uom_id, $factor, $id]
        );
    }

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