<?php
/**
 * Simple Feature Descriptions for AutoToolBox (Test Version)
 * Contains basic descriptions without WordPress dependencies
 * 
 * @package AI Auto Tool
 * @since 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Thoát nếu truy cập trực tiếp

class AiAutoTool_Feature_Descriptions {
    
    /**
     * Get feature description by key
     * 
     * @param string $key Feature key
     * @return string HTML description
     */
    public static function get($key) {
        $descriptions = self::get_all_descriptions();
        return isset($descriptions[$key]) ? $descriptions[$key] : '';
    }
    
    /**
     * Get truncated description with "Read more" button
     * 
     * @param string $key Feature key
     * @param int $length Maximum length for truncated text
     * @return string HTML with truncated description and modal trigger
     */
    public static function get_truncated_description($key, $length = 100) {
        $full_description = self::get($key);
        if (empty($full_description)) {
            return '';
        }
        
        // Strip HTML tags for text length calculation
        $plain_text = strip_tags($full_description);
        
        if (strlen($plain_text) <= $length) {
            return $full_description;
        }
        
        // Truncate text
        $truncated_text = substr($plain_text, 0, $length) . '...';
        
        // Generate unique ID for modal
        $modal_id = 'feature-desc-modal-' . $key;
        
        $html = '<div class="feature-description-truncated">';
        $html .= '<span class="truncated-text">' . htmlspecialchars($truncated_text) . '</span>';
        $html .= '<button type="button" class="btn-read-more" data-modal-id="' . htmlspecialchars($modal_id) . '" data-feature-key="' . htmlspecialchars($key) . '">';
        $html .= 'Xem thêm';
        $html .= '</button>';
        $html .= '</div>';
        
        // Add modal HTML
        $html .= '<div id="' . htmlspecialchars($modal_id) . '" class="feature-description-modal" style="display: none;">';
        $html .= '<div class="modal-content">';
        $html .= '<div class="modal-header">';
        $html .= '<h3>' . htmlspecialchars(self::get_feature_title($key)) . '</h3>';
        $html .= '<button type="button" class="modal-close">&times;</button>';
        $html .= '</div>';
        $html .= '<div class="modal-body">';
        $html .= $full_description;
        $html .= '</div>';
        $html .= '</div>';
        $html .= '</div>';
        
        return $html;
    }
    
    /**
     * Get feature title by key
     * 
     * @param string $key Feature key
     * @return string Feature title
     */
    public static function get_feature_title($key) {
        $titles = array(
            'auto_comment' => 'Auto Generate Comments Using AI',
            'canonical' => 'Canonical URL Manager & Auto Post Cleanup',
            'api_manager' => 'API Keys Manager',
            'autoblogging' => 'Autoblogging AI Post Generator',
            'keyword_autopost' => 'Keyword Autopost Generator',
            'suggest_auto' => 'AI Suggest New Post',
            'auto_create_tag' => 'Auto Generate Tags and Meta Description',
            'cloudflare_ai' => 'AI Thumbnail Generator',
            'tool_extra_config' => 'Extra Tools Configuration',
            'prompt_manager' => 'AI Prompt Manager',
            'thumb_fix' => 'Thumbnail 404 Fix',
            'google_index' => 'Google Indexing API',
            'webstories' => 'Web Stories Generator',
            'microsite_backlink' => 'Microsite Auto Backlink',
            'robots_txt' => 'Robots.txt & Ads.txt Manager',
            'auto_footer' => 'Auto Footer Generator',
            'install_plugin' => 'Quick Plugin Installer',
            'random_user' => 'Random User Generator',
            'single_ai' => 'AI Content Editor',
            'post_exporter' => 'Post Exporter & Importer',
            'auto_create_categories' => 'Auto Create Categories',
            'crawlcampaign' => 'Crawl Campaign',
        );
        
        return isset($titles[$key]) ? $titles[$key] : ucwords(str_replace('_', ' ', $key));
    }
    
    /**
     * Get all feature descriptions
     * 
     * @return array Associative array of descriptions
     */
    public static function get_all_descriptions() {
        return array(
            'auto_comment' => self::auto_comment_description(),
            'canonical' => self::canonical_description(),
            'api_manager' => self::api_manager_description(),
            'autoblogging' => self::autoblogging_description(),
            'keyword_autopost' => self::keyword_autopost_description(),
            'suggest_auto' => self::suggest_auto_description(),
            'auto_create_tag' => self::auto_create_tag_description(),
            'cloudflare_ai' => self::cloudflare_ai_description(),
            'tool_extra_config' => self::tool_extra_config_description(),
            'prompt_manager' => self::prompt_manager_description(),
            'thumb_fix' => self::thumb_fix_description(),
            'google_index' => self::google_index_description(),
            'webstories' => self::webstories_description(),
            'microsite_backlink' => self::microsite_backlink_description(),
            'robots_txt' => self::robots_txt_description(),
            'auto_footer' => self::auto_footer_description(),
            'install_plugin' => self::install_plugin_description(),
            'random_user' => self::random_user_description(),
            'single_ai' => self::single_ai_description(),
            'post_exporter' => self::post_exporter_description(),
            'auto_create_categories' => self::auto_create_categories_description(),
            'crawlcampaign' => self::crawlcampaign_description(),
        );
    }
    
    /**
     * Auto Comment Feature Description
     */
    private static function auto_comment_description() {
        $title = 'Auto Generate Comments Using AI';
        $description = 'Automatically generate intelligent and contextual comments for your posts using advanced AI technology.';
        
        $features = array(
            'Generate natural, human-like comments based on post content',
            'Support multiple languages for international websites',
            'Customize comment tone and style',
            'Set automatic scheduling for comment generation',
            'Filter by post type, category, and tags',
        );
        
        $usage = array(
            'Configure AI provider (OpenAI or Gemini) in API Manager',
            'Set the number of comments per post',
            'Choose comment language and tone',
            'Enable automatic mode or manual trigger',
            'Monitor generated comments in the dashboard',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * API Manager Description
     */
    private static function api_manager_description() {
        $title = 'API Keys Manager';
        $description = 'Securely manage your AI service API keys including OpenAI, Gemini, and other providers in one centralized location.';
        
        $features = array(
            'Secure storage for OpenAI API keys',
            'Gemini AI API configuration',
            'Test API connections before saving',
            'Monitor API usage and quotas',
            'Support for multiple AI providers',
            'Encrypted key storage for security',
        );
        
        $usage = array(
            'Navigate to API Manager in settings',
            'Enter your OpenAI API key from platform.openai.com',
            'Add Gemini API key from Google AI Studio',
            'Click "Test Connection" to verify keys',
            'Save settings to activate AI features',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Canonical URL Manager Description
     */
    private static function canonical_description() {
        $title = 'Canonical URL Manager & Auto Post Cleanup';
        $description = 'Manage canonical URLs and automatically clean up old posts to improve SEO and website performance.';
        
        $features = array(
            'Automatic canonical URL generation for all posts',
            'Fix duplicate content issues',
            'Auto-delete posts older than specified days',
            'Bulk canonical URL management',
            'SEO optimization for better search rankings',
            'Customizable cleanup rules and filters',
        );
        
        $usage = array(
            'Enable canonical URL generation in settings',
            'Set auto-cleanup threshold (days)',
            'Choose which post types to manage',
            'Configure exclusion rules for important posts',
            'Monitor cleanup activity in logs',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Microsite Backlink Description
     */
    private static function microsite_backlink_description() {
        $title = 'Microsite Auto Backlink';
        $description = 'Manage microsite network and automatically create backlinks between sites in your network for improved SEO and link building.';
        
        $features = array(
            'Microsite network management',
            'Automatic cross-site linking',
            'SEO-optimized backlink structure',
            'Link diversity control',
            'Network analytics dashboard',
        );
        
        $usage = array(
            'Add microsites to network',
            'Configure linking rules',
            'Set link insertion frequency',
            'Monitor backlink network health',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Google Index Description
     */
    private static function google_index_description() {
        $title = 'Google Indexing API';
        $description = 'Automatically submit your posts to Google for indexing using Google Indexing API for faster search visibility.';
        
        $features = array(
            'Direct submission to Google Index',
            'Automatic indexing on post publish',
            'Bulk URL submission',
            'Index status monitoring',
            'Google Search Console integration',
        );
        
        $usage = array(
            'Configure Google API credentials',
            'Enable automatic indexing',
            'Submit existing posts for indexing',
            'Monitor indexing status',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Post Exporter Description
     */
    private static function post_exporter_description() {
        $title = 'Post Exporter & Importer';
        $description = 'Export and import posts from URL lists. Perfect for content migration and bulk operations.';
        
        $features = array(
            'Export posts to various formats',
            'Import from URL list',
            'Bulk content migration',
            'Preserve metadata and formatting',
            'Schedule imports',
        );
        
        $usage = array(
            'Prepare URL list for import',
            'Configure import settings',
            'Map fields correctly',
            'Run import process',
            'Verify imported content',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Keyword Autopost Description
     */
    private static function keyword_autopost_description() {
        $title = 'Keyword Autopost Generator';
        $description = 'Automatically generate and publish SEO-optimized articles from keywords. The system processes keywords from a queue and creates complete articles with AI-generated content and images.';
        
        $features = array(
            'Keyword-based article generation',
            'SEO optimization for target keywords',
            'Automatic research and content creation',
            'Image generation matching keywords',
            'Queue system for bulk processing',
            'Performance tracking and analytics',
        );
        
        $usage = array(
            'Import keywords list (CSV or manual)',
            'Set keyword processing schedule',
            'Configure article template and structure',
            'Enable automatic publishing',
            'Monitor keyword queue and status',
            'Review generated articles before publishing',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Single AI Editor Description
     */
    private static function single_ai_description() {
        $title = 'AI Content Editor';
        $description = 'Intelligent editor with image search, real-time content writing in multiple languages, and AI-powered assistance.';
        
        $features = array(
            'AI-powered content suggestions',
            'Built-in image search',
            'Multi-language writing support',
            'Real-time AI assistance',
            'Content optimization tips',
        );
        
        $usage = array(
            'Open post editor',
            'Use AI assistant panel',
            'Search and insert images',
            'Get content suggestions',
            'Optimize with AI recommendations',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Thumbnail Fix Description
     */
    private static function thumb_fix_description() {
        $title = 'Thumbnail 404 Fix';
        $description = 'Automatically detect and fix broken thumbnail images (404 errors) across your website.';
        
        $features = array(
            'Automatic 404 thumbnail detection',
            'Replace broken images automatically',
            'Bulk image validation',
            'Image health monitoring',
        );
        
        $usage = array(
            'Enable automatic thumbnail fix',
            'Run image health scan',
            'Configure replacement image settings',
            'Monitor fix activity in logs',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Auto Create Tag Description
     */
    private static function auto_create_tag_description() {
        $title = 'Auto Generate Tags and Meta Description';
        $description = 'Automatically create relevant tags and SEO meta descriptions for your posts using AI analysis of content.';
        
        $features = array(
            'AI-powered tag generation from content',
            'SEO-optimized meta descriptions',
            'Automatic keyword extraction',
            'Bulk tag processing for existing posts',
            'Customizable tag rules and filters',
        );
        
        $usage = array(
            'Enable auto tag generation in settings',
            'Set maximum number of tags per post',
            'Choose tag generation mode (automatic or manual)',
            'Process existing posts in bulk',
            'Review and edit generated tags as needed',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Cloudflare AI Description
     */
    private static function cloudflare_ai_description() {
        $title = 'AI Thumbnail Generator';
        $description = 'Automatically find posts without featured images and generate suitable thumbnails using AI services like Cloudflare Workers AI and Hugging Face.';
        
        $features = array(
            'Automatic thumbnail generation for posts',
            'Multiple AI image providers support',
            'Smart image selection based on content',
            'Batch processing for multiple posts',
            'Image optimization and resizing',
        );
        
        $usage = array(
            'Configure AI image provider credentials',
            'Enable automatic thumbnail generation',
            'Set image style and dimensions',
            'Run scan to find posts without thumbnails',
            'Review and approve generated images',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Suggest Auto Description
     */
    private static function suggest_auto_description() {
        $title = 'AI Suggest New Post';
        $description = 'AI analyzes your existing content to automatically suggest relevant new post topics. Scans 20 random titles and generates related topic ideas that match your website\'s content strategy.';
        
        $features = array(
            'Intelligent topic suggestion based on existing content',
            'Analyzes patterns from your posts',
            'Generates relevant topic ideas',
            'Saves suggestions as drafts',
            'Integrates with Autoblogging for automatic publishing',
            'Works best with 100+ existing posts',
        );
        
        $usage = array(
            'Ensure you have at least 20 published posts',
            'Enable AI Suggest New Post feature',
            'Set suggestion frequency (daily, weekly)',
            'Review suggested topics in drafts',
            'Combine with Autoblogging for full automation',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Prompt Manager Description
     */
    private static function prompt_manager_description() {
        $title = 'AI Prompt Manager';
        $description = 'Create, manage, and organize custom AI prompts for consistent content generation across your website.';
        
        $features = array(
            'Custom prompt templates creation',
            'Prompt library management',
            'Reusable prompt snippets',
            'Category-based organization',
            'Share prompts across features',
        );
        
        $usage = array(
            'Create new prompt templates',
            'Organize prompts by category',
            'Test prompts with AI providers',
            'Apply prompts to content generation features',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Web Stories Description
     */
    private static function webstories_description() {
        $title = 'Web Stories Generator';
        $description = 'Create engaging Google Web Stories from your posts. Stories are accessible at domain.com/aiwebstories/';
        
        $features = array(
            'Automatic Web Stories creation',
            'Mobile-optimized stories',
            'SEO-friendly story structure',
            'Custom story URL structure',
            'Analytics integration',
        );
        
        $usage = array(
            'Enable Web Stories feature',
            'Configure story template',
            'Set automatic story generation',
            'Access stories at /aiwebstories/',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Install Plugin Description
     */
    private static function install_plugin_description() {
        $title = 'Quick Plugin Installer';
        $description = 'Quickly install essential SEO and MMO plugins with one click.';
        
        $features = array(
            'One-click plugin installation',
            'Curated plugin collection',
            'SEO essentials bundle',
            'Automatic updates',
        );
        
        $usage = array(
            'Browse recommended plugins',
            'Click install on desired plugins',
            'Activate and configure',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Autoblogging Feature Description
     */
    private static function autoblogging_description() {
        $title = 'Autoblogging AI Post Generator';
        $description = 'Automatically create and publish blog posts using AI. Generate content from titles, add images, and schedule posts for publication.';
        
        $features = array(
            'AI-powered content generation from post titles',
            'Automatic image search and insertion',
            'Scheduled post publishing with cron jobs',
            'Multi-language content support',
            'Custom prompt templates for content style',
            'Usage tracking and quota management',
            'Draft post management and monitoring',
            'Telegram notifications for published posts',
        );
        
        $usage = array(
            'Enter multiple post titles in the input field',
            'Select category and tags for posts',
            'Choose language for AI content generation',
            'Configure publishing schedule and frequency',
            'Enable automatic image insertion (optional)',
            'Monitor draft posts and publishing status',
            'Review logs for content generation activity',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Tool Extra Config Description
     */
    private static function tool_extra_config_description() {
        $title = 'Extra Tools Configuration';
        $description = 'Configure additional tools and utilities for enhanced website functionality.';
        
        $features = array(
            'Additional tool configurations',
            'Custom utility settings',
            'Extended functionality options',
            'Tool integration management',
        );
        
        $usage = array(
            'Access extra tools configuration',
            'Configure additional utilities',
            'Set up tool integrations',
            'Manage extended functionality',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Robots.txt Manager Description
     */
    private static function robots_txt_description() {
        $title = 'Robots.txt & Ads.txt Manager';
        $description = 'Manage robots.txt and ads.txt files for better SEO and ad management.';
        
        $features = array(
            'Robots.txt file management',
            'Ads.txt file configuration',
            'SEO optimization tools',
            'Crawl control settings',
        );
        
        $usage = array(
            'Configure robots.txt settings',
            'Set up ads.txt file',
            'Manage crawl permissions',
            'Optimize SEO settings',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Auto Footer Description
     */
    private static function auto_footer_description() {
        $title = 'Auto Footer Generator';
        $description = 'Automatically generate and manage footer content for your website.';
        
        $features = array(
            'Automatic footer generation',
            'Footer content management',
            'Customizable footer templates',
            'Dynamic footer updates',
        );
        
        $usage = array(
            'Configure footer settings',
            'Choose footer template',
            'Set automatic updates',
            'Customize footer content',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Random User Generator Description
     */
    private static function random_user_description() {
        $title = 'Random User Generator';
        $description = 'Generate random user profiles and accounts for testing and development purposes.';
        
        $features = array(
            'Random user profile generation',
            'Test account creation',
            'Development user management',
            'Bulk user creation',
        );
        
        $usage = array(
            'Configure user generation settings',
            'Set user profile parameters',
            'Generate test users',
            'Manage generated accounts',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Auto Create Categories Description
     */
    private static function auto_create_categories_description() {
        $title = 'Auto Create Categories';
        $description = 'Tự động tạo danh sách category dạng cây thư mục bằng AI. Phân tích chủ đề website và tạo ra cấu trúc category tối ưu SEO theo topic cluster.';
        
        $features = array(
            'Tự động phân tích chủ đề website và tạo cấu trúc category',
            'Tạo danh sách category dạng cây thư mục (Main Category > Sub-category > Sub-category cấp 2)',
            'Tối ưu SEO theo cụm chủ đề (topic cluster)',
            'Bao quát toàn bộ chủ đề, không trùng lặp',
            'Cho phép chỉnh sửa và xóa category trước khi thêm vào WordPress',
            'Tự động thêm categories vào WordPress với progress tracking',
            'Hỗ trợ trên trang Categories và menu riêng',
        );
        
        $usage = array(
            'Vào trang Categories: /wp-admin/edit-tags.php?taxonomy=category',
            'Click nút "AI Auto Create Categories"',
            'Nhập chủ đề website (ví dụ: "review xe hơi", "công nghệ", "du lịch")',
            'AI sẽ tự động tạo danh sách category dạng cây',
            'Xem và chỉnh sửa/xóa categories nếu cần',
            'Click "Thêm tất cả Categories vào WordPress" để import',
            'Hoặc vào menu "AI Auto Tool" > "Auto Create Categories"',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Crawl Campaign Description
     */
    private static function crawlcampaign_description() {
        $title = 'Crawl Campaign';
        $description = 'Tự động crawl và đăng bài từ nhiều nguồn (RSS Feeds, YouTube, Amazon, Facebook, Instagram, Twitter, etc.). Quản lý campaigns để tự động thu thập và đăng nội dung từ các nguồn khác nhau.';
        
        $features = array(
            'Quản lý campaigns: tạo, chỉnh sửa, xóa campaigns',
            'Hỗ trợ nhiều nguồn: RSS Feeds, YouTube, Amazon, Facebook, Instagram, Twitter',
            'Tự động phát hiện và tránh duplicate content',
            'Đăng bài theo lịch tự động (scheduled posting)',
            'Template system với các tags: [original_title], [matched_content], [source_link]',
            'Tự động gán category và tags',
            'Logging system để theo dõi hoạt động',
            'Cron system để chạy campaigns tự động',
        );
        
        $usage = array(
            'Kích hoạt feature trong Integrations page',
            'Tạo Campaign mới: Crawl Campaigns > Add New',
            'Cấu hình Campaign Type (Feeds, YouTube, etc.)',
            'Nhập RSS Feed URLs hoặc Keywords tùy theo loại campaign',
            'Cấu hình Post Settings (Title, Content, Status, Category)',
            'Publish campaign để bắt đầu chạy',
            'Cron sẽ tự động chạy campaigns theo lịch đã cấu hình',
        );
        
        return self::format_description($title, $description, $features, $usage);
    }
    
    /**
     * Format description into HTML
     * 
     * @param string $title Feature title
     * @param string $description Short description
     * @param array $features List of features
     * @param array $usage Usage steps
     * @return string Formatted HTML
     */
    private static function format_description($title, $description, $features = array(), $usage = array()) {
        $html = '<div class="aiautotool-feature-description">';
        
        // Description
        if (!empty($description)) {
            $html .= '<p class="description">' . htmlspecialchars($description) . '</p>';
        }
        
        // Features section
        if (!empty($features)) {
            $html .= '<h4>Key Features:</h4>';
            $html .= '<ul class="feature-list">';
            foreach ($features as $feature) {
                $html .= '<li><strong>✓</strong> ' . htmlspecialchars($feature) . '</li>';
            }
            $html .= '</ul>';
        }
        
        // Usage section
        if (!empty($usage)) {
            $html .= '<h4>How to Use:</h4>';
            $html .= '<ol class="usage-steps">';
            foreach ($usage as $step) {
                $html .= '<li>' . htmlspecialchars($step) . '</li>';
            }
            $html .= '</ol>';
        }
        
        $html .= '</div>';
        
        return $html;
    }
}
