import sys
import json
import re

def parse_price(text, feetype=None, action_type=None, default_multiplier=1000):
    text = str(text).strip()
    if not text:
        return {"price": 0.0, "score": 0.0, "multiplier": default_multiplier}
        
    # Normalize Arabic characters
    text = text.replace('أ', 'ا').replace('إ', 'ا').replace('ة', 'ه').replace('ى', 'ي')
    
    # Normalize Arabic numbers to English
    arabic_to_english = str.maketrans('٠١٢٣٤٥٦٧٨٩', '0123456789')
    text = text.translate(arabic_to_english)
    text = text.replace('،', ',').replace('٫', '.')
    
    # Convert spelled-out Arabic numbers/fractions to digits
    spelled_numbers = {
        'واحد ونص': '1.5', 'واحد ونصف': '1.5',
        'نص': '0.5', 'نصف': '0.5', 'ونص': '0.5', 'ونصف': '0.5',
        'بنص': '0.5', 'بنصف': '0.5', 'بالنص': '0.5', 'بالنصف': '0.5',
        'ربع': '0.25', 'بربع': '0.25', 'وربع': '0.25', 'بالربع': '0.25',
        'تلت': '0.333', 'ثلث': '0.333', 'بتلت': '0.333', 'بثلث': '0.333', 'وتلت': '0.333', 'وثلث': '0.333',
        'تلات ارباع': '0.75', 'ثلاثة ارباع': '0.75',
        'خمس': '0.2', 'بخمس': '0.2', 'وخمس': '0.2',
        'واحد': '1', 'بواحد': '1', 'ليره': '1', 'ليرة': '1', 'بليره': '1', 'بليرة': '1',
        'اثنين': '2', 'تنين': '2', 'ليرتين': '2', 'بليرتين': '2',
        'ثلاثة': '3', 'تلاته': '3', 'تلات': '3',
        'اربعة': '4', 'اربعه': '4', 'اربع': '4',
        'خمسة': '5', 'خمسه': '5',
        'ستة': '6', 'سته': '6', 'ست': '6',
        'سبعة': '7', 'سبعه': '7', 'سبع': '7',
        'ثمانية': '8', 'ثمانيه': '8', 'تمانيه': '8', 'تمان': '8',
        'تسعة': '9', 'تسعه': '9', 'تسع': '9',
        'عشرة': '10', 'عشره': '10', 'عشر': '10',
        'ليره ونص': '1.5', 'ليرة ونص': '1.5', 'بليره ونص': '1.5', 'بليرة ونص': '1.5',
        'ليرتين ونص': '2.5', 'بليرتين ونص': '2.5'
    }
    
    for phrase in sorted(spelled_numbers.keys(), key=len, reverse=True):
        val = spelled_numbers[phrase]
        pattern = r'\b' + phrase.replace(' ', r'\s+') + r'\b'
        text = re.sub(pattern, val, text)
    
    # Check for "بدون" or "بلا" (zero price)
    if re.search(r'\b(بدون|بلا|بلاش|ببلاش|بببلاش|صفر|مجانا|مجانان|مجانن|ب مجان)\b', text):
        return {"price": 0.0, "score": 1.0, "multiplier": 1}
        
    # Check for negative indicators
    is_negative = bool(re.search(r'(لكم|راجع|علين|سالب|-|الكم|للحساب)', text))
    
    # Check for multiplier terms
    # Check for multiplier terms
    mult_10000_list = [r'بالعشرتلاف', r'بالعشر تالاف', r'بالعشر الاف', r'بالعشرالاف', r'بعشر الاف', r'بعشرالاف', r'بعشر تالاف', r'بعشرتلاف', r'بالعشرا', r'العشرا', r'العشره', r'بعشره', r'بالعشره', r'ب العشره']
    mult_1000_list = [r'بالالاف', r'بالألاف', r'بالآلاف', r'بالالف', r'ب الالف', r'الالف', r'بالف', r'ب الف', r'الف', r'بلالف', r'بلف', r'باللف']
    mult_100_list = [r'بالميه', r'بالمييه', r'بميه', r'ب الميه', r'الميه', r'ميه', r'بالمئه', r'بمئه', r'ب المئه', r'مئه', r'%', r'٪']
    
    p_10000 = '|'.join(mult_10000_list)
    p_1000 = '|'.join(mult_1000_list)
    p_100 = '|'.join(mult_100_list)
    
    multiplier = default_multiplier
    if re.search(f'({p_100})', text):
        multiplier = 100
    elif re.search(f'({p_10000})', text):  # Evaluate 10,000 before 1,000 to avoid "الاف" substring overlap
        multiplier = 10000
    elif re.search(f'({p_1000})', text):
        multiplier = 1000
    
    # Clean text a bit before finding numbers to avoid breaking on combined strings like "لكم5"
    all_terms = p_10000 + '|' + p_1000 + '|' + p_100 + r'|لكم|راجع|علين|سالب|الكم|للحساب'
    text = re.sub(f'({all_terms})', r' \1 ', text)
    
    # Find numbers. Handle formats like .002, 0.002, 2,5, 2.33
    # This regex matches digits with optional dot or comma and more digits, or just a leading dot and digits
    # It ensures we capture the whole decimal (e.g. 2,33 or 25.2)
    numbers = re.findall(r'\d+[.,]\d+|\.\d+|\d+', text)
    
    if not numbers:
        return {"price": 0.0, "score": 0.2, "multiplier": 1} # text is there but no numbers found
        
    # Take the first matched number
    num_str = numbers[0].replace(',', '.')
    try:
        val = float(num_str)
    except ValueError:
        return {"price": 0.0, "score": 0.0, "multiplier": 1}
        
    price = val
    
    # If action_type is NOT 'need' (e.g. 'avaliable' or anything else), 
    # we should ignore the default fee type and parse from the message instead
    if action_type and action_type.lower() != 'need':
        feetype = None
        
    if feetype == 'alyna':
        price = -abs(price)
    elif feetype == 'lana':
        price = abs(price)
    else:
        if is_negative:
            price = -abs(price)
        else:
            price = abs(price)
        
    # Confidence heuristics
    score = 0.8 # Base score for finding a number
    
    # If the user explicitly wrote "لكم" or "راجع" or multiplier logic, it's almost certainly a price offer
    if is_negative or multiplier != 1:
        score = 1.0
    # If the message is very short (just a number or a couple of words), it's highly likely a price reply
    elif len(numbers) == 1 and len(text.split()) <= 3:
        score = 1.0
        
    return {"price": price, "score": score, "multiplier": multiplier}

if __name__ == '__main__':
    # Read from stdin
    input_text = sys.stdin.read().strip()
    if not input_text and len(sys.argv) > 1:
        input_text = sys.argv[1]
        
    feetype = None
    action_type = None
    default_multiplier = 1000
    try:
        data = json.loads(input_text)
        if isinstance(data, dict):
            if 'message' in data:
                input_text = data['message']
            if 'feetype' in data:
                feetype = data['feetype']
            if 'action_type' in data:
                action_type = data['action_type']
            if 'default_multiplier' in data:
                try:
                    default_multiplier = int(data['default_multiplier'])
                except Exception:
                    default_multiplier = 1000
    except Exception:
        pass
        
    res = parse_price(input_text, feetype=feetype, action_type=action_type, default_multiplier=default_multiplier)
    print(json.dumps(res, ensure_ascii=False))
