<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class Translation extends Model
{
    protected $fillable = ['lang', 'key_name', 'value'];

    public function scopeForLang(Builder $query, string $lang): Builder
    {
        return $query->where('lang', $lang);
    }

    public static function getTranslation(string $key, string $lang, string $fallback = 'fr'): ?string
    {
        $translation = static::where('key_name', $key)->where('lang', $lang)->first();

        if (!$translation && $lang !== $fallback) {
            $translation = static::where('key_name', $key)->where('lang', $fallback)->first();
        }

        return $translation?->value;
    }
}
