<?php
/*
 *  Copyright 2022.  Baks.dev <admin@baks.dev>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

namespace BaksDev\Users\Profile\UserProfile\Message\ModerationUserProfile;

use BaksDev\Users\Profile\UserProfile\Message\ModerationUserProfile\ModerationUserProfileDTO;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Symfony\Component\Notifier\Bridge\Telegram\Reply\Markup\Button\InlineKeyboardButton;
use Symfony\Component\Notifier\Bridge\Telegram\Reply\Markup\InlineKeyboardMarkup;
use Symfony\Component\Notifier\ChatterInterface;
use Symfony\Component\Notifier\Exception\TransportExceptionInterface;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

final class ModerationUserProfileHandler implements MessageHandlerInterface
{
    private ChatterInterface $chatter;
    private UrlGeneratorInterface $urlGenerator;
    private TranslatorInterface $translator;
    
    public function __construct(ChatterInterface $chatter, UrlGeneratorInterface $urlGenerator, TranslatorInterface $translator)
    {
        $this->chatter = $chatter;
        $this->urlGenerator = $urlGenerator;
        $this->translator = $translator;
    }
    
    public function __invoke(ModerationUserProfileDTO $command)
    {
        
        $chatMessage = new ChatMessage(
          
          $this->translator->trans('admin.moderation.message', [], 'userprofile')
        );
        
        $url = $this->urlGenerator->generate(
          'users-profile-user:admin.newedit.edit',
          ['id' => $command->getEvent()],
          UrlGeneratorInterface::ABSOLUTE_URL);

        $telegramOptions = (new TelegramOptions())
          ->parseMode('MarkdownV2')
          ->disableWebPagePreview(true)
          ->disableNotification(true)
          ->replyMarkup(
            (new InlineKeyboardMarkup())
              ->inlineKeyboard([
                                 (new InlineKeyboardButton(
                                   $this->translator->trans('admin.moderation.btn', [], 'userprofile')) // Перейти в профиль для модерации
                                 )
                                   ->url($url),
                               ])
          );
        
        // Add the custom options to the chat message and send the message
        $chatMessage->options($telegramOptions);
        
        $this->chatter->send($chatMessage);
        
        return true;
    }
    
}

