package cn.novalon.gym.manage.app.integration;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.test.context.ActiveProfiles;
import reactor.test.StepVerifier;

import java.time.Duration;

/**
 * 数据库初始化验证测试
 * 
 * 注意：此测试需要完整的数据库初始化，暂时禁用。
 * TODO: 修复数据库初始化问题
 * 
 * @author 张翔
 * @date 2026-04-03
 */
@Disabled("暂时禁用：数据库初始化问题需要修复")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
class DatabaseInitTest {

    @Autowired
    private R2dbcEntityTemplate r2dbcEntityTemplate;

    @Test
    void testSysUserTableExists() {
        r2dbcEntityTemplate.getDatabaseClient()
                .sql("SELECT COUNT(*) FROM sys_user")
                .fetch()
                .one()
                .as(StepVerifier::create)
                .expectNextCount(1)
                .verifyComplete();
    }

    @Test
    void testOperationLogTableExists() {
        r2dbcEntityTemplate.getDatabaseClient()
                .sql("SELECT COUNT(*) FROM operation_log")
                .fetch()
                .one()
                .as(StepVerifier::create)
                .expectNextCount(1)
                .verifyComplete();
    }

    @Test
    void testAllTablesCreated() {
        r2dbcEntityTemplate.getDatabaseClient()
                .sql("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'PUBLIC'")
                .fetch()
                .all()
                .map(row -> row.get("TABLE_NAME"))
                .collectList()
                .as(StepVerifier::create)
                .assertNext(tables -> {
                    System.out.println("Created tables: " + tables);
                    assert tables.contains("SYS_USER") : "SYS_USER table not found";
                    assert tables.contains("OPERATION_LOG") : "OPERATION_LOG table not found";
                })
                .verifyComplete();
    }
}
