<?php

namespace App\Models;

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

class HomeCurrency extends Model
{
    use HasFactory;

    protected $table = 'home_currencies';

    protected $fillable = [
        'code',
        'currency',  // This is the foreign key referencing currencies.id
        'description'
    ];

    /**
     * Relationship: points to the Currency model
     * This is the original relationship
     */
    public function currency()
    {
        return $this->belongsTo(Currency::class, 'currency', 'id');
    }

    /**
     * Alias relationship for controller eager loading
     * Use this if your controller is doing ->with('currencyRelation')
     */
    public function currencyRelation()
    {
        return $this->belongsTo(Currency::class, 'currency', 'id');
    }

    /**
     * Update a home currency record
     */
    public static function editHomeCurrency($id, $code, $currency, $description)
    {
        return DB::update(
            "UPDATE home_currencies SET code = ?, currency = ?, description = ? WHERE id = ?",
            [$code, $currency, $description, $id]
        );
    }

    /**
     * Delete a home currency record
     */
    public static function deleteHomeCurrency($id)
    {
        return DB::delete("DELETE FROM home_currencies WHERE id = ?", [$id]);
    }
}
