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

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

    public function handle(UpdateInterface $update): void
    {
        if (!$update instanceof MessageEditedUpdate) {
            return;
        }
        $mid = $update->message->body?->mid ?? '?';
        $text = $update->message->body?->text ?? '';
        $this->api->messages->send(
            MessageBuilder::new()
                ->setChat($update->getChatId())
                ->setText(sprintf('✎ *Отредактировано* (%s): %s', $mid, mb_substr($text, 0, 200)))
                ->setFormat(Format::Markdown),
        );
    }
}
