<?php

namespace XZD\AI\V5\SEO;

/**
 * SEOMetaBox — 文章编辑页SEO面板
 *
 * 在Gutenberg侧边栏和经典编辑器Metabox中展示AI生成的SEO数据
 * 允许手动编辑TDK、查看SEO评分
 *
 * @since V3.2.0
 */
class SEOMetaBox
{
    public function init(): void
    {
        add_action('add_meta_boxes', [$this, 'register']);
        add_action('save_post', [$this, 'save']);
    }

    public function register(): void
    {
        $screens = ['post'];
        add_meta_box(
            'xzd-seo-meta-box',
            '小栈AI · SEO数据',
            [$this, 'render'],
            $screens,
            'side',
            'default'
        );
    }

    public function render(\WP_Post $post): void
    {
        $postId = $post->ID;
        $seoTitle = get_post_meta($postId, '_xzd_ai_seo_title', true) ?: '';
        $seoDesc  = get_post_meta($postId, '_xzd_ai_seo_description', true) ?: '';
        $seoKeywords = get_post_meta($postId, '_xzd_ai_seo_keywords', true) ?: '';
        $summary  = get_post_meta($postId, '_xzd_ai_summary', true) ?: '';
        $schema   = get_post_meta($postId, '_xzd_ai_schema', true) ?: '';
        $score    = get_post_meta($postId, '_xzd_ai_seo_score', true) ?: '';
        $lastGen  = get_post_meta($postId, '_xzd_ai_seo_generated', true) ?: '';

        wp_nonce_field('xzd_seo_meta_box', 'xzd_seo_nonce');
        ?>
        <div style="margin-bottom:8px">
            <label style="font-weight:600;display:block;margin-bottom:2px">SEO标题</label>
            <input type="text" name="xzd_seo_title" value="<?php echo esc_attr($seoTitle); ?>"
                   style="width:100%;" placeholder="AI未生成">
            <span style="font-size:11px;color:#888">建议15-70字符</span>
        </div>
        <div style="margin-bottom:8px">
            <label style="font-weight:600;display:block;margin-bottom:2px">SEO描述</label>
            <textarea name="xzd_seo_desc" rows="3" style="width:100%;" placeholder="AI未生成"><?php echo esc_textarea($seoDesc); ?></textarea>
            <span style="font-size:11px;color:#888">建议80-160字符</span>
        </div>
        <div style="margin-bottom:8px">
            <label style="font-weight:600;display:block;margin-bottom:2px">关键词</label>
            <input type="text" name="xzd_seo_keywords" value="<?php echo esc_attr($seoKeywords); ?>"
                   style="width:100%;" placeholder="AI未生成">
        </div>
        <?php if ($summary): ?>
        <div style="margin-bottom:8px">
            <label style="font-weight:600;display:block;margin-bottom:2px">AI摘要</label>
            <p style="margin:2px 0;font-size:12px;color:#555"><?php echo esc_html(mb_substr($summary, 0, 200)); ?></p>
        </div>
        <?php endif; ?>
        <?php if ($score): ?>
        <div style="margin-bottom:8px">
            <label style="font-weight:600">SEO评分: <?php echo esc_html($score); ?>/100</label>
        </div>
        <?php endif; ?>
        <?php if ($lastGen): ?>
        <span style="font-size:11px;color:#aaa">上次生成: <?php echo esc_html($lastGen); ?></span>
        <?php endif; ?>
        <?php
    }

    public function save(int $postId): void
    {
        if (!isset($_POST['xzd_seo_nonce']) || !wp_verify_nonce($_POST['xzd_seo_nonce'], 'xzd_seo_meta_box')) return;
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
        if (!current_user_can('edit_post', $postId)) return;

        if (isset($_POST['xzd_seo_title'])) {
            update_post_meta($postId, '_xzd_ai_seo_title', sanitize_text_field($_POST['xzd_seo_title']));
        }
        if (isset($_POST['xzd_seo_desc'])) {
            update_post_meta($postId, '_xzd_ai_seo_description', sanitize_textarea_field($_POST['xzd_seo_desc']));
        }
        if (isset($_POST['xzd_seo_keywords'])) {
            update_post_meta($postId, '_xzd_ai_seo_keywords', sanitize_text_field($_POST['xzd_seo_keywords']));
        }
    }
}
