/*-
 * ~~~~~~licensing~~~~~~
 * object-producer-core
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.platform.core.producer.audit;

import org.osgi.framework.Filter;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ru.entaxy.audit.data.AuditEvent;
import ru.entaxy.audit.data.Outcome;
import ru.entaxy.audit.data.Severity;
import ru.entaxy.audit.service.EventConverter;
import ru.entaxy.audit.service.EventConverterInfo;
import ru.entaxy.audit.service.InterpretedEvent;
import ru.entaxy.audit.service.jmx.JMXInvokeEvent;

@Component(service = EventConverter.class, immediate = true)
@EventConverterInfo(classes = {JMXInvokeEvent.class})
public class ObjectProducerEventConverter implements EventConverter {

    private static final Logger LOG = LoggerFactory.getLogger(ObjectProducerEventConverter.class);

    @Override
    public <T extends InterpretedEvent> AuditEvent convert(T event) {
        if (!JMXInvokeEvent.class.equals(event.getClass()))
            return null;
        JMXInvokeEvent invokeEvent = (JMXInvokeEvent) event;
        Filter filter;
        try {
            filter = FrameworkUtil
                    .createFilter(
                            "(&(jmx.domain=ru.entaxy.esb)(jmx.methodName=createObjectByInstructions)(jmx.property.factoryId=*))");
        } catch (InvalidSyntaxException e) {
            LOG.error("Error creating filter", e);
            return null;
        }

        if (!filter.matches(event.getEventEssense())) {
            if (LOG.isDebugEnabled())
                LOG.debug("Event not matches: {}", event.toString());
            return null;
        }

        String objectId = invokeEvent.getMethodParams()[0].toString();
        String factoryId = event.getEventEssense().getOrDefault("jmx.property.factoryId", "unknown").toString();

        AuditEvent result = AuditEvent.AuditLoggingEventBuilder.getInstance()
                .target("ENTAXY OBJECTS")
                .outcome(Outcome.SUCCESS)
                .message(String.format("Object %s of factory %s was changed", objectId, factoryId))
                .severity(Severity.INFO)
                .category(String.format("%s:%s", objectId, factoryId))
                .build();

        return result;
    }

}
