<?php

namespace App\Traits;

use Illuminate\Support\Facades\App;

trait HasLocalizedAttributes
{
    public function __get($key)
    {
        $locale = App::getLocale();

        // If $key is localizable (e.g., short_description), map to short_description_en/ar
        if (property_exists($this, 'localizable') && in_array($key, $this->localizable)) {
            info("Key: {$key}, Locale: {$locale}");
            $localizedKey = "{$key}_{$locale}";

            if (array_key_exists($localizedKey, $this->attributes)) {
                return $this->getAttribute($localizedKey);
            }
        }

        return parent::__get($key);
    }
}
