"""Tests for zimba CLI."""

import json
import sys
from unittest import mock

import pytest

from zimba.cli import main


class TestCLI:
    def test_version(self, capsys):
        with mock.patch("sys.argv", ["zimba", "version"]):
            main()
        captured = capsys.readouterr()
        assert "zimba" in captured.out
        assert "0.1.0" in captured.out

    def test_help(self, capsys):
        with mock.patch("sys.argv", ["zimba", "--help"]):
            with pytest.raises(SystemExit) as exc_info:
                main()
            assert exc_info.value.code == 0
        captured = capsys.readouterr()
        assert "email" in captured.out
        assert "calendar" in captured.out
        assert "contacts" in captured.out

    def test_unknown_command(self, capsys):
        with mock.patch("sys.argv", ["zimba", "bogus"]):
            with pytest.raises(SystemExit) as exc_info:
                main()
            assert exc_info.value.code == 1

    def test_email_search(self, capsys):
        mock_result = {"total": 1, "emails": [{"id": "1", "subject": "Test"}]}
        with mock.patch("sys.argv", ["zimba", "email", "search", "in:inbox"]):
            with mock.patch("zimba.email.search", return_value=mock_result):
                main()
        captured = capsys.readouterr()
        output = json.loads(captured.out)
        assert output["total"] == 1
