"""Tests for kill and death tracking in combat."""

import time

import pytest

from mudlib.combat.engine import start_encounter
from mudlib.commands.snapneck import cmd_snap_neck
from mudlib.entity import Mob
from mudlib.player import accumulate_play_time


@pytest.mark.asyncio
async def test_player_finishes_mob_increments_stats(player, test_zone):
    """Snap-neck kill increments kills and mob_kills."""
    # Create a goblin mob
    goblin = Mob(name="goblin", x=0, y=0)
    goblin.location = test_zone
    test_zone._contents.append(goblin)

    # Start encounter and make target unconscious
    start_encounter(player, goblin)
    player.mode_stack.append("combat")
    goblin.pl = 0.0
    await cmd_snap_neck(player, "goblin")

    # Verify stats
    assert player.kills == 1
    assert player.mob_kills["goblin"] == 1


@pytest.mark.asyncio
async def test_player_finished_by_mob_increments_deaths(player, nearby_player):
    """Snap-neck finisher from opponent increments deaths."""
    start_encounter(nearby_player, player)
    nearby_player.mode_stack.append("combat")
    player.mode_stack.append("combat")
    player.pl = 0.0
    await cmd_snap_neck(nearby_player, "Goku")

    # Verify deaths incremented
    assert player.deaths == 1


@pytest.mark.asyncio
async def test_multiple_finisher_kills_accumulate(player, test_zone):
    """After 3 finishers, kill counters accumulate correctly."""
    for _ in range(3):
        # Create goblin
        goblin = Mob(name="goblin", x=0, y=0)
        goblin.location = test_zone
        test_zone._contents.append(goblin)

        # Create encounter and finish
        start_encounter(player, goblin)
        player.mode_stack.append("combat")
        goblin.pl = 0.0
        await cmd_snap_neck(player, "goblin")

    # Verify accumulated kills
    assert player.kills == 3
    assert player.mob_kills["goblin"] == 3


def test_session_time_tracking(player):
    """Session time tracking accumulates correctly."""
    # Set session start to a known time
    start_time = time.monotonic()
    player.session_start = start_time

    # Simulate 5 seconds passing
    time.sleep(0.01)  # Small real delay to ensure monotonic() advances
    player.session_start = start_time  # Reset for predictable test

    # Mock time to be 5 seconds later
    import unittest.mock

    with unittest.mock.patch("time.monotonic", return_value=start_time + 5.0):
        accumulate_play_time(player)

    # Should have accumulated 5 seconds
    assert player.play_time_seconds == 5.0

    # Session start should be reset to current time
    with unittest.mock.patch("time.monotonic", return_value=start_time + 5.0):
        assert player.session_start == start_time + 5.0


def test_accumulate_play_time_multiple_sessions(player):
    """Multiple accumulation calls should add up correctly."""
    start_time = time.monotonic()
    player.session_start = start_time

    import unittest.mock

    # First accumulation: 3 seconds
    with unittest.mock.patch("time.monotonic", return_value=start_time + 3.0):
        accumulate_play_time(player)

    assert player.play_time_seconds == 3.0

    # Second accumulation: 2 more seconds (from reset point)
    with unittest.mock.patch("time.monotonic", return_value=start_time + 5.0):
        accumulate_play_time(player)

    # Should have 3 + 2 = 5 total
    assert player.play_time_seconds == 5.0
