package com.screenshot.server.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

@Configuration
@ConfigurationProperties(prefix = "telegram.bot")
public class TelegramBotConfig {

    private String token;
    private String username;
    private String allowedChatIds = "";
    private int maxFileSizeMb = 50;

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getAllowedChatIds() {
        return allowedChatIds;
    }

    public void setAllowedChatIds(String allowedChatIds) {
        this.allowedChatIds = allowedChatIds;
    }

    public Set<Long> getAllowedChatIdsSet() {
        if (allowedChatIds == null || allowedChatIds.isBlank()) {
            return Collections.emptySet();
        }
        return Arrays.stream(allowedChatIds.split(","))
            .map(String::trim)
            .filter(s -> !s.isEmpty())
            .map(Long::parseLong)
            .collect(Collectors.toSet());
    }

    public int getMaxFileSizeMb() {
        return maxFileSizeMb;
    }

    public void setMaxFileSizeMb(int maxFileSizeMb) {
        this.maxFileSizeMb = maxFileSizeMb;
    }

    public long getMaxFileSizeBytes() {
        return maxFileSizeMb * 1024L * 1024L;
    }

    public boolean isUserAllowed(Long chatId) {
        Set<Long> allowed = getAllowedChatIdsSet();
        // Если список пуст - разрешаем всем (для тестирования)
        if (allowed.isEmpty()) {
            return true;
        }
        return allowed.contains(chatId);
    }
}
