<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class UserPlan extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'name_ar',
        'description',
        'description_ar',
        'type',
        'duration',
        'user_id',
        'plan_id',
        'duration',
        'price',
        'currency',
        'tax',
        'payment_method',
        'total_price',
        'discount',
        'promo_code',
        'payment_id',
        'expiry_date',
        'status',
        'request_count',
    ];
    
    protected $translatable = ['name','description'];
    public function getAttributes()
    {
        foreach($this->translatable as $attribute) {
            $locale = app()->getLocale();
            $localizedKey = "{$attribute}_{$locale}";
            if (isset($this->attributes[$localizedKey])) {
                $this->attributes[$attribute] =  (empty($this->attributes[$localizedKey]))?$this->attributes["{$attribute}"]:$this->attributes[$localizedKey];
            }
        }
        return $this->attributes;
    }

    public function plan()
    {
        return $this->belongsTo(PricingPlan::class, 'ad_plan_id');
    }

}