<?php

declare(strict_types=1);

namespace App\Handler\Update;

use App\Handler\AbstractUpdateHandler;
use Pkirillw\MaxBotApi\Api;
use Pkirillw\MaxBotApi\Builder\Message as MessageBuilder;
use Pkirillw\MaxBotApi\Scheme\Enum\Format;
use Pkirillw\MaxBotApi\Scheme\Enum\SenderAction;
use Pkirillw\MaxBotApi\Scheme\Update\BotAddedToChatUpdate;
use Pkirillw\MaxBotApi\Scheme\Update\UpdateInterface;
use Psr\Log\LoggerInterface;

final class BotAddedToChatHandler extends AbstractUpdateHandler
{
    public function updateClass(): string { return \Pkirillw\MaxBotApi\Scheme\Update\BotAddedToChatUpdate::class; }

    public function handle(UpdateInterface $update): void
    {
        if (!$update instanceof BotAddedToChatUpdate) {
            return;
        }
        $this->logger->info('bot added to chat', [
            'chat_id' => $update->chatId,
            'by_user_id' => $update->user->userId,
        ]);
        // Use chats->sendAction to demo SenderAction::TypingOn.
        $this->api->chats->sendAction($update->chatId, SenderAction::TypingOn);
        $this->api->messages->send(
            MessageBuilder::new()
                ->setChat($update->chatId)
                ->setText("👋 Спасибо за приглашение! Набери `/help` для списка команд.")
                ->setFormat(Format::Markdown),
        );
        $this->api->chats->sendAction($update->chatId, SenderAction::TypingOff);
    }
}
