package minecraft_mod_player_statistics.src.test.java;

import minecraft_mod_player_statistics.src.main.java.DatabaseManager;
import org.junit.jupiter.api.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

public class DatabaseManagerIntegrationTest {
    private static DatabaseManager dbManager;
    private static final UUID TEST_PLAYER_UUID = UUID.randomUUID();
    private static final String TEST_STAT_TYPE = "integration_test_stat";
    private static final String TEST_BLOCK_ID = "minecraft:test_block";
    private static final int TEST_VALUE = 42;

    @BeforeAll
    static void setUp() {
        dbManager = new DatabaseManager();
        // Initialize database schema
        dbManager.initializeDatabase();
    }

    @BeforeEach
    void cleanupTestData() throws SQLException {
        // Clean up any test data before each test
        try (Connection conn = dbManager.getConnection();
             PreparedStatement stmt = conn.prepareStatement(
                 "DELETE FROM player_stats WHERE stat_type = ?")) {
            stmt.setString(1, TEST_STAT_TYPE);
            stmt.executeUpdate();
        }
    }

    @Test
    void testDatabaseConnection() throws SQLException {
        // Test basic connection
        try (Connection conn = dbManager.getConnection()) {
            Assertions.assertTrue(conn.isValid(5));
        }
    }

    @Test
    void testRecordAndRetrieveStat() throws SQLException {
        // Record a test statistic
        dbManager.recordStat(TEST_PLAYER_UUID, TEST_STAT_TYPE, TEST_VALUE, TEST_BLOCK_ID);

        // Verify the recorded data
        try (Connection conn = dbManager.getConnection();
             PreparedStatement stmt = conn.prepareStatement(
                 "SELECT * FROM player_stats WHERE player_uuid = ? AND stat_type = ?")) {
            
            stmt.setString(1, TEST_PLAYER_UUID.toString());
            stmt.setString(2, TEST_STAT_TYPE);
            
            ResultSet rs = stmt.executeQuery();
            
            Assertions.assertTrue(rs.next(), "Stat record should exist");
            Assertions.assertEquals(TEST_PLAYER_UUID.toString(), rs.getString("player_uuid"));
            Assertions.assertEquals(TEST_STAT_TYPE, rs.getString("stat_type"));
            Assertions.assertEquals(TEST_VALUE, rs.getInt("stat_value"));
            Assertions.assertEquals(TEST_BLOCK_ID, rs.getString("item_id"));
            Assertions.assertNotNull(rs.getTimestamp("timestamp"));
            
            Assertions.assertFalse(rs.next(), "Should only have one matching record");
        }
    }

    @AfterEach
    void tearDownEach() throws SQLException {
        // Clean up test data after each test
        try (Connection conn = dbManager.getConnection();
             PreparedStatement stmt = conn.prepareStatement(
                 "DELETE FROM player_stats WHERE stat_type = ?")) {
            stmt.setString(1, TEST_STAT_TYPE);
            stmt.executeUpdate();
        }
    }

    @AfterAll
    static void tearDown() throws SQLException {
        // Perform any final cleanup if needed
        try (Connection conn = dbManager.getConnection();
             PreparedStatement stmt = conn.prepareStatement(
                 "DELETE FROM player_stats WHERE stat_type = ?")) {
            stmt.setString(1, TEST_STAT_TYPE);
            stmt.executeUpdate();
        }
    }
}