<?php

declare(strict_types=1);

namespace App\Scenario;

use App\Util\ParsedCommand;
use Pkirillw\MaxBotApi\Builder\Message as MessageBuilder;
use Pkirillw\MaxBotApi\Scheme\Enum\Format;
use Pkirillw\MaxBotApi\Scheme\Update\MessageCreatedUpdate;

final class ChatsScenario extends AbstractScenario
{
    public function handle(MessageCreatedUpdate $update, ParsedCommand $command): void
    {
        switch ($command->name) {
            case 'chats':
                $this->listChats($update, $command);
                return;
            case 'chat':
                $this->showChat($update, $command);
                return;
            case 'chat_membership':
                $this->showMembership($update, $command);
                return;
        }
    }

    private function listChats(MessageCreatedUpdate $update, ParsedCommand $command): void
    {
        $count = $command->argInt(0, 10);
        $list = $this->api->chats->getChats($count);
        $lines = [];
        foreach ($list->chats as $chat) {
            $lines[] = sprintf('• `%d` %s (type=%s, members=%d)', $chat->chatId, $chat->title ?? '—', $chat->type?->value ?? '?', $chat->participantsCount);
        }
        $text = "*Chats* (count=$count, marker=" . ($list->marker ?? 'null') . ")\n" . ($lines === [] ? '(empty)' : implode("\n", $lines));
        $this->api->messages->send(
            MessageBuilder::new()->setChat($update->getChatId())->setText($text)->setFormat(Format::Markdown),
        );
    }

    private function showChat(MessageCreatedUpdate $update, ParsedCommand $command): void
    {
        $chatId = $command->argInt(0);
        if ($chatId <= 0) {
            $this->replyMarkdown($update, 'Usage: `/chat <chatId>`');
            return;
        }
        $chat = $this->api->chats->getChat($chatId);
        $text = sprintf(
            "*Chat %d*\ntitle: %s\ntype: %s\nstatus: %s\nowner: %d\nmembers: %d\npublic: %s\nlink: %s\ndescription: %s",
            $chat->chatId,
            $chat->title ?? '—',
            $chat->type?->value ?? '?',
            $chat->status?->value ?? '?',
            $chat->ownerId,
            $chat->participantsCount,
            $chat->isPublic ? 'yes' : 'no',
            $chat->link ?? '—',
            $chat->description ?? '—',
        );
        $this->api->messages->send(
            MessageBuilder::new()->setChat($update->getChatId())->setText($text)->setFormat(Format::Markdown),
        );
    }

    private function showMembership(MessageCreatedUpdate $update, ParsedCommand $command): void
    {
        $chatId = $command->argInt(0);
        if ($chatId <= 0) {
            $this->replyMarkdown($update, 'Usage: `/chat_membership <chatId>`');
            return;
        }
        $m = $this->api->chats->getChatMembership($chatId);
        $perms = array_map(fn($p) => $p->value, $m->permissions);
        $text = sprintf(
            "*Membership in %d*\nisOwner: %s\nisAdmin: %s\njoinTime: %d\npermissions: %s",
            $chatId,
            $m->isOwner ? 'yes' : 'no',
            $m->isAdmin ? 'yes' : 'no',
            $m->joinTime,
            $perms === [] ? '(none)' : '`' . implode('`, `', $perms) . '`',
        );
        $this->api->messages->send(
            MessageBuilder::new()->setChat($update->getChatId())->setText($text)->setFormat(Format::Markdown),
        );
    }
}
