package cn.novalon.gym.manage.sys.handler.auth;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

@Component
public class PasswordDiagnosticHandler {

    private static final Logger logger = LoggerFactory.getLogger(PasswordDiagnosticHandler.class);
    private final PasswordEncoder passwordEncoder;

    public PasswordDiagnosticHandler(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
        logger.info("PasswordDiagnosticHandler initialized with encoder: {}", passwordEncoder.getClass().getName());
    }

    public Mono<ServerResponse> diagnose(ServerRequest request) {
        String testPassword = "Test@123";
        String dbHash = "$2a$12$nZ1EMUpZQljbnEdIKzH72eHlDJKUmHmHppnTTVth/SlHs5VpSAr8C";
        
        logger.info("=== Password Diagnostic Start ===");
        logger.info("Test password: {}", testPassword);
        logger.info("DB hash: {}", dbHash);
        logger.info("Encoder type: {}", passwordEncoder.getClass().getName());
        
        boolean matches = passwordEncoder.matches(testPassword, dbHash);
        
        logger.info("Match result: {}", matches);
        logger.info("=== Password Diagnostic End ===");
        
        return ServerResponse.ok()
                .bodyValue(java.util.Map.of(
                    "testPassword", testPassword,
                    "dbHash", dbHash,
                    "encoderType", passwordEncoder.getClass().getName(),
                    "matches", matches
                ));
    }
}
