<?php

namespace Shm\ShmChat;

/**
 * Канал чата: все действия над одним каналом.
 */
class Channel
{
    public function __construct(
        private readonly string $channelId,
    ) {
    }

    public function id(): string
    {
        return $this->channelId;
    }

    public function doc(): ?object
    {
        return ShmChatService::getChannel($this->channelId);
    }

    /**
     * @param array<int, mixed> $attachments
     * @param array<string, mixed>|null $payload
     */
    public function send(
        Member $sender,
        string $text = '',
        ?string $reply = null,
        array $attachments = [],
        mixed $voice = null,
        ?array $payload = null,
        ?string $clientMessageId = null,
    ): object {
        return ShmChatService::sendMessage(
            $this->channelId,
            $sender,
            $text,
            $reply,
            $attachments,
            $voice,
            $payload,
            $clientMessageId,
        );
    }

    public function system(string $text, ?Member $by = null): object
    {
        return ShmChatService::sendSystemMessage($this->channelId, $text, $by);
    }

    public function close(?Member $by = null): bool
    {
        return ShmChatService::closeChannel($this->channelId, $by);
    }

    public function markRead(Member $reader, ?string $lastReadMessageId = null): void
    {
        ShmChatService::markRead($this->channelId, $reader, $lastReadMessageId);
    }

    public function addMember(Member $member): bool
    {
        return ShmChatService::addMember($this->channelId, $member);
    }

    public function link(Link $link): bool
    {
        return ShmChatService::addLink($this->channelId, $link);
    }

    public function unlink(Link $link): bool
    {
        return ShmChatService::removeLink($this->channelId, $link);
    }

    /**
     * @return array{data?: array<int, mixed>, hash: string}
     */
    public function messages(?ListQueryInput $query = null): array
    {
        return ShmChatService::listMessages($this->channelId, $query ?? new ListQueryInput());
    }

    public function deleteMessage(string $messageId, Member $by): bool
    {
        return ShmChatService::deleteMessage($this->channelId, $messageId, $by);
    }

    public function react(string $messageId, string $reaction, Member $user): bool
    {
        return ShmChatService::toggleReaction($this->channelId, $messageId, $reaction, $user);
    }

    public function togglePinned(string $messageId, Member $by): bool
    {
        return ShmChatService::togglePinnedMessage($messageId, $by);
    }
}
