<?php

namespace App\Models;

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

class PricingPlan extends Model
{
    use HasFactory;
    
    protected $fillable = [
        'name',
        'name_ar',
        'type',
        'duration',
        'price',
        'currency',
        'description',
        'description_ar',
        'status',
        'max_request_count'
    ];
    public $appends = ['duration_text','type_text'];
    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 getDurationTextAttribute(){
        $locale = app()->getLocale();
        if($locale=='ar'){
            if($this->duration == 'monthly'){
                return 'شهريا';
            }
            if($this->duration == 'quarterly'){
                return 'ربع سنوي';
            }
            if($this->duration == 'yearly'){
                return 'سنوي';
            }
        }
        return $this->duration;
    }
    
    public function getTypeTextAttribute(){
        $locale = app()->getLocale();
        if($locale=='ar'){
            if($this->type == 'free'){
                return 'حر';
            }
            if($this->type == 'basic'){
                return 'أساسي';
            }
            if($this->type == 'premium'){
                return 'غالي';
            }
        }
        return $this->type;
    }
    

    public function variants()
    {
        return $this->hasMany(PlanDuration::class, 'pricing_plan_id');
    }

}