package minecraft_mod_player_statistics.src.test.java;

import minecraft_mod_player_statistics.src.main.java.DatabaseManager;

import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

import minecraft_mod_player_statistics.src.main.java.PlayerStatisticsMod;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.Bootstrap;
import net.minecraft.SharedConstants;
import net.minecraftforge.event.world.BlockEvent;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;

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

@ExtendWith(MockitoExtension.class)
public class DatabaseManagerTest {
    private DatabaseManager dbManager;
    
    @Mock
    private Connection connection;
    
    @Mock
    private PreparedStatement preparedStatement;

    private final UUID TEST_UUID = UUID.randomUUID();
    private final String STAT_TYPE = "block_mined";
    private final String BLOCK_ID = "minecraft:stone";

    @BeforeEach
    void setUp() throws SQLException {
        dbManager = new DatabaseManager() {
            @Override
            public Connection getConnection() throws SQLException {
                return connection;
            }
        };
        
        // Set up the mock behavior
        when(connection.prepareStatement(anyString())).thenReturn(preparedStatement);
        doNothing().when(preparedStatement).setString(anyInt(), anyString());
        doNothing().when(preparedStatement).setInt(anyInt(), anyInt());
        doNothing().when(preparedStatement).setTimestamp(anyInt(), any(Timestamp.class));
        when(preparedStatement.executeUpdate()).thenReturn(1);
    }

    @Test
    void testRecordStat() throws SQLException {
        // Arrange
        int value = 1;

        // Act
        dbManager.recordStat(TEST_UUID, STAT_TYPE, value, BLOCK_ID);

        // Verify
        verify(connection).prepareStatement(
            "INSERT INTO player_stats (player_uuid, stat_type, stat_value, item_id, timestamp) VALUES (?, ?, ?, ?, ?)"
        );
        
        verify(preparedStatement).setString(1, TEST_UUID.toString());
        verify(preparedStatement).setString(2, STAT_TYPE);
        verify(preparedStatement).setInt(3, value);
        verify(preparedStatement).setString(4, BLOCK_ID);
        verify(preparedStatement).setTimestamp(eq(5), any(Timestamp.class));
        verify(preparedStatement).executeUpdate();
    }
}