# Copyright 2025 Softwell S.r.l.
# Licensed under the Apache License, Version 2.0

"""Tests for OutputFormatter."""

from __future__ import annotations

from genro_cli.formatter import OutputFormatter


class TestOutputFormatter:

    def setup_method(self) -> None:
        self.fmt = OutputFormatter()

    def test_format_json_dict(self) -> None:
        result = self.fmt.format_json({"name": "test", "value": 42})
        assert '"name": "test"' in result
        assert '"value": 42' in result

    def test_format_json_string_passthrough(self) -> None:
        assert self.fmt.format_json("hello") == "hello"

    def test_format_raw_dict(self) -> None:
        result = self.fmt.format_raw({"a": 1})
        assert "a" in result

    def test_format_raw_string(self) -> None:
        assert self.fmt.format_raw("hello") == "hello"

    def test_format_table_dict(self) -> None:
        result = self.fmt.format_table({"name": "Alice", "role": "admin"})
        assert "name" in result
        assert "Alice" in result

    def test_format_table_list_of_dicts(self) -> None:
        data = [
            {"id": 1, "name": "Alice"},
            {"id": 2, "name": "Bob"},
        ]
        result = self.fmt.format_table(data)
        assert "id" in result
        assert "Alice" in result
        assert "Bob" in result

    def test_format_table_empty_dict(self) -> None:
        assert self.fmt.format_table({}) == "(empty)"

    def test_format_table_fallback_to_json(self) -> None:
        result = self.fmt.format_table("not a dict or list")
        assert result == "not a dict or list"

    def test_format_response_success(self) -> None:
        response = {"status": 200, "data": {"ok": True}}
        result = self.fmt.format_response(response)
        assert '"ok": true' in result

    def test_format_response_error(self) -> None:
        response = {"status": 404, "data": {"error": "Not found"}}
        result = self.fmt.format_response(response)
        assert "Error 404" in result
        assert "Not found" in result

    def test_format_error_string(self) -> None:
        result = self.fmt.format_error(500, "Internal error")
        assert "Error 500" in result
        assert "Internal error" in result

    def test_format_mode_dispatch(self) -> None:
        data = {"key": "val"}
        assert '"key"' in self.fmt.format(data, "json")
        assert "key" in self.fmt.format(data, "table")
        assert "key" in self.fmt.format(data, "raw")
