import time
import random
from typing import Any, Callable
from src import ai_client

class UserSimAgent:
 def __init__(self, hook_client: Any, model: str = "gemini-2.5-flash-lite", enable_delays: bool = True, batch_typing: bool = False) -> None:
  self.hook_client = hook_client
  self.model = model
  self.enable_delays = enable_delays
  self.batch_typing = batch_typing
  self.system_prompt = (
   "You are a software engineer testing an AI coding assistant called 'Manual Slop'. "
   "You want to build a small Python project and verify the assistant's capabilities. "
   "Keep your responses concise and human-like. "
   "Do not use markdown blocks for your main message unless you are providing code."
  )

 def calculate_reading_delay(self, text: str) -> float:
  word_count = len(text.split())
  delay = (word_count / 200.0) * 60.0 * 0.5
  return max(0.5, min(5.0, delay))

 def wait_to_read(self, text: str) -> None:
  """
    [C: simulation/workflow_sim.py:WorkflowSimulator.wait_for_ai_response]
  """
  if self.enable_delays:
   delay = self.calculate_reading_delay(text)
   time.sleep(delay)

 def wait_to_think(self, probability: float = 0.2, min_delay: float = 2.0, max_delay: float = 5.0) -> None:
  """
    [C: simulation/workflow_sim.py:WorkflowSimulator.wait_for_ai_response]
  """
  if self.enable_delays and random.random() < probability:
   delay = random.uniform(min_delay, max_delay)
   time.sleep(delay)

 def simulate_typing(self, text: str, jitter_range: tuple[float, float] = (0.01, 0.05), batch_typing: bool = False) -> None:
  """
    [C: simulation/workflow_sim.py:WorkflowSimulator.run_discussion_turn_async]
  """
  if not self.enable_delays:
   return
  if batch_typing or self.batch_typing:
   time.sleep(0.01)
   return
  # Simulate typing by sleeping after chunks or characters to balance speed and realism
  if len(text) > 200:
   for i in range(0, len(text), 10):
    time.sleep(random.uniform(jitter_range[0] * 3, jitter_range[1] * 3))
  elif len(text) > 50:
   for i in range(0, len(text), 3):
    time.sleep(random.uniform(jitter_range[0] * 1.5, jitter_range[1] * 1.5))
  else:
   for char in text:
    time.sleep(random.uniform(jitter_range[0], jitter_range[1]))

 def generate_response(self, conversation_history: list[dict]) -> str:
  """
  
    
      Generates a human-like response based on the conversation history.
      conversation_history: list of dicts with 'role' and 'content'
    [C: simulation/workflow_sim.py:WorkflowSimulator.run_discussion_turn_async]
  """
  last_ai_msg = ""
  for entry in reversed(conversation_history):
   if entry.get('role') == 'AI':
    last_ai_msg = entry.get('content', '')
    break
  try:
   ai_client.set_custom_system_prompt(self.system_prompt)
   response = ai_client.send(md_content="", user_message=last_ai_msg)
  finally:
   ai_client.set_custom_system_prompt("")
  return response

 def perform_action_with_delay(self, action_func: Callable, *args: Any, **kwargs: Any) -> Any:
  """
  
    
      Executes an action with a human-like delay if enabled.
    [C: tests/test_user_agent.py:test_perform_action_with_delay]
  """
  if self.enable_delays:
   delay = random.uniform(0.5, 2.0)
   time.sleep(delay)
  return action_func(*args, **kwargs)