use toast_api::deepseek::{DeepSeekPOW, PowChallenge};
use serde_json::json;
use base64::{engine::general_purpose::STANDARD, Engine as _};

#[test]
fn test_deepseek_pow_creation() {
    // Test that we can create a DeepSeekPOW instance
    let result = DeepSeekPOW::new();
    assert!(result.is_ok(), "Failed to create DeepSeekPOW: {:?}", result.err());
}

#[test] 
fn test_deepseek_pow_solve_challenge() {
    // Create a test challenge with realistic parameters
    let challenge_json = json!({
        "algorithm": "sha3_256",
        "challenge": "test_challenge_12345",
        "salt": "test_salt",
        "difficulty": 1.0,
        "expire_at": 1735689600_u64,
        "signature": "test_signature",
        "target_path": "/api/v0/chat/completion"
    });
    
    let challenge: PowChallenge = serde_json::from_value(challenge_json).unwrap();
    let pow_solver = DeepSeekPOW::new().unwrap();
    
    // Test that solve_challenge returns a result (should work with difficulty 1.0)
    let result = pow_solver.solve_challenge(&challenge);
    assert!(result.is_ok(), "Failed to solve challenge: {:?}", result.err());
    
    let solution = result.unwrap();
    assert!(!solution.is_empty(), "Solution should not be empty");
    
    // Verify the solution is valid base64
    let decoded = STANDARD.decode(&solution);
    assert!(decoded.is_ok(), "Solution should be valid base64");
    
    // Verify the decoded solution is valid JSON
    let decoded_bytes = decoded.unwrap();
    let decoded_str = String::from_utf8(decoded_bytes).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&decoded_str).unwrap();
    
    // Verify the structure contains required fields with correct values
    assert_eq!(parsed["algorithm"], "sha3_256");
    assert_eq!(parsed["challenge"], "test_challenge_12345");
    assert_eq!(parsed["salt"], "test_salt");
    assert!(parsed["answer"].is_number());
    assert_eq!(parsed["signature"], "test_signature");
    assert_eq!(parsed["target_path"], "/api/v0/chat/completion");
}
