<?php

namespace Shm\ShmChat\Classes;

use Shm\ShmAuth\Auth;
use Shm\ShmDB\mDB;

class ChatMember
{
    public function __construct(
        public mixed $owner,
        public string $ownerCollection,
        public string $role = 'member',
    ) {
    }

    public static function fromAuth(): self
    {
        return new self(Auth::getAuthID(), Auth::getAuthCollection());
    }

    /**
     * @return array{owner: mixed, ownerCollection: string, role: string}
     */
    public function toMemberDocument(): array
    {
        return [
            'owner' => mDB::id($this->owner),
            'ownerCollection' => $this->ownerCollection,
            'role' => $this->role,
        ];
    }

    public function ownerKey(): string
    {
        return (string) $this->owner;
    }

    public function matches(object $member): bool
    {
        return (string) ($member->owner ?? '') === $this->ownerKey()
            && ($member->ownerCollection ?? null) === $this->ownerCollection;
    }
}
