<?php
// Reward settings service for the channel order claim button (getcoin-ORDER_ID).
// This controls how many diamonds users receive after joining an advertised/order channel.

if(!function_exists('bot_reward_setting_normalize_number')){
    function bot_reward_setting_normalize_number($value){
        $value = trim((string)$value);
        if(function_exists('bot_normalize_number_digits')){
            $value = bot_normalize_number_digits($value);
        }else{
            $fa = ['۰','۱','۲','۳','۴','۵','۶','۷','۸','۹','٠','١','٢','٣','٤','٥','٦','٧','٨','٩'];
            $en = ['0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9'];
            $value = str_replace($fa, $en, $value);
        }
        return preg_replace('/[^0-9\-]/', '', $value);
    }
}

if(!function_exists('bot_channel_claim_reward_defaults')){
    function bot_channel_claim_reward_defaults(){
        return ['normal'=>1, 'pro'=>2, 'vip'=>3];
    }
}

if(!function_exists('bot_channel_claim_reward_paths')){
    function bot_channel_claim_reward_paths(){
        return [
            'normal' => 'lib/Button/coinamount1.txt',
            'pro'    => 'lib/Button/coinamount2.txt',
            'vip'    => 'lib/Button/coinamount3.txt',
        ];
    }
}

if(!function_exists('bot_channel_claim_reward_key_from_user')){
    function bot_channel_claim_reward_key_from_user($user){
        $type = is_array($user) ? trim((string)($user['type-panel'] ?? 'عادی')) : 'عادی';
        if(in_array($type, ['ویژه', 'vip', 'VIP'], true)) return 'vip';
        if(in_array($type, ['حرفه ای', 'حرفه‌ای', 'pro', 'PRO'], true)) return 'pro';
        return 'normal';
    }
}

if(!function_exists('bot_channel_claim_reward_amount')){
    function bot_channel_claim_reward_amount($key){
        $key = (string)$key;
        $paths = bot_channel_claim_reward_paths();
        $defaults = bot_channel_claim_reward_defaults();
        if(!isset($paths[$key])) $key = 'normal';
        $default = (int)($defaults[$key] ?? 1);
        if(function_exists('bot_data_get_text')){
            $raw = bot_data_get_text($paths[$key], (string)$default);
        }else{
            $raw = is_file($paths[$key]) ? (string)@file_get_contents($paths[$key]) : (string)$default;
        }
        $raw = bot_reward_setting_normalize_number($raw);
        if($raw === '' || !preg_match('/^-?\d+$/', $raw)) return $default;
        return max(0, (int)$raw);
    }
}

if(!function_exists('bot_channel_claim_reward_for_user')){
    function bot_channel_claim_reward_for_user($user){
        return bot_channel_claim_reward_amount(bot_channel_claim_reward_key_from_user($user));
    }
}

if(!function_exists('bot_channel_claim_reward_save_amount')){
    function bot_channel_claim_reward_save_amount($key, $amount){
        $paths = bot_channel_claim_reward_paths();
        if(!isset($paths[$key])) return false;
        $amount = bot_reward_setting_normalize_number($amount);
        $amount = ($amount === '' || !preg_match('/^-?\d+$/', $amount)) ? 0 : max(0, (int)$amount);
        if(function_exists('bot_data_put_text')) return bot_data_put_text($paths[$key], (string)$amount);
        if(function_exists('Save')){ Save($paths[$key], (string)$amount); return true; }
        $dir = dirname($paths[$key]);
        if($dir && $dir !== '.' && !is_dir($dir)) @mkdir($dir, 0775, true);
        return @file_put_contents($paths[$key], (string)$amount, LOCK_EX) !== false;
    }
}

if(!function_exists('bot_channel_claim_reward_save_all')){
    function bot_channel_claim_reward_save_all($normal, $pro, $vip){
        bot_channel_claim_reward_save_amount('normal', $normal);
        bot_channel_claim_reward_save_amount('pro', $pro);
        bot_channel_claim_reward_save_amount('vip', $vip);
        return true;
    }
}
