<?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\Update\ChatTitleChangedUpdate;
use Pkirillw\MaxBotApi\Scheme\Update\UpdateInterface;
use Psr\Log\LoggerInterface;

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

    public function handle(UpdateInterface $update): void
    {
        if (!$update instanceof ChatTitleChangedUpdate) {
            return;
        }
        $this->logger->info('chat title changed', [
            'chat_id' => $update->chatId,
            'new_title' => $update->title,
            'by_user_id' => $update->user->userId,
        ]);
        $this->api->messages->send(
            MessageBuilder::new()
                ->setChat($update->chatId)
                ->setText(sprintf('📝 Новый заголовок чата: *%s*', $update->title))
                ->setFormat(Format::Markdown),
        );
    }
}
