<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Translation extends Model
{
    protected $fillable = [
        'language_code',
        'group',
        'key',
        'value'
    ];

    public function language()
    {
        return $this->belongsTo(Language::class, 'language_code', 'code');
    }

    public static function get($key, $group = 'general', $default = null)
    {
        $locale = app()->getLocale();

        $translation = static::where('language_code', $locale)
            ->where('group', $group)
            ->where('key', $key)
            ->first();

        return $translation ? $translation->value : ($default ?? $key);
    }
}
