<?php

declare(strict_types=1);

namespace App\Models\Document;

use App\Concerns\Models\SoftDeletes;
use App\Contracts\Models\SoftDeletable;
use App\Enums\Pivots\Document\PageRoleType;
use App\Events\Document\Page\PageCreated;
use App\Events\Document\Page\PageDeleted;
use App\Events\Document\Page\PageRestored;
use App\Events\Document\Page\PageUpdated;
use App\Models\Auth\Role;
use App\Models\BaseModel;
use App\Pivots\Document\PageRole;
use App\Scopes\ReadablePagesScope;
use Database\Factories\Document\PageFactory;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Collection;
use OwenIt\Auditing\Auditable as HasAudits;
use OwenIt\Auditing\Contracts\Auditable;

/**
 * @property string $body
 * @property Collection<int, Role> $editorRoles
 * @property string $name
 * @property int|null $next_id
 * @property Page|null $next
 * @property int $page_id
 * @property int|null $previous_id
 * @property Page|null $previous
 * @property Collection<int, Role> $roles
 * @property string $slug
 * @property Collection<int, Role> $viewerRoles
 *
 * @method static PageFactory factory(...$parameters)
 */
#[Hidden([Page::ATTRIBUTE_BODY])]
#[ScopedBy(ReadablePagesScope::class)]
#[Table(Page::TABLE, Page::ATTRIBUTE_ID)]
class Page extends BaseModel implements Auditable, SoftDeletable
{
    use HasAudits;
    use HasFactory;
    use SoftDeletes;

    final public const string TABLE = 'pages';

    final public const string ATTRIBUTE_BODY = 'body';
    final public const string ATTRIBUTE_ID = 'page_id';
    final public const string ATTRIBUTE_NAME = 'name';
    final public const string ATTRIBUTE_NEXT = 'next_id';
    final public const string ATTRIBUTE_PREVIOUS = 'previous_id';
    final public const string ATTRIBUTE_SLUG = 'slug';

    final public const string RELATION_EDITOR_ROLES = 'editorRoles';
    final public const string RELATION_NEXT = 'next';
    final public const string RELATION_PREVIOUS = 'previous';
    final public const string RELATION_ROLES = 'roles';
    final public const string RELATION_VIEWER_ROLES = 'viewerRoles';

    /**
     * The event map for the model.
     *
     * Allows for object-based events for native Eloquent events.
     *
     * @var array<string, class-string>
     */
    protected $dispatchesEvents = [
        'created' => PageCreated::class,
        'deleted' => PageDeleted::class,
        'restored' => PageRestored::class,
        'updated' => PageUpdated::class,
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        Page::ATTRIBUTE_BODY,
        Page::ATTRIBUTE_NAME,
        Page::ATTRIBUTE_NEXT,
        Page::ATTRIBUTE_PREVIOUS,
        Page::ATTRIBUTE_SLUG,
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            Page::ATTRIBUTE_BODY => 'string',
            Page::ATTRIBUTE_NAME => 'string',
            Page::ATTRIBUTE_NEXT => 'int',
            Page::ATTRIBUTE_PREVIOUS => 'int',
            Page::ATTRIBUTE_SLUG => 'string',
        ];
    }

    /**
     * Get the route key for the model.
     *
     * @noinspection PhpMissingParentCallCommonInspection
     */
    public function getRouteKeyName(): string
    {
        return Page::ATTRIBUTE_SLUG;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function getSubtitle(): string
    {
        return $this->slug;
    }

    public function isPublic(): bool
    {
        return $this->viewerRoles()->doesntExist();
    }

    /**
     * @return BelongsTo<Page, $this>
     */
    public function next(): BelongsTo
    {
        return $this->belongsTo(Page::class, Page::ATTRIBUTE_NEXT);
    }

    /**
     * @return BelongsTo<Page, $this>
     */
    public function previous(): BelongsTo
    {
        return $this->belongsTo(Page::class, Page::ATTRIBUTE_PREVIOUS);
    }

    /**
     * @return BelongsToMany<Role, $this, PageRole>
     */
    public function roles(): BelongsToMany
    {
        return $this->belongsToMany(Role::class, PageRole::TABLE, PageRole::ATTRIBUTE_PAGE, PageRole::ATTRIBUTE_ROLE)
            ->using(PageRole::class)
            ->withPivot([PageRole::ATTRIBUTE_ID, PageRole::ATTRIBUTE_TYPE])
            ->withTimestamps();
    }

    /**
     * @return BelongsToMany<Role, $this, PageRole>
     */
    public function viewerRoles(): BelongsToMany
    {
        return $this->roles()->wherePivot(PageRole::ATTRIBUTE_TYPE, PageRoleType::VIEWER->value);
    }

    /**
     * @return BelongsToMany<Role, $this, PageRole>
     */
    public function editorRoles(): BelongsToMany
    {
        return $this->roles()->wherePivot(PageRole::ATTRIBUTE_TYPE, PageRoleType::EDITOR->value);
    }
}
