package com.iqser.red.service.persistence.management.v1.processor.configuration;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import lombok.RequiredArgsConstructor;

@Configuration
@RequiredArgsConstructor
public class UserMessagingConfiguration {

    public static final String PERSISTENCE_SERVICE_USER_CREATED_QUEUE = "persistence-service-user-created-queue";
    public static final String PERSISTENCE_SERVICE_USER_DELETED_QUEUE = "persistence-service-user-deleted-queue";
    public static final String PERSISTENCE_SERVICE_USER_UPDATED_QUEUE = "persistence-service-user-updated-queue";
    public static final String PERSISTENCE_SERVICE_USER_STATUS_CHANGED_QUEUE = "persistence-service-user-status-changed-queue";
    public static final String PERSISTENCE_SERVICE_USER_ROLES_UPDATED_QUEUE = "persistence-service-user-roles-updated-queue";
    public static final String PERSISTENCE_SERVICE_USER_OWN_PROFILE_UPDATED_QUEUE = "persistence-service-user-own-profile-updated-queue";
    public static final String PERSISTENCE_SERVICE_USER_EVENTS_DQL = "persistence-service-user-events-dql";


    @Bean("persistenceServiceUserRolesUpdatedQueue")
    public Queue persistenceServiceUserRolesUpdatedQueue() {

        return QueueBuilder.durable(PERSISTENCE_SERVICE_USER_ROLES_UPDATED_QUEUE)
                .withArgument("x-dead-letter-exchange", "")
                .withArgument("x-dead-letter-routing-key", PERSISTENCE_SERVICE_USER_EVENTS_DQL)
                .build();
    }


    @Bean
    public Binding userRolesUpdatedBinding(@Qualifier("persistenceServiceUserRolesUpdatedQueue") Queue persistenceServiceUserRolesUpdatedQueue,
                                           @Qualifier("userExchange") TopicExchange userExchange) {

        return BindingBuilder.bind(persistenceServiceUserRolesUpdatedQueue).to(userExchange).with("user.rolesUpdated");
    }


    @Bean("persistenceServiceUserStatusChangedQueue")
    public Queue persistenceServiceUserStatusChangedQueue() {

        return QueueBuilder.durable(PERSISTENCE_SERVICE_USER_STATUS_CHANGED_QUEUE)
                .withArgument("x-dead-letter-exchange", "")
                .withArgument("x-dead-letter-routing-key", PERSISTENCE_SERVICE_USER_EVENTS_DQL)
                .build();
    }


    @Bean
    public Binding userStatusChangedBinding(@Qualifier("persistenceServiceUserStatusChangedQueue") Queue persistenceServiceUserStatusChangedQueue,
                                            @Qualifier("userExchange") TopicExchange userExchange) {

        return BindingBuilder.bind(persistenceServiceUserStatusChangedQueue).to(userExchange).with("user.statusChanged");
    }


    @Bean("persistenceServiceUserUpdatedQueue")
    public Queue persistenceServiceUserUpdatedQueue() {

        return QueueBuilder.durable(PERSISTENCE_SERVICE_USER_UPDATED_QUEUE)
                .withArgument("x-dead-letter-exchange", "")
                .withArgument("x-dead-letter-routing-key", PERSISTENCE_SERVICE_USER_EVENTS_DQL)
                .build();
    }


    @Bean
    public Binding userUpdatedBinding(@Qualifier("persistenceServiceUserUpdatedQueue") Queue persistenceServiceUserUpdatedQueue,
                                      @Qualifier("userExchange") TopicExchange userExchange) {

        return BindingBuilder.bind(persistenceServiceUserUpdatedQueue).to(userExchange).with("user.updated");
    }


    @Bean("persistenceServiceUserDeletedQueue")
    public Queue persistenceServiceUserDeletedQueue() {

        return QueueBuilder.durable(PERSISTENCE_SERVICE_USER_DELETED_QUEUE)
                .withArgument("x-dead-letter-exchange", "")
                .withArgument("x-dead-letter-routing-key", PERSISTENCE_SERVICE_USER_EVENTS_DQL)
                .build();
    }


    @Bean
    public Binding userDeletedBinding(@Qualifier("persistenceServiceUserDeletedQueue") Queue persistenceServiceUserDeletedQueue,
                                      @Qualifier("userExchange") TopicExchange userExchange) {

        return BindingBuilder.bind(persistenceServiceUserDeletedQueue).to(userExchange).with("user.deleted");
    }


    @Bean("persistenceServiceUserOwnProfileUpdatedQueue")
    public Queue persistenceServiceUserOwnProfileUpdatedQueue() {

        return QueueBuilder.durable(PERSISTENCE_SERVICE_USER_OWN_PROFILE_UPDATED_QUEUE)
                .withArgument("x-dead-letter-exchange", "")
                .withArgument("x-dead-letter-routing-key", PERSISTENCE_SERVICE_USER_EVENTS_DQL)
                .build();
    }


    @Bean
    public Binding userProfileUpdatedBinding(@Qualifier("persistenceServiceUserOwnProfileUpdatedQueue") Queue persistenceServiceUserOwnProfileUpdatedQueue,
                                             @Qualifier("userExchange") TopicExchange userExchange) {

        return BindingBuilder.bind(persistenceServiceUserOwnProfileUpdatedQueue).to(userExchange).with("user.ownProfileUpdated");
    }


    @Bean("persistenceServiceUserCreatedQueue")
    public Queue persistenceServiceUserCreatedQueue() {

        return QueueBuilder.durable(PERSISTENCE_SERVICE_USER_CREATED_QUEUE)
                .withArgument("x-dead-letter-exchange", "")
                .withArgument("x-dead-letter-routing-key", PERSISTENCE_SERVICE_USER_EVENTS_DQL)
                .build();
    }


    @Bean
    public Binding userCreatedBinding(@Qualifier("persistenceServiceUserCreatedQueue") Queue persistenceServiceUserCreatedQueue,
                                      @Qualifier("userExchange") TopicExchange userExchange) {

        return BindingBuilder.bind(persistenceServiceUserCreatedQueue).to(userExchange).with("user.created");
    }


    @Bean
    public Queue persistenceServiceUserEventsDLQ() {

        return QueueBuilder.durable(PERSISTENCE_SERVICE_USER_EVENTS_DQL).build();
    }


    @Bean(name = "userExchange")
    TopicExchange tenantExchange(@Value("${fforesight.user-exchange.name}") String userExchange) {

        return new TopicExchange(userExchange);
    }

}
