<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Translation extends Model
{
    protected $keyType = 'string';
    public $incrementing = false;

    protected $fillable = [
        'key',
        'locale',
        'value',
    ];

    public function scopeLocale($query, $locale)
    {
        return $query->where('locale', $locale);
    }

    public function getKeyNameAttribute()
    {
        return $this->key;
    }

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            if (!$model->id) {
                $model->id = (string) Str::uuid();
            }
        });

        // 🔥 CACHE INVALIDIEREN (WICHTIG!)
        static::saved(function ($model) {
            cache()->forget("translations:{$model->locale}");
        });

        static::deleted(function ($model) {
            cache()->forget("translations:{$model->locale}");
        });
    }
}
