<?php

namespace App\Models;

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

class PriceListing extends Model
{
    use HasFactory;

    protected $table = 'price_listings'; // actual table name
    protected $fillable = ['item_id', 'price_list_id', 'price', 'unit', 'currency_id'];

    // Edit price listing
    public static function editPriceListing($id, $item_id, $price_list_id, $price, $unit, $currency_id)
    {
        return DB::update(
            "UPDATE price_listings SET item_id = ?, price_list_id = ?, price = ?, unit = ?, currency_id = ? WHERE id = ?",
            [$item_id, $price_list_id, $price, $unit, $currency_id, $id]
        );
    }

    // Delete price listing
    public static function deletePriceListing($id)
    {
        return DB::delete(
            "DELETE FROM price_listings WHERE id = ?",
            [$id]
        );
    }

    // Optional: relationships (for use in views/controllers)
    public function item()
    {
        return $this->belongsTo(InventoryItem::class, 'item_id');
    }

    public function priceList()
    {
        return $this->belongsTo(PriceListName::class, 'price_list_id');
    }

    public function currency()
    {
        return $this->belongsTo(Currency::class, 'currency_id');
    }
}
