package com.mosquito.project.integration;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInstance;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.mosquito.project.security.IntrospectionResponse;
import com.mosquito.project.security.UserIntrospectionService;

import java.time.Instant;
import java.util.List;

/**
 * 集成测试基类
 * 提供H2内存数据库和配置用于集成测试
 */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ContextConfiguration(initializers = AbstractIntegrationTest.Initializer.class)
abstract class AbstractIntegrationTest {

    @MockBean
    private UserIntrospectionService userIntrospectionService;

    /**
     * Spring Boot测试属性初始化器
     */
    static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
        public void initialize(ConfigurableApplicationContext applicationContext) {
            TestPropertyValues.of(
                // H2内存数据库配置
                "spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;MODE=PostgreSQL",
                "spring.datasource.username=sa",
                "spring.datasource.password=",
                "spring.datasource.driver-class-name=org.h2.Driver",
                "spring.jpa.hibernate.ddl-auto=create-drop",
                "spring.jpa.show-sql=true",
                "spring.jpa.database-platform=org.hibernate.dialect.H2Dialect",
                
                // Redis配置
                "spring.redis.host=localhost",
                "spring.redis.port=6379",
                "spring.data.redis.host=localhost",
                "spring.data.redis.port=6379",
                
                // Flyway配置
                "spring.liquibase.enabled=false",
                "spring.flyway.enabled=false",
                
                // 日志配置
                "logging.level.com.mosquito.project=DEBUG",
                "logging.level.org.springframework.jdbc=DEBUG"
            ).applyTo(applicationContext.getEnvironment());
        }
    }

    /**
     * 等待测试环境准备就绪
     */
    @BeforeAll
    static void setUp() {
        System.out.println("集成测试环境准备就绪 - 使用H2内存数据库");
    }

    @BeforeEach
    void stubIntrospection() {
        IntrospectionResponse active = new IntrospectionResponse();
        active.setActive(true);
        active.setUserId("test-user");
        active.setTenantId("test-tenant");
        active.setRoles(List.of("test"));
        active.setScopes(List.of("api"));
        long now = Instant.now().getEpochSecond();
        active.setIat(now);
        active.setExp(now + 3600);
        active.setJti("test-jti");
        Mockito.when(userIntrospectionService.introspect(Mockito.anyString())).thenReturn(active);
    }
}
