<?php

namespace App\Models\Registries;

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\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

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

    protected $dates = [
    ];

    protected $fillable = [
        'uuid',
        'page_id',
        'type'
    ];

    protected $hidden = [
        'id'
    ];


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

    public function categories(): HasMany {
        return $this->hasMany(Category::class);
    }

    public function rootCategories(): HasMany {
        return $this->categories()->where(['parent_id' => 0]);
    }

    public function entries(): HasMany {
        return $this->hasMany(Entry::class);
    }



    public function getParsedTypeAttribute(): array {
        return ['name' => $this->type, 'title' => RegistryType::TITLES[$this->type] ?? null, 'options' => $this->options];
    }

    public function getOptionsAttribute(): array {
        return RegistryType::OPTIONS[$this->type] ?? [];
    }


}
