package com.mosquito.project;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.ResultSet;
import java.sql.SQLException;

import static org.junit.jupiter.api.Assertions.assertTrue;

@SpringBootTest
@org.springframework.context.annotation.Import({
    com.mosquito.project.config.TestCacheConfig.class
})
@org.springframework.test.context.TestPropertySource(properties = {
    "spring.cache.type=NONE",
    "spring.flyway.enabled=false",
    "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration"
})
class SchemaVerificationTest {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Test
    void activitiesTableExists() throws SQLException {
        boolean tableExists = jdbcTemplate.query("SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'ACTIVITIES'", (ResultSet rs) -> {
            return rs.next();
        });

        assertTrue(tableExists, "Table 'activities' should exist in the database schema.");
    }

    @Test
    void activityRewardsTableExists() throws SQLException {
        boolean tableExists = jdbcTemplate.query("SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'ACTIVITY_REWARDS'", (ResultSet rs) -> {
            return rs.next();
        });

        assertTrue(tableExists, "Table 'activity_rewards' should exist in the database schema.");
    }

    @Test
    void multiLevelRewardRulesTableExists() throws SQLException {
        boolean tableExists = jdbcTemplate.query("SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'MULTI_LEVEL_REWARD_RULES'", (ResultSet rs) -> {
            return rs.next();
        });

        assertTrue(tableExists, "Table 'multi_level_reward_rules' should exist in the database schema.");
    }

    @Test
    void apiKeysTableExists() throws SQLException {
        boolean tableExists = jdbcTemplate.query("SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'API_KEYS'", (ResultSet rs) -> {
            return rs.next();
        });

        assertTrue(tableExists, "Table 'api_keys' should exist in the database schema.");
    }

    @Test
    void dailyActivityStatsTableExists() throws SQLException {
        boolean tableExists = jdbcTemplate.query("SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'DAILY_ACTIVITY_STATS'", (ResultSet rs) -> {
            return rs.next();
        });

        assertTrue(tableExists, "Table 'daily_activity_stats' should exist in the database schema.");
    }

    @Test
    void processedCallbacksTableExists() throws SQLException {
        boolean tableExists = jdbcTemplate.query("SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'PROCESSED_CALLBACKS'", (ResultSet rs) -> {
            return rs.next();
        });

        assertTrue(tableExists, "Table 'processed_callbacks' should exist in the database schema.");
    }

}
