<?php
/**
 * 生成 check_config/*.php（define 拼接，输出格式与历史 dashboard 一致）
 */
class ConfigWriter
{
    /**
     * @param string $checkName 配置名，如 index.php
     * @param array  $data      表单字段
     * @return string PHP 配置源码
     */
    public static function buildDefineSource($checkName, array $data)
    {
        $methods              = $data['methods'];
        $blacklist_group_id   = (int) $data['blacklist_group_id'];
        $whitelist_group_id   = (int) $data['whitelist_group_id'];
        $country              = $data['country'];
        $show_content         = $data['show_content'];
        $DB_fp                = $data['DB_fp'];
        $costm_ip_score       = $data['costm_ip_score'];
        $auto_black           = $data['auto_black'];
        $is_virtual           = $data['is_virtual'];
        $auto_black_times     = $data['auto_black_times'];
        $white_params         = $data['white_params'];
        $iphone_model         = $data['iphone_model'];
        $write_log            = $data['write_log'];
        $zh_on                = $data['zh_on'];
        $os_on                = $data['os_on'];
        $keep_params          = $data['keep_params'];
        $redirect_method      = $data['redirect_method'];
        $DB_zp                = $data['DB_zp'];
        $mobile_on            = $data['mobile_on'];
        $v_referer            = $data['v_referer'];
        $risk_number          = $data['risk_number'];
        $risk_enhanced        = $data['risk_enhanced'];
        $url_args_timeout     = $data['url_args_timeout'];
        $debug_mode           = $data['debug_mode'];
        $ad_fb                = $data['ad_fb'];
        $ad_google            = $data['ad_google'];
        $ad_tiktok            = $data['ad_tiktok'];
        $ad_strict            = $data['ad_strict'];
        $ad_spm_id            = $data['ad_spm_id'];

        // deprecated：业务代码未读取，保留 define 以兼容旧工具链
        $mobile = 0;
        $output = 1;

        return "<?php
		define('SHOW_SITE_MODE_SWITCH', '" . $methods . "' );
		define('SHOW_SITE_IP', '' );
		define('BLACKLIST_GROUP_ID', " . $blacklist_group_id . " );
		define('WHITELIST_GROUP_ID', " . $whitelist_group_id . " );
		define('SHOW_SITE_COUNTRY', '" . $country . "' );
		define('CLOAK_SHOW_CONTENT', '" . $show_content . "');
		define('DB_FP', " . var_export($DB_fp, true) . " );
		define('COSTM_IP_SCORE', '" . $costm_ip_score . "' );
		define('BLACK_LIST', '0' );
		define('AUTO_BLACK', '" . $auto_black . "' );
		define('IS_VIRTUAL', '" . $is_virtual . "' );
		define('AUTO_BLACK_TIMES', '" . $auto_black_times . "' );
		define('WHITE_PARAMS', '" . $white_params . "' );
		define('IPHONE_MODEL', " . $iphone_model . " );

		define('SHOW_SITE_MOBILE', " . $mobile . " );
		define('SHOW_SITE_OUTPUT', " . $output . " );
		define('WRITE_LOG', " . $write_log . " );
		define('CLOAK_ZH_ON', '" . $zh_on . "');
		define('CLOAK_OS_ON', '" . $os_on . "');
		define('KEEP_PARAMS', '" . $keep_params . "');
		define('CLOAK_REDIRECT_METHOD', '" . $redirect_method . "');
		define('DB_ZP', '" . $DB_zp . "');
		define('CLOAK_MOBILE_ON', '" . $mobile_on . "');
		define('CLOAK_V_REFERER', '" . $v_referer . "');
		define('CLOAK_RISK_NUMBER', '" . $risk_number . "');
		define('CLOAK_RISK_ENHANCED', '" . $risk_enhanced . "');
		define('CLOAK_URL_ARGS_TIMEOUT', '" . $url_args_timeout . "');

		define('CLOAK_AD_FB', '" . $ad_fb . "');
		define('CLOAK_AD_GOOGLE', '" . $ad_google . "');
		define('CLOAK_AD_TIKTOK', '" . $ad_tiktok . "');
		define('CLOAK_AD_STRICT', '" . $ad_strict . "');
		define('CLOAK_AD_SPM_ID', '" . $ad_spm_id . "');

		define('CLOAK_DEBUG_MODE', '" . $debug_mode . "');
		?" . ">";
    }

    public static function save($checkName, array $data)
    {
        $str         = self::buildDefineSource($checkName, $data);
        $config_name = str_replace('.php', '', $checkName);
        $config_file = 'check_config/' . $config_name . '_config.php';
        if (!file_exists($config_file)) {
            copy('check_config/index_config.php', $config_file);
        }
        $f = fopen($config_file, 'w');
        rewind($f);
        fwrite($f, $str);
        fclose($f);
    }
}
