import os

file_path = r"C:\Users\ahmed\Downloads\smartlogicchatbot\lib\SalesBrain.php"

with open(file_path, "r", encoding="utf-8") as f:
    content = f.read()

target = """        $companyData = self::extractCompanyData($text);
        if (!empty($companyData)) {
            $analysis['company_data'] = $companyData;
            $analysis['score_factors'][] = 'company_info';
        }"""
        
replacement = """        $companyData = self::extractCompanyData($text);
        if (!empty($companyData)) {
            $analysis['company_data'] = $companyData;
            $analysis['score_factors'][] = 'company_info';
        }

        // Data Enrichment: Extract URLs or potential brand names
        if (preg_match('/(?:https?:\/\/)?(?:www\.)?([a-zA-Z0-9-]+\.[a-zA-Z]{2,})/u', $input, $matches)) {
            $analysis['company_data']['website'] = $matches[0];
            $analysis['score_factors'][] = 'provided_website';
        }"""

if target in content:
    content = content.replace(target, replacement)
    
target2 = """    if (!empty($analysis['project_size'])) {
        $customer['project_size'] = $analysis['project_size'];
    }"""
replacement2 = """    if (!empty($analysis['project_size'])) {
        $customer['project_size'] = $analysis['project_size'];
    }
    if (!empty($analysis['company_data']['website'])) {
        $customer['company_website'] = $analysis['company_data']['website'];
    }"""

bot_file = r"C:\Users\ahmed\Downloads\smartlogicchatbot\bot.php"
with open(bot_file, "r", encoding="utf-8") as f:
    bot = f.read()

if target2 in bot:
    bot = bot.replace(target2, replacement2)
    with open(bot_file, "w", encoding="utf-8") as f:
        f.write(bot)

with open(file_path, "w", encoding="utf-8") as f:
    f.write(content)

print("Data Enrichment added.")
