"""
Test MARKITECT_EMOJI environment variable functionality - Issue #37

Tests the implementation of MARKITECT_EMOJI environment variable
to set user preferences for emoji output.
"""

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


class TestEmojiEnvironmentVariable:
    """Test MARKITECT_EMOJI environment variable functionality."""

    def test_markitect_emoji_env_var_enables_emoji_visualize_schema(self):
        """Test that MARKITECT_EMOJI=true enables emoji output 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)
            ], capture_output=True, text=True, env={'MARKITECT_EMOJI': 'true'})

            assert result.returncode == 0

            # Should contain emoji characters
            has_emojis = any(ord(char) > 127 for char in result.stdout)
            assert has_emojis, "MARKITECT_EMOJI=true should produce emoji output"

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

    def test_markitect_emoji_env_var_enables_emoji_schema_summary(self):
        """Test that MARKITECT_EMOJI=true enables emoji output 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)
            ], capture_output=True, text=True, env={'MARKITECT_EMOJI': 'true'})

            assert result.returncode == 0

            # Should contain emoji characters
            has_emojis = any(ord(char) > 127 for char in result.stdout)
            assert has_emojis, "MARKITECT_EMOJI=true should produce emoji output"

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

    def test_markitect_emoji_env_var_disables_emoji_visualize_schema(self):
        """Test that MARKITECT_EMOJI=false disables emoji output 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)
            ], capture_output=True, text=True, env={'MARKITECT_EMOJI': 'false'})

            assert result.returncode == 0

            # Should NOT contain emoji characters
            has_emojis = any(ord(char) > 127 for char in result.stdout)
            assert not has_emojis, "MARKITECT_EMOJI=false should produce ASCII-only output"

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

    def test_markitect_emoji_env_var_disables_emoji_schema_summary(self):
        """Test that MARKITECT_EMOJI=false disables emoji output 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)
            ], capture_output=True, text=True, env={'MARKITECT_EMOJI': 'false'})

            assert result.returncode == 0

            # Should NOT contain emoji characters
            has_emojis = any(ord(char) > 127 for char in result.stdout)
            assert not has_emojis, "MARKITECT_EMOJI=false should produce ASCII-only output"

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

    def test_cli_flag_overrides_env_var_ascii_visualize_schema(self):
        """Test that --ascii CLI flag overrides MARKITECT_EMOJI=true 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, env={'MARKITECT_EMOJI': 'true'})

            assert result.returncode == 0

            # Should NOT contain emoji characters (CLI flag wins)
            has_emojis = any(ord(char) > 127 for char in result.stdout)
            assert not has_emojis, "--ascii flag should override MARKITECT_EMOJI=true"

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

    def test_cli_flag_overrides_env_var_ascii_schema_summary(self):
        """Test that --ascii CLI flag overrides MARKITECT_EMOJI=true 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, env={'MARKITECT_EMOJI': 'true'})

            assert result.returncode == 0

            # Should NOT contain emoji characters (CLI flag wins)
            has_emojis = any(ord(char) > 127 for char in result.stdout)
            assert not has_emojis, "--ascii flag should override MARKITECT_EMOJI=true"

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

    def test_cli_flag_overrides_env_var_emoji_visualize_schema(self):
        """Test that --emoji CLI flag overrides MARKITECT_EMOJI=false 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), '--emoji'
            ], capture_output=True, text=True, env={'MARKITECT_EMOJI': 'false'})

            assert result.returncode == 0

            # Should contain emoji characters (CLI flag wins)
            has_emojis = any(ord(char) > 127 for char in result.stdout)
            assert has_emojis, "--emoji flag should override MARKITECT_EMOJI=false"

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

    def test_cli_flag_overrides_env_var_emoji_schema_summary(self):
        """Test that --emoji CLI flag overrides MARKITECT_EMOJI=false 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), '--emoji'
            ], capture_output=True, text=True, env={'MARKITECT_EMOJI': 'false'})

            assert result.returncode == 0

            # Should contain emoji characters (CLI flag wins)
            has_emojis = any(ord(char) > 127 for char in result.stdout)
            assert has_emojis, "--emoji flag should override MARKITECT_EMOJI=false"

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

    def test_invalid_env_var_values_default_to_emoji(self):
        """Test that invalid MARKITECT_EMOJI values default to emoji output - Issue #37."""
        test_file = Path("temp_test_schema.json")
        test_file.write_text('{"type": "object", "properties": {"name": {"type": "string"}}}')

        invalid_values = ['invalid', '1', 'yes', 'no']

        try:
            for invalid_value in invalid_values:
                result = subprocess.run([
                    sys.executable, 'tools/visualize_schema.py', str(test_file)
                ], capture_output=True, text=True, env={'MARKITECT_EMOJI': invalid_value})

                assert result.returncode == 0

                # Should default to emoji output for invalid values
                has_emojis = any(ord(char) > 127 for char in result.stdout)
                assert has_emojis, f"MARKITECT_EMOJI='{invalid_value}' should default to emoji output"

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

    def test_case_insensitive_env_var_handling(self):
        """Test that MARKITECT_EMOJI handles case variations properly - Issue #37."""
        test_file = Path("temp_test_schema.json")
        test_file.write_text('{"type": "object", "properties": {"name": {"type": "string"}}}')

        case_variations = [
            ('True', True),
            ('TRUE', True),
            ('true', True),
            ('False', False),
            ('FALSE', False),
            ('false', False)
        ]

        try:
            for env_value, should_have_emojis in case_variations:
                result = subprocess.run([
                    sys.executable, 'tools/visualize_schema.py', str(test_file)
                ], capture_output=True, text=True, env={'MARKITECT_EMOJI': env_value})

                assert result.returncode == 0

                has_emojis = any(ord(char) > 127 for char in result.stdout)
                if should_have_emojis:
                    assert has_emojis, f"MARKITECT_EMOJI='{env_value}' should enable emoji output"
                else:
                    assert not has_emojis, f"MARKITECT_EMOJI='{env_value}' should disable emoji output"

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