package dev.lions.unionflow.server.repository;

import dev.lions.unionflow.server.entity.AlertConfiguration;
import io.quarkus.arc.Unremovable;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.persistence.TypedQuery;

import java.util.Optional;

/**
 * Repository pour l'entité AlertConfiguration (singleton)
 *
 * @author UnionFlow Team
 * @version 1.0
 * @since 2026-03-15
 */
@ApplicationScoped
@Unremovable
public class AlertConfigurationRepository extends BaseRepository<AlertConfiguration> {

    public AlertConfigurationRepository() {
        super(AlertConfiguration.class);
    }

    /**
     * Récupérer la configuration unique des alertes.
     * Crée une configuration par défaut si elle n'existe pas.
     */
    public AlertConfiguration getConfiguration() {
        TypedQuery<AlertConfiguration> query = entityManager.createQuery(
                "SELECT c FROM AlertConfiguration c",
                AlertConfiguration.class
        );
        query.setMaxResults(1);

        Optional<AlertConfiguration> config = query.getResultList().stream().findFirst();

        if (config.isPresent()) {
            return config.get();
        } else {
            // Créer une configuration par défaut
            AlertConfiguration defaultConfig = new AlertConfiguration();
            persist(defaultConfig);
            return defaultConfig;
        }
    }

    /**
     * Mettre à jour la configuration des alertes
     */
    public AlertConfiguration updateConfiguration(AlertConfiguration config) {
        AlertConfiguration existing = getConfiguration();

        // Mettre à jour tous les champs
        existing.setCpuHighAlertEnabled(config.getCpuHighAlertEnabled());
        existing.setCpuThresholdPercent(config.getCpuThresholdPercent());
        existing.setCpuDurationMinutes(config.getCpuDurationMinutes());
        existing.setMemoryLowAlertEnabled(config.getMemoryLowAlertEnabled());
        existing.setMemoryThresholdPercent(config.getMemoryThresholdPercent());
        existing.setCriticalErrorAlertEnabled(config.getCriticalErrorAlertEnabled());
        existing.setErrorAlertEnabled(config.getErrorAlertEnabled());
        existing.setConnectionFailureAlertEnabled(config.getConnectionFailureAlertEnabled());
        existing.setConnectionFailureThreshold(config.getConnectionFailureThreshold());
        existing.setConnectionFailureWindowMinutes(config.getConnectionFailureWindowMinutes());
        existing.setEmailNotificationsEnabled(config.getEmailNotificationsEnabled());
        existing.setPushNotificationsEnabled(config.getPushNotificationsEnabled());
        existing.setSmsNotificationsEnabled(config.getSmsNotificationsEnabled());
        existing.setAlertEmailRecipients(config.getAlertEmailRecipients());

        persist(existing);
        return existing;
    }

    /**
     * Vérifier si les alertes CPU sont activées
     */
    public boolean isCpuAlertEnabled() {
        return getConfiguration().getCpuHighAlertEnabled();
    }

    /**
     * Vérifier si les alertes mémoire sont activées
     */
    public boolean isMemoryAlertEnabled() {
        return getConfiguration().getMemoryLowAlertEnabled();
    }

    /**
     * Récupérer le seuil CPU
     */
    public int getCpuThreshold() {
        return getConfiguration().getCpuThresholdPercent();
    }

    /**
     * Récupérer le seuil mémoire
     */
    public int getMemoryThreshold() {
        return getConfiguration().getMemoryThresholdPercent();
    }
}
