package de.assecutor.votianlt.messaging.plugin;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

/**
 * Event representing a connection state change.
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ConnectionStateEvent {

    /**
     * Connection state
     */
    private ConnectionState state;

    /**
     * Previous connection state
     */
    private ConnectionState previousState;

    /**
     * Timestamp of the state change
     */
    @Builder.Default
    private LocalDateTime timestamp = LocalDateTime.now();

    /**
     * Optional error message if state is ERROR or DISCONNECTED
     */
    private String errorMessage;

    /**
     * Optional exception if state is ERROR
     */
    private Throwable exception;

    /**
     * Plugin that generated this event
     */
    private String pluginName;

    /**
     * Connection states
     */
    public enum ConnectionState {
        /**
         * Plugin is initializing
         */
        INITIALIZING,

        /**
         * Plugin is connecting to the transport
         */
        CONNECTING,

        /**
         * Plugin is connected and ready
         */
        CONNECTED,

        /**
         * Plugin is disconnecting
         */
        DISCONNECTING,

        /**
         * Plugin is disconnected
         */
        DISCONNECTED,

        /**
         * Plugin encountered an error
         */
        ERROR,

        /**
         * Plugin is reconnecting after a failure
         */
        RECONNECTING
    }

    /**
     * Check if the connection is active.
     *
     * @return true if connected
     */
    public boolean isConnected() {
        return state == ConnectionState.CONNECTED;
    }

    /**
     * Check if there was an error.
     *
     * @return true if state is ERROR
     */
    public boolean isError() {
        return state == ConnectionState.ERROR;
    }
}

