"""
Test basic emoji flag functionality - Issue #37

Tests the implementation of --emoji flag and MARKITECT_EMOJI environment variable
to complement the existing --ascii flag functionality.
"""

import pytest
import os
import subprocess
import sys
from pathlib import Path
from unittest.mock import patch, mock_open


class TestEmojiFlag:
    """Test emoji flag functionality in CLI tools."""

    def test_emoji_flag_exists_in_visualize_schema(self):
        """Test that --emoji flag is available in visualize_schema.py - Issue #37."""
        # Test that the --emoji flag is recognized by the argument parser
        result = subprocess.run([
            sys.executable, 'tools/visualize_schema.py', '--help'
        ], capture_output=True, text=True)

        assert result.returncode == 0
        assert "--emoji" in result.stdout
        assert "Use emoji output" in result.stdout or "Enable emoji output" in result.stdout

    def test_emoji_flag_exists_in_schema_summary(self):
        """Test that --emoji flag is available in schema_summary.py - Issue #37."""
        result = subprocess.run([
            sys.executable, 'tools/schema_summary.py', '--help'
        ], capture_output=True, text=True)

        assert result.returncode == 0
        assert "--emoji" in result.stdout
        assert "Use emoji output" in result.stdout or "Enable emoji output" in result.stdout

    def test_emoji_flag_mutually_exclusive_with_ascii_visualize_schema(self):
        """Test that --emoji and --ascii flags are mutually exclusive in visualize_schema - Issue #37."""
        # Create a temporary test file
        test_file = Path("temp_test_schema.json")
        test_file.write_text('{"type": "object", "properties": {"name": {"type": "string"}}}')

        try:
            result = subprocess.run([
                sys.executable, 'tools/visualize_schema.py', str(test_file), '--ascii', '--emoji'
            ], capture_output=True, text=True)

            # Should fail with argument parsing error due to mutual exclusivity
            assert result.returncode == 2, "Using both --ascii and --emoji should result in argument parsing error"
            assert "not allowed with argument" in result.stderr, "Error message should indicate mutual exclusivity"

        finally:
            if test_file.exists():
                test_file.unlink()

    def test_emoji_flag_mutually_exclusive_with_ascii_schema_summary(self):
        """Test that --emoji and --ascii flags are mutually exclusive in schema_summary - Issue #37."""
        test_file = Path("temp_test_schema.json")
        test_file.write_text('{"type": "object", "properties": {"name": {"type": "string"}}}')

        try:
            result = subprocess.run([
                sys.executable, 'tools/schema_summary.py', str(test_file), '--ascii', '--emoji'
            ], capture_output=True, text=True)

            # Should fail with argument parsing error due to mutual exclusivity
            assert result.returncode == 2, "Using both --ascii and --emoji should result in argument parsing error"
            assert "not allowed with argument" in result.stderr, "Error message should indicate mutual exclusivity"

        finally:
            if test_file.exists():
                test_file.unlink()

    def test_emoji_output_is_default_in_visualize_schema(self):
        """Test that emoji output is the default behavior in visualize_schema - Issue #37."""
        test_file = Path("temp_test_schema.json")
        test_file.write_text('{"type": "object", "properties": {"name": {"type": "string"}}}')

        try:
            # Test default output (should have emojis)
            result_default = subprocess.run([
                sys.executable, 'tools/visualize_schema.py', str(test_file)
            ], capture_output=True, text=True)

            # Test explicit emoji flag
            result_emoji = subprocess.run([
                sys.executable, 'tools/visualize_schema.py', str(test_file), '--emoji'
            ], capture_output=True, text=True)

            assert result_default.returncode == 0
            assert result_emoji.returncode == 0

            # Both should contain emoji characters (basic check)
            default_has_emojis = any(ord(char) > 127 for char in result_default.stdout)
            emoji_has_emojis = any(ord(char) > 127 for char in result_emoji.stdout)

            assert default_has_emojis, "Default output should contain emoji characters"
            assert emoji_has_emojis, "Explicit --emoji flag should produce emoji output"

        finally:
            if test_file.exists():
                test_file.unlink()

    def test_emoji_output_is_default_in_schema_summary(self):
        """Test that emoji output is the default behavior in schema_summary - Issue #37."""
        test_file = Path("temp_test_schema.json")
        test_file.write_text('{"type": "object", "properties": {"name": {"type": "string"}}}')

        try:
            # Test default output (should have emojis)
            result_default = subprocess.run([
                sys.executable, 'tools/schema_summary.py', str(test_file)
            ], capture_output=True, text=True)

            # Test explicit emoji flag
            result_emoji = subprocess.run([
                sys.executable, 'tools/schema_summary.py', str(test_file), '--emoji'
            ], capture_output=True, text=True)

            assert result_default.returncode == 0
            assert result_emoji.returncode == 0

            # Both should contain emoji characters (basic check)
            default_has_emojis = any(ord(char) > 127 for char in result_default.stdout)
            emoji_has_emojis = any(ord(char) > 127 for char in result_emoji.stdout)

            assert default_has_emojis, "Default output should contain emoji characters"
            assert emoji_has_emojis, "Explicit --emoji flag should produce emoji output"

        finally:
            if test_file.exists():
                test_file.unlink()

    def test_ascii_flag_overrides_emoji_default_visualize_schema(self):
        """Test that --ascii flag overrides emoji default in visualize_schema - Issue #37."""
        test_file = Path("temp_test_schema.json")
        test_file.write_text('{"type": "object", "properties": {"name": {"type": "string"}}}')

        try:
            result = subprocess.run([
                sys.executable, 'tools/visualize_schema.py', str(test_file), '--ascii'
            ], capture_output=True, text=True)

            assert result.returncode == 0

            # Should NOT contain emoji characters
            has_emojis = any(ord(char) > 127 for char in result.stdout)
            assert not has_emojis, "ASCII mode should not contain emoji characters"

        finally:
            if test_file.exists():
                test_file.unlink()

    def test_ascii_flag_overrides_emoji_default_schema_summary(self):
        """Test that --ascii flag overrides emoji default in schema_summary - Issue #37."""
        test_file = Path("temp_test_schema.json")
        test_file.write_text('{"type": "object", "properties": {"name": {"type": "string"}}}')

        try:
            result = subprocess.run([
                sys.executable, 'tools/schema_summary.py', str(test_file), '--ascii'
            ], capture_output=True, text=True)

            assert result.returncode == 0

            # Should NOT contain emoji characters
            has_emojis = any(ord(char) > 127 for char in result.stdout)
            assert not has_emojis, "ASCII mode should not contain emoji characters"

        finally:
            if test_file.exists():
                test_file.unlink()