<?php

namespace App\Models\Localization;

use App\Models\Pages\Page;
use App\Support\RelationValuesTrait;
use App\Support\UuidScopeTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\App;

class Locale extends Model {
    use UuidScopeTrait, SoftDeletes, RelationValuesTrait;

    protected $dates = [
    ];

    protected $fillable = [
        'uuid',
        'name',
        'name_iso',
        'title',
        'rtl',
        'is_enabled',
        'is_default'
    ];

    protected $hidden = [
        'id'
    ];


    public function page(): BelongsTo {
        return $this->belongsTo(Page::class, 'name', 'slug');
    }


    public function scopeEnabled($query) {
        return $query->where(['is_enabled' => true]);
    }

    public function scopeDefault($query) {
        return $query->where(['name' => env('APP_LOCALE')]);
    }
    public function scopeNotDefault($query) {
        return $query->where('name', '!=', env('APP_LOCALE'));
    }


    public function getIsActiveAttribute(): bool {
        return $this->name === App::getLocale();
    }


    public function toggle(): bool {
        return $this->is_enabled ? $this->disable() : $this->enable();
    }
    public function enable(): bool {
        return $this->update(['is_enabled' => true]);
    }
    public function disable(): bool {
        return $this->update(['is_enabled' => false]);
    }


}
