package com.mosquito.project.config;

import com.mosquito.project.persistence.entity.ApiKeyEntity;
import com.mosquito.project.persistence.repository.ActivityRepository;
import com.mosquito.project.persistence.repository.ApiKeyRepository;
import com.mosquito.project.persistence.repository.LinkClickRepository;
import com.mosquito.project.persistence.repository.UserInviteRepository;
import com.mosquito.project.security.IntrospectionResponse;
import com.mosquito.project.security.UserIntrospectionService;
import com.mosquito.project.service.*;
import com.mosquito.project.support.TestAuthSupport;
import org.mockito.Mockito;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;

import java.util.Optional;

@TestConfiguration
public class ControllerTestConfig {

    @Bean
    @Primary
    public ActivityService activityService() {
        return Mockito.mock(ActivityService.class);
    }

    @Bean
    @Primary
    public ShareTrackingService shareTrackingService() {
        return Mockito.mock(ShareTrackingService.class);
    }

    @Bean
    @Primary
    public ShareConfigService shareConfigService() {
        return Mockito.mock(ShareConfigService.class);
    }

    @Bean
    @Primary
    public PosterRenderService posterRenderService() {
        return Mockito.mock(PosterRenderService.class);
    }

    @Bean
    @Primary
    public ShortLinkService shortLinkService() {
        return Mockito.mock(ShortLinkService.class);
    }

    @Bean
    @Primary
    public LinkClickRepository linkClickRepository() {
        return Mockito.mock(LinkClickRepository.class);
    }

    @Bean
    @Primary
    public ActivityRepository activityRepository() {
        return Mockito.mock(ActivityRepository.class);
    }

    @Bean
    @Primary
    public ApiKeyRepository apiKeyRepository() {
        ApiKeyRepository repository = Mockito.mock(ApiKeyRepository.class);
        ApiKeyEntity apiKeyEntity = TestAuthSupport.buildApiKeyEntity();
        Mockito.when(repository.findByKeyPrefix(TestAuthSupport.API_KEY_PREFIX))
            .thenReturn(Optional.of(apiKeyEntity));
        return repository;
    }

    @Bean
    @Primary
    public UserInviteRepository userInviteRepository() {
        return Mockito.mock(UserInviteRepository.class);
    }

    @Bean
    @Primary
    public UserIntrospectionService userIntrospectionService() {
        UserIntrospectionService service = Mockito.mock(UserIntrospectionService.class);
        IntrospectionResponse response = new IntrospectionResponse();
        response.setActive(true);
        Mockito.when(service.introspect(Mockito.anyString())).thenReturn(response);
        return service;
    }
}
