<?php

namespace XZD\AI\V5\Theme\Adapters;

use XZD\AI\V5\Theme\AdapterBase;

/**
 * 默认适配器 — 通用主题降级方案
 *
 * 所有主题功能标记为不可用，前端显示"需要子比主题"提示。
 * 当没有深度适配器匹配时，自动使用此适配器。
 */
class DefaultAdapter extends AdapterBase
{
    private ?string $themeName = null;

    public function getThemeName(): string
    {
        if ($this->themeName === null) {
            $theme = wp_get_theme();
            $this->themeName = $theme->get('Name') ?: '未知主题';
        }
        return $this->themeName;
    }

    public function getThemeVersion(): string
    {
        $theme = wp_get_theme();
        return $theme->get('Version') ?: '0.0';
    }

    public function getCapabilities(): array
    {
        // 动态检查实际是否有可用功能
        $caps = parent::getCapabilities();

        // 检查是否意外安装了一些 Zibll 函数
        if (function_exists('zibpay_get_user_points')) { $caps['points']['available'] = true; $caps['points']['source'] = 'zibpay_get_user_points'; }
        if (function_exists('zibpay_get_user_balance')) { $caps['balance']['available'] = true; $caps['balance']['source'] = 'zibpay_get_user_balance'; }
        if (function_exists('zib_get_user_vip_level')) { $caps['vip']['available'] = true; $caps['vip']['source'] = 'zib_get_user_vip_level'; }
        if (function_exists('zib_ajax_set_user_ban')) { $caps['blacklist']['available'] = true; $caps['blacklist']['source'] = 'zib_ajax_set_user_ban'; }
        if (function_exists('zib_add_user_medal')) { $caps['medal']['available'] = true; $caps['medal']['source'] = 'zib_add_user_medal'; }
        if (function_exists('zib_send_message')) { $caps['notification']['available'] = true; $caps['notification']['source'] = 'zib_send_message'; }
        if (function_exists('zib_user_is_checkined')) { $caps['checkin']['available'] = true; $caps['checkin']['source'] = 'zib_user_is_checkined'; }
        if (function_exists('zib_invite')) { $caps['invite']['available'] = true; $caps['invite']['source'] = 'zib_invite'; }
        if (function_exists('zib_bbs_get_topic')) { $caps['bbs']['available'] = true; $caps['bbs']['source'] = 'zib_bbs_get_topic'; }

        // 检查订单表
        global $wpdb;
        $orderTable = $wpdb->prefix . 'zibpay_order';
        if ($wpdb->get_var("SHOW TABLES LIKE '{$orderTable}'") === $orderTable) {
            $caps['order']['available'] = true;
            $caps['order']['source'] = 'wp_zibpay_order 表';
        }

        return $caps;
    }
}
