import torch


def ddm(
    starting_value: torch.Tensor,
    drift_rate: torch.Tensor,
    non_decision_time: torch.Tensor,
    threshold: torch.Tensor,
    noise: torch.Tensor,
    time_step_size: torch.Tensor,
):
    """
    A model that simulates a simple noisy drift diffusion model using Euler-Maruyama integration. This is implemented
    without performance in mind.

    Args:
        starting_value: The starting value of the particle.
        drift_rate: The constant drift rate the particles is under.
        non_decision_time: A constant amount of time added to the reaction time that signifies automatic processing.
        threshold: The threshold that a particle must reach to stop integration.
        noise: The standard deviation of the Gaussian noise added to the particles position at each time step.
        time_step_size: The time step size (in seconds) for the integration process.

    Returns:
       A two element tuple containing the reaction times and the decisions
    """

    x = starting_value
    rt = torch.tensor(0.0)

    # Integrate until the negative or positive threshold is reached
    while torch.abs(x) < threshold:
        x = x + torch.normal(mean=drift_rate * time_step_size, std=noise) * torch.sqrt(
            time_step_size
        )
        rt = rt + 1.0

    # Compute the actual reaction time and the decision (as a bool)
    rt = non_decision_time + rt * time_step_size
    decision = x >= threshold

    return rt, decision