"""Tests for output formatting utilities."""

import json

import pytest

from ai_org.utils.output import OutputFormatter


@pytest.mark.unit
class TestOutputFormatter:
    def test_json_mode_default_off(self):
        fmt = OutputFormatter()
        assert fmt.json_mode is False

    def test_set_json_mode(self):
        fmt = OutputFormatter()
        fmt.set_json_mode(True)
        assert fmt.json_mode is True

    def test_info_suppressed_in_json_mode(self):
        fmt = OutputFormatter()
        fmt.set_json_mode(True)
        # Should not raise; output is suppressed
        fmt.info("test message")

    def test_success_suppressed_in_json_mode(self):
        fmt = OutputFormatter()
        fmt.set_json_mode(True)
        fmt.success("test")

    def test_warning_suppressed_in_json_mode(self):
        fmt = OutputFormatter()
        fmt.set_json_mode(True)
        fmt.warning("test")

    def test_error_suppressed_in_json_mode(self):
        fmt = OutputFormatter()
        fmt.set_json_mode(True)
        fmt.error("test")

    def test_progress_suppressed_in_json_mode(self):
        fmt = OutputFormatter()
        fmt.set_json_mode(True)
        fmt.progress("test")

    def test_text_suppressed_in_json_mode(self):
        fmt = OutputFormatter()
        fmt.set_json_mode(True)
        fmt.text("test")

    def test_json_output_prints_in_json_mode(self, capsys):
        fmt = OutputFormatter()
        fmt.set_json_mode(True)
        fmt.json_output({"key": "value"})
        captured = capsys.readouterr()
        data = json.loads(captured.out)
        assert data == {"key": "value"}

    def test_json_output_silent_when_not_json_mode(self, capsys):
        fmt = OutputFormatter()
        fmt.json_output({"key": "value"})
        captured = capsys.readouterr()
        assert captured.out == ""

    def test_table_empty_data(self):
        fmt = OutputFormatter()
        fmt.table([], ["Col1", "Col2"])
        # Should show "No data to display" via info()

    def test_table_json_mode_outputs_json(self, capsys):
        fmt = OutputFormatter()
        fmt.set_json_mode(True)
        data = [{"Name": "test", "Id": "123"}]
        fmt.table(data, ["Name", "Id"])
        captured = capsys.readouterr()
        parsed = json.loads(captured.out)
        assert parsed == data

    def test_table_renders_data(self):
        fmt = OutputFormatter()
        data = [{"Name": "test", "Id": "123"}]
        # Should not raise
        fmt.table(data, ["Name", "Id"], title="Test Table")

    def test_table_bool_conversion(self):
        fmt = OutputFormatter()
        data = [{"Active": True, "Deleted": False}]
        # In non-JSON mode, booleans convert to Yes/No
        fmt.table(data, ["Active", "Deleted"])

    def test_table_none_conversion(self):
        fmt = OutputFormatter()
        data = [{"Name": "test", "Value": None}]
        # Should render None as "-" without raising
        fmt.table(data, ["Name", "Value"])

    def test_dict_display_json_mode(self, capsys):
        fmt = OutputFormatter()
        fmt.set_json_mode(True)
        fmt.dict_display({"key": "value"}, title="Test")
        captured = capsys.readouterr()
        parsed = json.loads(captured.out)
        assert parsed == {"key": "value"}

    def test_dict_display_renders(self):
        fmt = OutputFormatter()
        # Handles nested dicts and lists without raising
        fmt.dict_display(
            {"simple": "value", "nested": {"a": 1}, "items": ["x", "y"]},
            title="Details",
        )
