<?php

namespace Shm\ShmChat\Classes;

use Shm\ShmDB\mDB;

class ChannelReference
{
    public function __construct(
        public mixed $documentId,
        public string $documentCollection,
    ) {
    }

    public static function make(mixed $documentId, string $documentCollection): self
    {
        return new self($documentId, $documentCollection);
    }

    /**
     * @return array{documentId: mixed, documentCollection: string}
     */
    public function toDocument(): array
    {
        return [
            'documentId' => mDB::id($this->documentId),
            'documentCollection' => $this->documentCollection,
        ];
    }

    public function documentIdKey(): string
    {
        return (string) $this->documentId;
    }

    public function matches(object|array $item): bool
    {
        if (is_array($item)) {
            return (string) ($item['documentId'] ?? '') === $this->documentIdKey()
                && ($item['documentCollection'] ?? null) === $this->documentCollection;
        }

        return (string) ($item->documentId ?? '') === $this->documentIdKey()
            && ($item->documentCollection ?? null) === $this->documentCollection;
    }
}
