"""Original deterministic tabular MDP; standard-library mechanics, not a maze benchmark."""
import json
import math
import platform
import random


NONTERMINAL_STATES = (0, 1, 2)
ACTIONS = (0, 1)  # L=0, R=1
GOAL, TRAP = 3, 4
GAMMA, ALPHA, EPSILON = 0.9, 0.2, 0.2
SEED, EPISODES, MAX_STEPS = 7, 2000, 20


def argmax_low_id(row):
    return max(range(len(row)), key=lambda action: row[action])


def environment_step(state, action):
    """Only the environment/evaluator uses this complete transition model."""
    transitions = {
        (0, 0): (TRAP, -1.0, True),
        (0, 1): (1, -0.1, False),
        (1, 0): (0, -0.1, False),
        (1, 1): (2, -0.1, False),
        (2, 0): (1, -0.1, False),
        (2, 1): (GOAL, 1.0, True),
    }
    if (state, action) not in transitions:
        raise ValueError("Step accepts only nonterminal states0..2 and actions0..1")
    return transitions[state, action]


class QLearner:
    def __init__(self, seed):
        self.q = [[0.0, 0.0] for _ in NONTERMINAL_STATES]
        self.visits = [[0, 0] for _ in NONTERMINAL_STATES]
        self.rng = random.Random(seed)

    def choose(self, state):
        if self.rng.random() < EPSILON:
            # Random branch includes the greedy action as well.
            return self.rng.randrange(len(ACTIONS))
        return argmax_low_id(self.q[state])

    def observe(self, state, action, reward, next_state, terminated):
        # A true terminal has no continuation. Time-limit truncation is NOT terminal.
        bootstrap = 0.0 if terminated else max(self.q[next_state])
        target = reward + GAMMA * bootstrap
        before = self.q[state][action]
        td_error = target - before
        after = before + ALPHA * td_error
        self.q[state][action] = after
        self.visits[state][action] += 1
        return {"state": state, "action": action, "reward": reward,
            "next_state": next_state, "terminated": terminated,
            "old_q": before, "bootstrap_max_q": bootstrap,
            "target": target, "td_error": td_error, "new_q": after}


def model_oracle():
    """Synchronous Bellman iteration using the known model, separate from the learner."""
    q = [[0.0, 0.0] for _ in NONTERMINAL_STATES]
    for sweep in range(1, 1001):
        updated = []
        for state in NONTERMINAL_STATES:
            row = []
            for action in ACTIONS:
                next_state, reward, terminated = environment_step(state, action)
                row.append(reward if terminated else reward + GAMMA * max(q[next_state]))
            updated.append(row)
        change = max(abs(updated[s][a] - q[s][a]) for s in NONTERMINAL_STATES for a in ACTIONS)
        q = updated
        if change < 1e-14:
            return q, sweep
    raise RuntimeError("Oracle iteration did not stabilize")


def evaluate_greedy(q):
    state = 0
    trajectory = []
    for time_index in range(MAX_STEPS):
        action = argmax_low_id(q[state])
        next_state, reward, terminated = environment_step(state, action)
        trajectory.append({"state": state, "action": action, "reward": reward,
                           "next_state": next_state, "terminated": terminated})
        state = next_state
        if terminated:
            break
    return {"trajectory": trajectory,
        "undiscounted_return": math.fsum(x["reward"] for x in trajectory),
        "discounted_return": math.fsum(GAMMA**t * x["reward"] for t, x in enumerate(trajectory)),
        "terminated": trajectory[-1]["terminated"],
        "truncated": not trajectory[-1]["terminated"] and len(trajectory) == MAX_STEPS}


def main():
    learner = QLearner(SEED)
    endings = {"goal": 0, "trap": 0, "truncated": 0}
    terminal_samples = {"goal": [], "trap": []}
    truncation_samples = []
    first_episode_transitions = []
    episode_lengths = []
    episode_returns = []
    for episode in range(EPISODES):
        state = 0
        rewards = []
        for step in range(1, MAX_STEPS + 1):
            action = learner.choose(state)
            next_state, reward, terminated = environment_step(state, action)
            # This flag controls the collector/reset only, not the Bellman target.
            truncated = step == MAX_STEPS and not terminated
            update = learner.observe(state, action, reward, next_state, terminated)
            rewards.append(reward)
            record = {"episode_0_based": episode, "step_1_based": step,
                      "truncated": truncated, **update}
            if episode == 0:
                first_episode_transitions.append(record)
            if terminated:
                kind = "goal" if next_state == GOAL else "trap"
                endings[kind] += 1
                if len(terminal_samples[kind]) < 3:
                    terminal_samples[kind].append(record)
                break
            if truncated:
                endings["truncated"] += 1
                if len(truncation_samples) < 3:
                    truncation_samples.append(record)
                break
            state = next_state
        episode_lengths.append(len(rewards))
        episode_returns.append(math.fsum(rewards))
    oracle_q, sweeps = model_oracle()
    max_error = max(abs(learner.q[s][a] - oracle_q[s][a])
                    for s in NONTERMINAL_STATES for a in ACTIONS)
    greedy_evaluation = evaluate_greedy(learner.q)
    # A controlled nonzero continuation example, NOT an extra training episode.
    check = QLearner(0)
    check.q = [row[:] for row in oracle_q]
    truncation_check = check.observe(0, 1, -0.1, 1, False)
    next_q = oracle_q[1]
    greedy_action = argmax_low_id(next_q)
    behavior_probabilities = [EPSILON / 2 + (1 - EPSILON if a == greedy_action else 0)
                              for a in ACTIONS]
    comparison = {"transition": {"state": 0, "action": 1, "reward": -0.1, "next_state": 1},
        "next_state_q_values": next_q,
        "epsilon_behavior_probabilities_L_R": behavior_probabilities,
        "q_learning_target_using_max": -0.1 + GAMMA * max(next_q),
        "sarsa_target_if_next_behavior_action_is_L": -0.1 + GAMMA * next_q[0],
        "expected_behavior_target_for_comparison_only": -0.1 + GAMMA * math.fsum(
            p * q for p, q in zip(behavior_probabilities, next_q))}
    assert sum(endings.values()) == EPISODES
    assert sum(map(sum, learner.visits)) == sum(episode_lengths)
    assert all(n > 0 for row in learner.visits for n in row)
    assert max_error < 1e-10
    assert [x["action"] for x in greedy_evaluation["trajectory"]] == [1, 1, 1]
    assert math.isclose(greedy_evaluation["undiscounted_return"], .8)
    assert math.isclose(greedy_evaluation["discounted_return"], .62)
    print(json.dumps({
        "scope": "Original small deterministic tabular MDP; actual2000trainingepisodes, "
                 "not a maze reproduction, policy benchmark, or finite-run convergence theorem.",
        "versions": {"python": platform.python_version(), "dependencies": "standard library only"},
        "settings": {"nonterminal_states": NONTERMINAL_STATES, "goal": GOAL, "trap": TRAP,
            "actions": {"L": 0, "R": 1}, "gamma": GAMMA, "alpha": ALPHA, "epsilon": EPSILON,
            "seed": SEED, "episodes": EPISODES, "collector_max_steps": MAX_STEPS,
            "start_state_every_episode": 0, "initial_q": "all six entries zero",
            "tie_break": "lowest action ID", "epsilon_branch": "uniform over both actions",
            "update": "Q(s,a) += alpha*(r + gamma*maxQ(next) - Q(s,a)); no continuation for true terminals",
            "truncation": "time limit resets collector only; bootstrap from observed next state retained"},
        "model_for_reader_and_oracle_only": [
            {"state": s, "action": a, "next_state": environment_step(s, a)[0],
             "reward": environment_step(s, a)[1], "terminated": environment_step(s, a)[2]}
            for s in NONTERMINAL_STATES for a in ACTIONS],
        "training": {"episode_endings": endings, "total_observed_transitions": sum(episode_lengths),
            "state_action_visit_counts_L_R": learner.visits,
            "mean_episode_length": math.fsum(episode_lengths) / EPISODES,
            "mean_undiscounted_episode_return": math.fsum(episode_returns) / EPISODES,
            "first_episode_transitions": first_episode_transitions,
            "first_terminal_updates": terminal_samples,
            "first_observed_truncation_updates": truncation_samples},
        "learned_q_L_R": learner.q,
        "model_oracle": {"q_L_R": oracle_q, "synchronous_sweeps": sweeps,
            "optimal_v": [max(row) for row in oracle_q], "max_learned_abs_error": max_error,
            "role": "Known transition model only used AFTER training for evaluation, never learner update/choice"},
        "greedy_evaluation_no_learning_no_exploration": greedy_evaluation,
        "controlled_truncation_example_not_training": {
            "description": "At time limit after 0R->1, next state1 is not terminal; continue bootstrapping",
            "correct_update": truncation_check,
            "wrong_target_if_truncation_mistaken_for_terminal": -0.1},
        "behavior_vs_target_arithmetic_not_sarsa_training": comparison,
        "limits": ["Learner receives only observed state/action/reward/nextstate/terminated tuples, not the model.",
            "Fixed2000episodes and alpha.2 do not satisfy the decreasing-step-size convergence theorem in general.",
            "All six pairs were visited in this run; finite positive visits are not infinite exploration.",
            "Goal/trap terminal value is zero; terminal Q rows are not stored or read.",
            "Collector truncation is exogenous, not a finite-horizon MDP terminal or a learned reset transition.",
            "If time remaining changed the actual task objective, it would need to be included in the state.",
            "Greedy evaluation freezes Q and removes epsilon exploration; it differs from training behavior.",
            "An epsilon random branch may choose the greedy action; action probabilities are .9/.1 with unique best.",
            "Oracle uses known deterministic transitions; it is a separate model-based reference, not model-free training.",
            "Single seed, hand-designed rewards, no heldout tuning, stochastic environment, function approximation or real-world lift."]}, indent=2))


if __name__ == "__main__":
    main()
