<?php

namespace App\Models\Pages;

use App\Models\Objects\Field;
use App\Models\Publications\Publication;
use App\Models\Registries\Registry;
use App\Models\User;
use App\Support\HasObjectsTrait;
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\Relations\MorphToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;

class Page extends Model {
    use UuidScopeTrait, SoftDeletes, HasObjectsTrait, RelationValuesTrait;

    protected $dates = [
    ];

    protected $fillable = [
        'uuid',
        'parent_id',
        'slug',
        'type',
        'sub_type',
        'name',
        'title',
        'h1',
        'ord'
    ];

    protected $hidden = [
        'id'
    ];


    public function parent(): BelongsTo {
        return $this->belongsTo(Page::class, 'parent_id');
    }

    public function children(): HasMany {
        return $this->hasMany(Page::class, 'parent_id')->orderBy('ord');
    }

    public function sections(): MorphToMany {
        return $this->objects()->wherePivot('group', '=', 'sections');
    }

    public function sidebars(): MorphToMany {
        return $this->objects()->wherePivot('group', '=', 'sidebars');
    }

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

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


    public function scopeBySlug($query, $slug) {
        $query->where(['slug' => $slug]);
    }

    public function scopeNthParentSlug($query, $nth, $slug) {
        $query->whereHas(implode('.', array_fill(0, $nth, 'parent')), function($query) use($slug) {
            $query->bySlug($slug);
        });
    }


    public function getLinkAttribute(): string {
        return '/' . $this->parents->reverse()->push($this)->pluck('slug')->implode('/');
    }

    public function getParentsAttribute(): Collection {
        $page = $this;
        $result = collect([]);
        while ($page = $page->parent) $result->push($page);
        return $result;
    }

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

    public function getRegistryAttribute(): Model {
        return $this->registries()->firstOrCreate();
    }



    public function isEditable(User $user): bool {
        return $user->isModerator && $user->membership()->whereHas('objects', function($query) {
                Field::applyFilters($query, collect(['types' => 'company-member-properties', 'moderate-pages' => $this->parents->add($this)->pluck('uuid')->all()]));
            })->exists();
    }

    public function addSection($typeName, $ord = null): ?Model {
        return $this->createObject($typeName, $ord, 'sections');
    }

    public function addSidebar($typeName = 'page-sidebar', $ord = null): ?Model {
        return $this->createObject($typeName, $ord, 'sidebars');
    }


    public static function byUrl($url) {
        if ($url = trim($url, '/ ')) {
            $query = self::query();
            collect(explode('/', $url))->reverse()->values()->each(function($slug, $index) use ($query) {
                if ($slug !== '') {
                    $index ? $query->nthParentSlug($index, $slug) : $query->bySlug($slug);
                }
            });
            return $query->first();
        }
        return null;
    }

    public static function root() {
        return self::query()->where(['parent_id' => 0])->orderBy('ord')->get();
    }

}
