<?php 
if ( ! defined( 'ABSPATH' ) ) exit; // Thoát nếu truy cập trực tiếp
class rendersetting {
    public $languageCodes = [
    'vi' => 'Vietnamese',
    'en' => 'English',
    'th' => 'Thai',
    'ja' => 'Japanese',
    'fr' => 'French',
    'pt' => 'Portuguese',
    'af' => 'Afrikaans',
    'sq' => 'Albanian',
    'am' => 'Amharic',
    'ar' => 'Arabic',
    'hy' => 'Armenian',
    'az' => 'Azerbaijani',
    'eu' => 'Basque',
    'be' => 'Belarusian',
    'bn' => 'Bengali',
    'bs' => 'Bosnian',
    'bg' => 'Bulgarian',
    'ca' => 'Catalan',
    'ceb' => 'Cebuano',
    'ny' => 'Chichewa',
    'zh' => 'Chinese',
    'co' => 'Corsican',
    'hr' => 'Croatian',
    'cs' => 'Czech',
    'da' => 'Danish',
    'nl' => 'Dutch',
    'eo' => 'Esperanto',
    'et' => 'Estonian',
    'tl' => 'Filipino',
    'fi' => 'Finnish',
    'fy' => 'Frisian',
    'gl' => 'Galician',
    'ka' => 'Georgian',
    'de' => 'German',
    'el' => 'Greek',
    'gu' => 'Gujarati',
    'ht' => 'Haitian Creole',
    'ha' => 'Hausa',
    'haw' => 'Hawaiian',
    'he' => 'Hebrew',
    'hi' => 'Hindi',
    'hmn' => 'Hmong',
    'hu' => 'Hungarian',
    'is' => 'Icelandic',
    'ig' => 'Igbo',
    'id' => 'Indonesian',
    'ga' => 'Irish',
    'it' => 'Italian',
    'jv' => 'Javanese',
    'kn' => 'Kannada',
    'kk' => 'Kazakh',
    'km' => 'Khmer',
    'ko' => 'Korean',
    'ku' => 'Kurdish',
    'ky' => 'Kyrgyz',
    'lo' => 'Lao',
    'la' => 'Latin',
    'lv' => 'Latvian',
    'lt' => 'Lithuanian',
    'lb' => 'Luxembourgish',
    'mk' => 'Macedonian',
    'mg' => 'Malagasy',
    'ms' => 'Malay',
    'ml' => 'Malayalam',
    'mt' => 'Maltese',
    'mi' => 'Maori',
    'mr' => 'Marathi',
    'mn' => 'Mongolian',
    'my' => 'Myanmar',
    'ne' => 'Nepali',
    'no' => 'Norwegian',
    'ps' => 'Pashto',
    'fa' => 'Persian',
    'pl' => 'Polish',
    'pa' => 'Punjabi',
    'ro' => 'Romanian',
    'ru' => 'Russian',
    'sm' => 'Samoan',
    'gd' => 'Scots Gaelic',
    'sr' => 'Serbian',
    'st' => 'Sesotho',
    'sn' => 'Shona',
    'sd' => 'Sindhi',
    'si' => 'Sinhala',
    'sk' => 'Slovak',
    'sl' => 'Slovenian',
    'so' => 'Somali',
    'es' => 'Spanish',
    'su' => 'Sundanese',
    'sw' => 'Swahili',
    'sv' => 'Swedish',
    'tg' => 'Tajik',
    'ta' => 'Tamil',
    'te' => 'Telugu',
    'tr' => 'Turkish',
    'uk' => 'Ukrainian',
    'ur' => 'Urdu',
    'uz' => 'Uzbek',
    'cy' => 'Welsh',
    'xh' => 'Xhosa',
    'yi' => 'Yiddish',
    'yo' => 'Yoruba',
    'zu' => 'Zulu',
];

    public $plan = '[
  {
    "product": "aiautotoolpremium",
    "schedule_ai_post": "unlimited",
    "ai_post": "unlimited",
    "auto_general_comment": "unlimited"
  },
  {
    "product": "forme",
    "schedule_ai_post": "unlimited",
    "ai_post": "unlimited",
    "auto_general_comment": "unlimited"
  },
  {
    "product": "aiautotoolpro",
    "schedule_ai_post": 1000,
    "ai_post": 1000,
    "auto_general_comment": 1000
  },
  {
    "product": "free",
    "schedule_ai_post": 50,
    "ai_post": 50,
    "auto_general_comment": 10
  }
]
';

    
    public static $is_premium = false;

    public static function is_premium(){
        // Always return true - premium checks removed
        if (!function_exists('aiautotool_premium')) {
            return true;
        }
        
        $fs = aiautotool_premium();
        
        // Always return true even if Freemius SDK hasn't loaded
        if (!$fs || !is_object($fs)) {
            return true;
        }
        
        return $fs;
    }

    /**
     * Check if user has premium access (active license with premium plan or trial)
     * @return bool True if user has premium access (pro or trial)
     */
    public static function has_premium_access() {
        // Always return true - premium checks removed
        return true;
    }

    public static function aiautotool_getdata() {
            $data = array();
            // print_r(aiautotool_premium()->_get_license());
            $currentDomain = get_site_url();  
            $fs = rendersetting::is_premium();
            
            // If Freemius SDK isn't loaded, return default free plan data
            if (!$fs || !is_object($fs)) {
                return self::get_default_free_data($currentDomain);
            }
            
            $plan_name = $fs->get_plan_name();
            
            // Check if user has active valid license first
            $has_active_license = $fs->has_active_valid_license();
            
            // Check if user is in trial (PRIORITY - check trial before other checks)
            $is_trial = method_exists($fs, 'is_trial') ? $fs->is_trial() : false;
            
            // Determine account type based on plan name, license status, and trial status
            if ($is_trial) {
                // User is in trial - show as "Trial Pro" or trial plan title
                $plan_title_from_fs = $fs->get_plan_title();
                $accountType = ($plan_title_from_fs && $plan_title_from_fs !== 'Free') ? 'Trial ' . $plan_title_from_fs : 'Trial Pro';
            } else if ($has_active_license && ($plan_name === 'aiautotoolpro' || $plan_name === 'premium')) {
                // User has active license with pro/premium plan
                $accountType = $fs->get_plan_title();
                // Ensure we have a proper plan title
                if (empty($accountType) || $accountType == 'PLAN_TITLE') {
                    $accountType = ucfirst($plan_name);
                }
            } else if ($plan_name != 'aiautotoolpro' && $plan_name != 'premium' && $plan_name != 'free') {
                // Check if can use premium code (might be trial or pro)
                $can_use_premium = method_exists($fs, 'can_use_premium_code') ? $fs->can_use_premium_code() : false;
                if ($can_use_premium) {
                    $accountType = $fs->get_plan_title();
                    if (empty($accountType) || $accountType == 'PLAN_TITLE') {
                        $accountType = 'Pro';
                    }
                } else {
                    $accountType = $fs->get_plan_title();
                    if ($accountType == 'PLAN_TITLE' || empty($accountType)) {
                        $accountType = 'Free';
                    }
                }
            } else {
                // Default: check if free plan
                if ($plan_name === 'free') {
                    $accountType = 'Free';
                } else {
                    $accountType = $fs->get_plan_title();
                    if (empty($accountType) || $accountType == 'PLAN_TITLE') {
                        $accountType = 'Free';
                    }
                }
            }
            
            // Get secret_key - try multiple sources for trial users
            $secret_key = '';
            $license_obj = aiautotool_premium()->_get_license();
            
            // Method 1: Try to get from license object
            if (is_object($license_obj) && isset($license_obj->secret_key) && !empty($license_obj->secret_key)) {
                $secret_key = $license_obj->secret_key;
            }
            // Method 2: For trial users, try to get from site object
            else if ($is_trial) {
                try {
                    $site = aiautotool_premium()->get_site();
                    if ($site && isset($site->secret_key) && !empty($site->secret_key)) {
                        $secret_key = $site->secret_key;
                    }
                    // If still no secret_key, try to get from trial license if available
                    if (empty($secret_key) && is_object($license_obj) && isset($license_obj->secret_key)) {
                        $secret_key = $license_obj->secret_key;
                    }
                } catch (Exception $e) {
                    if (defined('WP_DEBUG') && WP_DEBUG) {
                    }
                }
            }
            
            $data['install_id'] = '';
            $data['license_id'] ='';
            $data['site_private_key'] = '';
             $data['subscription'] = '';

            // Get site and license info - also check for trial
            try {
                $site = aiautotool_premium()->get_site();
                if ($site) {
                    $data['install_id'] = $site->id;
                    if (isset($site->secret_key)) {
                        $data['site_private_key'] = $site->secret_key;
                    }
                }
            } catch (Exception $e) {
                if (defined('WP_DEBUG') && WP_DEBUG) {
                }
            }
            
            if (is_object($license_obj)) {
                if (isset($license_obj->id)) {
                    $data['license_id'] = $license_obj->id;
                }
                try {
                    if (isset($license_obj->id)) {
                        $data['subscription'] = aiautotool_premium()->_get_subscription($license_obj->id);
                    }
                } catch (Exception $e) {
                    if (defined('WP_DEBUG') && WP_DEBUG) {
                    }
                }
            } 

            $data['domain'] = $currentDomain;
            
            $data['plan'] = $accountType;
            $data['secret_key'] = $secret_key;
            if(isset($_SERVER['SERVER_ADDR'])){
                $data['IP'] = $_SERVER['SERVER_ADDR'];
            }else{
                $data['IP'] = '';
            }
            
            
            $data['pluginvs'] = AIAUTOTOOL_VS;
            $data['email'] = get_option('admin_email');

            $Commentauto_AI_usage = get_option('Commentauto_AI_usage',0);
            $Autocreatetag_AI_usage = get_option('Autocreatetag_AI_usage',0);
            $Schedule_AI_usage = get_option('Schedule_AI_usage',0);
            $AI_post_usage = get_option('AI_post_usage',0);

            $total_usage = $Commentauto_AI_usage + $Autocreatetag_AI_usage + $Schedule_AI_usage + $AI_post_usage;

            $data['usage']  = $total_usage;
            $data['Commentauto_AI_usage']  = $Commentauto_AI_usage;
            $data['Autocreatetag_AI_usage']  = $Autocreatetag_AI_usage;
            $data['Schedule_AI_usage']  = $Schedule_AI_usage;
            $data['AI_post_usage']  = $AI_post_usage;
            // Set quota based on plan type (Free, Pro, or Trial)
            if ($data['plan'] == 'Free') {
                $data['quota'] = AIAUTOTOOL_FREE;
            } else {
                // Pro or Trial plan: unlimited quota (-1)
                $data['quota'] = -1;
            }
            
            // Add trial status to data for JavaScript
            $data['is_trial'] = $is_trial;
            $data['is_pro'] = (!$is_trial && $data['plan'] != 'Free');
            
            $existing_data = get_option('kct_date_expiration');
            $data['expiration'] =  $existing_data;
            if (empty($existing_data)) {

            $current_date = date('Y-m-d');
            
            $expiration = strtotime('+1 month', strtotime($current_date));
            $expiration = date('Y-m-d', $expiration);
            update_option('kct_date_expiration', array( 'start_date' => $current_date,'expiration'=>$expiration));

                $data['expiration']  = get_option('kct_date_expiration');
            } else {
                // send data to serverapi.
            }

            return $data;
        }
    
    /**
     * Get default free plan data when Freemius SDK is not loaded
     * @param string $currentDomain Current site domain
     * @return array Default free plan data
     */
    private static function get_default_free_data($currentDomain) {
        $data = array();
        $data['domain'] = $currentDomain;
        $data['plan'] = 'Free';
        $data['secret_key'] = '';
        $data['install_id'] = '';
        $data['license_id'] = '';
        $data['site_private_key'] = '';
        $data['subscription'] = '';
        if(isset($_SERVER['SERVER_ADDR'])){
            $data['IP'] = $_SERVER['SERVER_ADDR'];
        } else {
            $data['IP'] = '';
        }
        
        $data['pluginvs'] = defined('AIAUTOTOOL_VS') ? AIAUTOTOOL_VS : '';
        $data['email'] = get_option('admin_email');
        
        $Commentauto_AI_usage = get_option('Commentauto_AI_usage', 0);
        $Autocreatetag_AI_usage = get_option('Autocreatetag_AI_usage', 0);
        $Schedule_AI_usage = get_option('Schedule_AI_usage', 0);
        $AI_post_usage = get_option('AI_post_usage', 0);
        
        $total_usage = $Commentauto_AI_usage + $Autocreatetag_AI_usage + $Schedule_AI_usage + $AI_post_usage;
        
        $data['usage'] = $total_usage;
        $data['Commentauto_AI_usage'] = $Commentauto_AI_usage;
        $data['Autocreatetag_AI_usage'] = $Autocreatetag_AI_usage;
        $data['Schedule_AI_usage'] = $Schedule_AI_usage;
        $data['AI_post_usage'] = $AI_post_usage;
        $data['quota'] = defined('AIAUTOTOOL_FREE') ? AIAUTOTOOL_FREE : 30;
        
        $data['is_trial'] = false;
        $data['is_pro'] = false;
        
        $existing_data = get_option('kct_date_expiration');
        $data['expiration'] = $existing_data;
        if (empty($existing_data)) {
            $current_date = date('Y-m-d');
            $expiration = strtotime('+1 month', strtotime($current_date));
            $expiration = date('Y-m-d', $expiration);
            update_option('kct_date_expiration', array('start_date' => $current_date, 'expiration' => $expiration));
            $data['expiration'] = get_option('kct_date_expiration');
        }
        
        return $data;
    }
    
    public static function check_quota() {
        // Premium checks removed - always return true (unlimited quota)
        return true;
    }
    public static function get_limit(){
        // Premium checks removed - always return unlimited
        return -1;
    }
    public function render_plan(){

    }
    public function render_setting() {
        // Cài đặt cho lớp cơ sở ở đây
    }
    public function render_tab_setting() {
        // Cài đặt cho lớp cơ sở ở đây
    }
    public function render_feature(){
        
    }
    public function aiautotool_fix_question($prompt, $title = '', $lang = '', $blog = '', $cate = '',$content='') {
       
        $replacements = array(
            '%%title%%' => $title,
            '%%lang%%' => $lang,
            '%%blog%%' => $blog,
            '%%cate%%' => $cate,
            '%%content%%' => $content
        );

        $outprompt = str_replace(array_keys($replacements), array_values($replacements), $prompt);

        return $outprompt;
    }
}

?>