"""Integration tests for the ExprCommand."""

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

import pebble_shell.commands
import pebble_shell.shell

if TYPE_CHECKING:
    import ops


@pytest.fixture
def command(shell: pebble_shell.shell.PebbleShell):
    """Fixture to create an ExprCommand instance."""
    yield pebble_shell.commands.ExprCommand(shell=shell)


def test_name(command: pebble_shell.commands.ExprCommand):
    assert command.name == "expr"


def test_category(command: pebble_shell.commands.ExprCommand):
    assert command.category == "Mathematical Utilities"


def test_help(command: pebble_shell.commands.ExprCommand):
    with command.shell.console.capture() as capture:
        command.show_help()
    assert "Evaluate expressions" in capture.get()


@pytest.mark.parametrize("args", [["-h"], ["--help"]])
def test_execute_help(
    client: ops.pebble.Client,
    command: pebble_shell.commands.ExprCommand,
    args: list[str],
):
    with command.shell.console.capture() as capture:
        result = command.execute(client=client, args=args)
    assert result == 0
    assert "expressions" in capture.get()


def test_execute_no_args(
    client: ops.pebble.Client,
    command: pebble_shell.commands.ExprCommand,
):
    with command.shell.console.capture() as _:
        result = command.execute(client=client, args=[])
    assert result == 1
    # Should show usage information


def test_execute_simple_math(
    client: ops.pebble.Client,
    command: pebble_shell.commands.ExprCommand,
):
    with command.shell.console.capture() as _:
        result = command.execute(client=client, args=["2", "+", "3"])
    # Should calculate 2 + 3 = 5 successfully
    assert result == 0


def test_execute_string_comparison(
    client: ops.pebble.Client,
    command: pebble_shell.commands.ExprCommand,
):
    with command.shell.console.capture() as _:
        result = command.execute(client=client, args=["hello", "=", "hello"])
    # Should evaluate string equality successfully
    assert result == 0
