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

import org.junit.jupiter.api.BeforeEach;
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;

/**
 * 手动创建表测试
 * 
 * @author 张翔
 * @date 2026-04-03
 */
@SpringBootTest(
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
    classes = cn.novalon.gym.manage.app.ManageApplication.class
)
@ActiveProfiles("test")
class ManualTableCreationTest {

    @Autowired
    private R2dbcEntityTemplate r2dbcEntityTemplate;

    @BeforeEach
    void setUp() {
        r2dbcEntityTemplate.getDatabaseClient()
                .sql("CREATE TABLE IF NOT EXISTS operation_log (" +
                        "id BIGSERIAL PRIMARY KEY, " +
                        "username VARCHAR(50), " +
                        "operation VARCHAR(100), " +
                        "method VARCHAR(200), " +
                        "params TEXT, " +
                        "result TEXT, " +
                        "ip VARCHAR(50), " +
                        "duration BIGINT, " +
                        "status VARCHAR(1) DEFAULT '0', " +
                        "error_msg TEXT, " +
                        "create_by VARCHAR(50), " +
                        "update_by VARCHAR(50), " +
                        "created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " +
                        "updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " +
                        "deleted_at TIMESTAMP)")
                .then()
                .as(StepVerifier::create)
                .verifyComplete();
    }

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