import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent))

import tests.test_lexer as test_lexer
import tests.test_parser as test_parser
import tests.test_visualizer as test_visualizer
import tests.test_processor as test_processor


def run_all_tests():
    print("=" * 50)
    print("Running Lexer Tests")
    print("=" * 50)
    try:
        test_lexer.test_lexer_keywords()
        test_lexer.test_lexer_identifiers()
        test_lexer.test_lexer_numbers()
        test_lexer.test_lexer_operators()
        test_lexer.test_lexer_delimiters()
        test_lexer.test_lexer_line_tracking()
        test_lexer.test_lexer_complex_expression()
        print("\n✓ All lexer tests PASSED\n")
    except Exception as e:
        print(f"\n✗ Lexer test failed: {e}\n")
        return False
    
    print("=" * 50)
    print("Running Parser Tests")
    print("=" * 50)
    try:
        test_parser.test_parser_simple_module()
        test_parser.test_parser_module_with_ports()
        test_parser.test_parser_wire_declaration()
        test_parser.test_parser_assign_statement()
        test_parser.test_parser_always_block()
        test_parser.test_parser_arithmetic_expression()
        test_parser.test_parser_complex_module()
        test_parser.test_parser_reg_declaration()
        print("\n✓ All parser tests PASSED\n")
    except Exception as e:
        print(f"\n✗ Parser test failed: {e}\n")
        return False
    
    print("=" * 50)
    print("Running Visualizer Tests")
    print("=" * 50)
    try:
        test_visualizer.test_visualizer_dot_generation()
        test_visualizer.test_visualizer_node_creation()
        test_visualizer.test_visualizer_with_ports()
        test_visualizer.test_visualizer_with_always()
        test_visualizer.test_visualizer_complex_structure()
        print("\n✓ All visualizer tests PASSED\n")
    except Exception as e:
        print(f"\n✗ Visualizer test failed: {e}\n")
        return False
    
    print("=" * 50)
    print("Running Processor Tests")
    print("=" * 50)
    try:
        test_processor.test_processor_load_file()
        test_processor.test_processor_parse()
        test_processor.test_processor_visualize()
        print("\n✓ All processor tests PASSED\n")
    except Exception as e:
        print(f"\n✗ Processor test failed: {e}\n")
        return False
    
    print("=" * 50)
    print("FINAL RESULT: ALL TESTS PASSED ✓✓✓")
    print("=" * 50)
    return True


if __name__ == "__main__":
    success = run_all_tests()
    sys.exit(0 if success else 1)
