// SPDX-License-Identifier: Apache-2.0
// Copyright 2025 Wildcard Matcher Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "WildcardMatcher.h"
#include <iostream>
#include <string>

/**
 * @brief Demonstrates the wildcard matching functionality
 */
void DemonstrateWildcardMatching() {
    WildcardMatcher matcher;
    
    std::cout << "🎯 Wildcard Pattern Matching Demonstration" << std::endl;
    std::cout << "===========================================" << std::endl;
    std::cout << "This program solves the LeetCode 44 - Wildcard Matching problem" << std::endl;
    std::cout << "Supports '?' (single character) and '*' (any sequence) wildcards" << std::endl;
    
    // Run comprehensive tests
    matcher.RunTests();
    
    // Interactive examples
    std::cout << "\n🔍 Interactive Examples:" << std::endl;
    std::cout << std::string(40, '-') << std::endl;
    
    std::vector<std::pair<std::string, std::string>> examples = {
        {"hello", "h*o"},
        {"programming", "p*g*m*g"},
        {"leetcode", "l?et*de"},
        {"algorithm", "*rith*"},
        {"dynamic", "d?n?m?c"}
    };
    
    for (const auto& example : examples) {
        const std::string& text = example.first;
        const std::string& pattern = example.second;
        
        bool dpResult = matcher.IsMatchDP(text, pattern);
        bool recursiveResult = matcher.IsMatchRecursive(text, pattern);
        
        std::cout << "\nText: \"" << text << "\" | Pattern: \"" << pattern << "\"" << std::endl;
        std::cout << "DP Result: " << (dpResult ? "✅ MATCH" : "❌ NO MATCH") << std::endl;
        std::cout << "Recursive Result: " << (recursiveResult ? "✅ MATCH" : "❌ NO MATCH") << std::endl;
        std::cout << "Recursions: " << matcher.GetRecursionCount() 
                  << " | Backtracks: " << matcher.GetBacktrackCount() << std::endl;
    }
}

/**
 * @brief Interactive mode for user input
 */
void InteractiveMode() {
    WildcardMatcher matcher;
    std::string text, pattern;
    
    std::cout << "\n🎮 Interactive Wildcard Matching Mode" << std::endl;
    std::cout << "====================================" << std::endl;
    std::cout << "Enter 'quit' for either input to exit" << std::endl;
    
    while (true) {
        std::cout << "\nEnter text to match: ";
        std::getline(std::cin, text);
        
        if (text == "quit") break;
        
        std::cout << "Enter wildcard pattern: ";
        std::getline(std::cin, pattern);
        
        if (pattern == "quit") break;
        
        bool result = matcher.IsMatch(text, pattern);
        matcher.PrintMatchResult(text, pattern, result);
        
        // Also show recursive approach for comparison
        std::cout << "\nRecursive approach comparison:" << std::endl;
        bool recursiveResult = matcher.IsMatchRecursive(text, pattern);
        std::cout << "Recursive result: " << (recursiveResult ? "MATCH" : "NO MATCH") << std::endl;
        std::cout << "Recursions used: " << matcher.GetRecursionCount() << std::endl;
        std::cout << "Backtracks: " << matcher.GetBacktrackCount() << std::endl;
    }
}

/**
 * @brief Main function
 */
int main() {
    std::cout << "🌟 Wildcard Pattern Matcher - LeetCode 44 Solution" << std::endl;
    std::cout << "=================================================" << std::endl;
    std::cout << "Converted from Sudoku Solver using similar backtracking principles" << std::endl;
    
    try {
        // Run demonstration
        DemonstrateWildcardMatching();
        
        // Ask user if they want interactive mode
        std::cout << "\nWould you like to try interactive mode? (y/n): ";
        std::string choice;
        std::getline(std::cin, choice);
        
        if (choice == "y" || choice == "Y" || choice == "yes" || choice == "Yes") {
            InteractiveMode();
        }
        
        std::cout << "\n✨ Thank you for using Wildcard Pattern Matcher!" << std::endl;
        
    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }
    
    return 0;
}