package com.konkerlabs.platform.registry.test.base;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.konkerlabs.platform.registry.business.model.enumerations.DateFormat;
import com.konkerlabs.platform.registry.business.model.enumerations.Language;
import com.konkerlabs.platform.registry.business.model.enumerations.TimeZone;
import com.konkerlabs.platform.registry.config.WebMvcConfig;
import com.konkerlabs.platform.registry.web.converters.utils.ConverterUtils;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.konkerlabs.platform.registry.business.model.Device;
import com.konkerlabs.platform.registry.business.model.Tenant;
import com.konkerlabs.platform.registry.business.model.User;
import com.konkerlabs.platform.registry.business.repositories.DeviceRepository;
import com.konkerlabs.platform.registry.business.repositories.UserRepository;
import com.konkerlabs.platform.registry.security.DeviceUserDetailsService;
import com.konkerlabs.platform.registry.security.TenantUserDetailsService;
import com.konkerlabs.platform.registry.security.UserContextResolver;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

@Configuration
public class WebTestConfiguration {

    @Bean
    public Tenant tenant() {
        return Tenant.builder().name("konker").domainName("konker").id("id").build();
    }

    @Bean
    public User user() {
        return User.builder()
                .email("user@domain.com")
                .zoneId(TimeZone.AMERICA_SAO_PAULO)
                .language(Language.EN)
                .avatar("default.jpg")
                .dateFormat(DateFormat.YYYYMMDD)
                .tenant(tenant()).build();
    }

	@Bean
	public UserContextResolver userContextResolver() {
		return mock(UserContextResolver.class);
	}
	
	@Bean
	public TenantUserDetailsService userDetailsService(){
    	TenantUserDetailsService tenantDetailService = mock(TenantUserDetailsService.class);

    	when(tenantDetailService.loadUserByUsername(Mockito.anyString()))
    		.thenReturn(User.builder().email("admin").password("admin123").build());

		return tenantDetailService;
	}

    @Bean
    public DeviceUserDetailsService deviceUserDetailsService(){
    	DeviceUserDetailsService instance = Mockito.mock(DeviceUserDetailsService.class);

    	Mockito.when(instance.loadUserByUsername(Mockito.anyString()))
    		.thenReturn(Device.builder().deviceId("123").build());

    	return instance;
    }

    @Bean
    public DeviceRepository deviceRepository(){
    	return Mockito.mock(DeviceRepository.class);
    }

    @Bean
    public UserRepository userRepository(){
    	return Mockito.mock(UserRepository.class);
    }

    @Bean
    public LocaleResolver localeResolver(){
        return new SessionLocaleResolver();
    }

    @Bean
    public ConverterUtils converterUtils(){
        ConverterUtils converterUtils = Mockito.mock(ConverterUtils.class);
        when(converterUtils.getUserZoneID()).thenReturn("America/Sao_Paulo");
        when(converterUtils.getCurrentLocale()).thenReturn(
                new Locale("pt", "BR")
        );
        when(converterUtils.getDateTimeFormatPattern())
                .thenReturn("dd/MM/yyyy HH:mm:ss.SSS zzz");
        return converterUtils;
    }

}

@@ -367,69 +367,8 @@
 ne;%0A
-import com.konkerlabs.platform.registry.config.WebMvcConfig;%0A
 impo
@@ -444,33 +444,8 @@
 ls;%0A
-import org.mockito.Mock;%0A
 impo
@@ -919,32 +919,113 @@
 UserRepository;%0A
+import com.konkerlabs.platform.registry.business.services.api.LoginAuditService;%0A
 import com.konke
@@ -2005,10 +2005,9 @@
 %0A%09%7D%0A
-%09
 %0A
+
 %09@Be
@@ -2336,20 +2336,199 @@
 ervice;%0A
-
 %09%7D%0A%0A
+    @Bean%0A    public LoginAuditService loginAuditService()%7B%0A        LoginAuditService loginAuditService = mock(LoginAuditService.class);%0A%0A        return loginAuditService;%0A    %7D%0A%0A
     @Bea

fix tests: add LoginAuditService mock