package com.social.media;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
public class BCryptTest {

    @Test
    public void testPasswordMatch() {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        
        // Test with the correct password (from SQL comment)
        String correctPassword = "admin123";
        String storedHash = "$2a$10$N2l0vV/7vQPj1e8zq0lj4.QKUeJKQVhM1oGxPYq5yNh2T1sGqKQtu";
        
        System.out.println("Testing password match:");
        System.out.println("Correct password: " + correctPassword);
        System.out.println("Stored hash: " + storedHash);
        
        boolean correctMatches = encoder.matches(correctPassword, storedHash);
        System.out.println("Correct password matches: " + correctMatches);
        
        // Test with the wrong password that was being tried
        String wrongPassword = "Leader@2025!Strong#Pass";
        boolean wrongMatches = encoder.matches(wrongPassword, storedHash);
        System.out.println("Wrong password (" + wrongPassword + ") matches: " + wrongMatches);
        
        // Let's also test generating a new hash for the correct password
        String newHash = encoder.encode(correctPassword);
        System.out.println("New hash generated: " + newHash);
        
        boolean newHashMatches = encoder.matches(correctPassword, newHash);
        System.out.println("New hash matches: " + newHashMatches);
        
        // The original stored hash should match the correct password
        assertTrue(correctMatches, "The password 'admin123' should match the stored hash");
        // The newly generated hash should also match
        assertTrue(newHashMatches, "The password should match the newly generated hash");
        // The wrong password should not match
        assertFalse(wrongMatches, "The wrong password should not match the stored hash");
    }
    
    @Test
    public void testDifferentPassword() {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        
        String wrongPassword = "WrongPassword123";
        String storedHash = "$2a$10$N2l0vV/7vQPj1e8zq0lj4.QKUeJKQVhM1oGxPYq5yNh2T1sGqKQtu";
        
        boolean matches = encoder.matches(wrongPassword, storedHash);
        System.out.println("Wrong password matches: " + matches);
        
        assertFalse(matches, "Wrong password should not match the stored hash");
    }
}
