package ru.kayashov.bar.bot.domain.command.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import ru.kayashov.bar.bot.annotation.CommandData;
import ru.kayashov.bar.bot.domain.command.api.Command;
import ru.kayashov.bar.bot.domain.methods.AbstractMethod;
import ru.kayashov.bar.bot.domain.model.AbstractChat;
import ru.kayashov.bar.bot.domain.model.AbstractUpdate;
import ru.kayashov.bar.model.entity.Visitor;
import ru.kayashov.bar.repository.VisitorsRepository;

import java.util.List;
import java.util.Optional;

import static ru.kayashov.bar.bot.domain.keyboards.reply.ReplyKeyboardMarker.getMainMenu;
import static ru.kayashov.bar.bot.domain.utils.MethodUtils.sendKeyboardMessage;

/**
 * Команда обработки запроса меню
 * <p>
 * Реализация {@link }
 */
@Component
@CommandData(name = "/start")
@RequiredArgsConstructor
public class StartCommand implements Command {

    private final VisitorsRepository visitorsRepository;

    @Override
    public List<? extends AbstractMethod> execute(AbstractUpdate abstractUpdate) {
        AbstractChat chat = abstractUpdate.getMessage().getChat();

        Optional<Visitor> visitorOpt = visitorsRepository.findById(chat.getId());
        String message;
        Visitor visitor;
        if (visitorOpt.isPresent()) {
            visitor = visitorOpt.get();
            message = "С возвращением ";
        } else {
            message = "Спасибо за регистрацию ";
            visitor = new Visitor();
            visitor.setId(chat.getId());
            visitor.setName(chat.getFirstName());
            visitor.setLastName(chat.getLastName());
            visitor = visitorsRepository.save(visitor);
        }
        message = message + visitor.getName().strip() +
                (visitor.getLastName() != null ? (" " + visitor.getLastName().strip() + "!") : "!");


        return List.of(sendKeyboardMessage(chat.getId(), message, getMainMenu()));
    }
}
