package com.zsc.edu.gateway;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zsc.edu.gateway.domain.system.DeptBuilder;
import com.zsc.edu.gateway.domain.system.RoleBuilder;
import com.zsc.edu.gateway.domain.system.UserBuilder;
import com.zsc.edu.gateway.framework.security.CustomAccessDeniedHandler;
import com.zsc.edu.gateway.framework.security.CustomAuthenticationFailureHandler;
import com.zsc.edu.gateway.framework.security.UserDetailsImpl;
import com.zsc.edu.gateway.modules.system.entity.Dept;
import com.zsc.edu.gateway.modules.system.entity.Role;
import com.zsc.edu.gateway.modules.system.entity.User;
import com.zsc.edu.gateway.modules.system.mapper.RoleMapper;
import com.zsc.edu.gateway.modules.system.mapper.UserMapper;
import com.zsc.edu.gateway.modules.system.service.RoleService;
import com.zsc.edu.gateway.modules.system.service.UserService;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;

import javax.sql.DataSource;
import java.util.HashSet;
import java.util.Set;

/**
 * @author pengzheng
 */
@ExtendWith(SpringExtension.class)
//@AutoConfigureRestDocs
//@ActiveProfiles("test")
abstract public class MockMvcConfigBase {

    protected static UserDetailsImpl userDetails;
    protected static User user;
    @MockBean
    protected DataSource dataSource;
    @MockBean
    protected SessionRegistry sessionRegistry;
    @MockBean
    protected UserMapper userMapper;
    @MockBean
    protected RoleMapper roleMapper;

    @MockBean
    private UserService userService;
    @MockBean
    protected CustomAuthenticationFailureHandler customAuthenticationFailureHandler;
    @MockBean
    protected CustomAccessDeniedHandler customAccessDeniedHandler;
    @Resource
    protected MockMvc mockMvc;
    @Resource
    protected ObjectMapper objectMapper;

    @BeforeAll
    public static void setup() {
        Dept dept = DeptBuilder.aDept().name("Platform").build();
        Role role = RoleBuilder.aRole().authorities(new HashSet<>()).build();
//        Role role = RoleBuilder.aRole().authorities(new HashSet<>(Arrays.asList(Authority))).build();
        user = UserBuilder.anUser().username("admin").dept(dept).role(role).build();
        userDetails = UserDetailsImpl.from(user, Set.of());
    }
}
