<?php
/**
 * ==========================
 * 🔐 IGNY8 FILE RULE HEADER
 * ==========================
 * @file         : kpi-config.php
 * @location     : /modules/config/kpi-config.php
 * @type         : Config Array
 * @scope        : Global
 * @allowed      : KPI definitions, metrics configurations
 * @reusability  : Globally Reusable
 * @notes        : Central KPI configuration for all modules
 */

// Prevent direct access only if not being included
if (!defined('ABSPATH') && !defined('IGNY8_INCLUDE_CONFIG')) {
    exit;
}

return [
    // Keywords KPIs
    'planner_keywords' => [
        'total_keywords' => [
            'label' => 'Total Keywords',
            'query' => 'SELECT COUNT(*) as count FROM {table_name}',
            'color' => 'blue'
        ],
        'mapped_keywords' => [
            'label' => 'Mapped',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE status = "mapped"',
            'color' => 'green'
        ],
        'unmapped_keywords' => [
            'label' => 'Unmapped',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE status = "unmapped"',
            'color' => 'amber'
        ],
        'total_volume' => [
            'label' => 'Total Volume',
            'query' => 'SELECT SUM(search_volume) as count FROM {table_name}',
            'color' => 'purple'
        ],
        'avg_difficulty' => [
            'label' => 'Avg Difficulty',
            'query' => 'SELECT ROUND(AVG(difficulty)) as count FROM {table_name}',
            'color' => 'blue'
        ],
        'high_volume_keywords' => [
            'label' => 'High Volume (>1K)',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE search_volume > 1000',
            'color' => 'green'
        ]
    ],
    
    // Clusters KPIs
    'planner_clusters' => [
        'total_clusters' => [
            'label' => 'Clusters',
            'query' => 'SELECT COUNT(*) as count FROM {table_name}',
            'color' => ''
        ],
        'total_volume' => [
            'label' => 'Volume',
            'query' => 'SELECT SUM(total_volume) as count FROM {table_name}',
            'color' => 'green'
        ],
        'total_keywords' => [
            'label' => 'Keywords',
            'query' => 'SELECT SUM(keyword_count) as count FROM {table_name}',
            'color' => 'amber'
        ],
        'mapped_clusters' => [
            'label' => 'Mapped',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE mapped_pages_count > 0',
            'color' => 'purple'
        ],
        'avg_keywords_per_cluster' => [
            'label' => 'Avg Keywords/Cluster',
            'query' => 'SELECT ROUND(AVG(keyword_count)) as count FROM {table_name}',
            'color' => 'blue'
        ],
        'high_volume_clusters' => [
            'label' => 'High Volume Clusters',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE total_volume > 10000',
            'color' => 'green'
        ]
    ],
    
    // Ideas KPIs
    'planner_ideas' => [
        'total_ideas' => [
            'label' => 'Ideas',
            'query' => 'SELECT COUNT(*) as count FROM {table_name}',
            'color' => ''
        ],
        'new_ideas' => [
            'label' => 'New',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE status = "new"',
            'color' => 'green'
        ],
        'scheduled_ideas' => [
            'label' => 'Scheduled',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE status = "scheduled"',
            'color' => 'amber'
        ],
        'published_ideas' => [
            'label' => 'Published',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE status = "published"',
            'color' => 'purple'
        ],
        'ai_generated' => [
            'label' => 'AI Generated',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE source = "AI"',
            'color' => 'blue'
        ],
        'recent_ideas' => [
            'label' => 'This Week',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)',
            'color' => 'green'
        ]
    ],
    
    // Planner Home KPIs (Main Dashboard)
    'planner_home' => [
        'total_keywords' => [
            'label' => 'Keywords',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_keywords',
            'color' => ''
        ],
        'total_volume' => [
            'label' => 'Volume',
            'query' => 'SELECT SUM(search_volume) as count FROM {prefix}igny8_keywords',
            'color' => 'green'
        ],
        'total_clusters' => [
            'label' => 'Clusters',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_clusters',
            'color' => 'amber'
        ],
        'total_ideas' => [
            'label' => 'Ideas',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_content_ideas',
            'color' => 'purple'
        ],
        'high_volume_keywords' => [
            'label' => 'High Vol (>1K)',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_keywords WHERE search_volume > 1000',
            'color' => 'blue'
        ],
        'recent_ideas' => [
            'label' => 'This Week',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_content_ideas WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)',
            'color' => 'teal'
        ],
        'mapped_keywords' => [
            'label' => 'Mapped Keywords',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_keywords WHERE status = "mapped"',
            'color' => 'green'
        ],
        'unmapped_keywords' => [
            'label' => 'Unmapped Keywords',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_keywords WHERE status = "unmapped"',
            'color' => 'amber'
        ],
        'clusters_with_ideas' => [
            'label' => 'Clusters With Ideas',
            'query' => 'SELECT COUNT(DISTINCT keyword_cluster_id) as count FROM {prefix}igny8_content_ideas WHERE keyword_cluster_id IS NOT NULL',
            'color' => 'green'
        ],
        'queued_ideas' => [
            'label' => 'Queued Ideas',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_content_ideas WHERE status = "scheduled"',
            'color' => 'amber'
        ]
    ],
    
    // Writer Home KPIs (Main Dashboard)
    'writer_home' => [
        'queued_tasks' => [
            'label' => 'Queued Tasks',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_tasks WHERE status IN ("queued", "in_progress")',
            'color' => 'blue'
        ],
        'draft_tasks' => [
            'label' => 'Drafts',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_tasks WHERE status IN ("draft", "review")',
            'color' => 'amber'
        ],
        'published_tasks' => [
            'label' => 'Published',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_tasks WHERE status = "published"',
            'color' => 'green'
        ],
        'total_tasks' => [
            'label' => 'Total Tasks',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_tasks',
            'color' => ''
        ]
    ],

    // Writer Tasks KPIs
    'writer_tasks' => [
        'total_ideas' => [
            'label' => 'Ideas',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_content_ideas',
            'color' => ''
        ],
        'content_scheduled' => [
            'label' => 'Content Scheduled',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_tasks WHERE status IN ("queued", "in_progress")',
            'color' => 'green'
        ],
        'written' => [
            'label' => 'Written',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_tasks WHERE status IN ("draft", "review")',
            'color' => 'amber'
        ],
        'published' => [
            'label' => 'Published',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_tasks WHERE status = "published"',
            'color' => 'purple'
        ]
    ],

    // Writer Drafts KPIs
    'writer_drafts' => [
        'total_ideas' => [
            'label' => 'Ideas',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_content_ideas',
            'color' => ''
        ],
        'content_scheduled' => [
            'label' => 'Content Scheduled',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_tasks WHERE status IN ("queued", "in_progress")',
            'color' => 'green'
        ],
        'written' => [
            'label' => 'Written',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_tasks WHERE status IN ("draft", "review")',
            'color' => 'amber'
        ],
        'published' => [
            'label' => 'Published',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_tasks WHERE status = "published"',
            'color' => 'purple'
        ]
    ],

    // Writer Published KPIs
    'writer_published' => [
        'total_ideas' => [
            'label' => 'Ideas',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_content_ideas',
            'color' => ''
        ],
        'content_scheduled' => [
            'label' => 'Content Scheduled',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_tasks WHERE status IN ("queued", "in_progress")',
            'color' => 'green'
        ],
        'written' => [
            'label' => 'Written',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_tasks WHERE status IN ("draft", "review")',
            'color' => 'amber'
        ],
        'published' => [
            'label' => 'Published',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_tasks WHERE status = "published"',
            'color' => 'purple'
        ]
    ],

    
    // Optimizer Audits KPIs
    'optimizer_audits' => [
        'total_audits' => [
            'label' => 'Total Audits',
            'query' => 'SELECT COUNT(*) as count FROM {table_name}',
            'color' => 'blue'
        ],
        'completed_audits' => [
            'label' => 'Completed',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE audit_status = "completed"',
            'color' => 'green'
        ],
        'pending_audits' => [
            'label' => 'Pending',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE audit_status = "pending"',
            'color' => 'amber'
        ],
        'avg_seo_score' => [
            'label' => 'Avg SEO Score',
            'query' => 'SELECT ROUND(AVG(seo_score)) as count FROM {table_name} WHERE audit_status = "completed"',
            'color' => 'purple'
        ],
        'total_issues' => [
            'label' => 'Total Issues',
            'query' => 'SELECT SUM(issues_found) as count FROM {table_name} WHERE audit_status = "completed"',
            'color' => 'blue'
        ],
        'recent_audits' => [
            'label' => 'This Week',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)',
            'color' => 'green'
        ]
    ],
    
    // Writer Templates KPIs
    'writer_templates' => [
        'total_templates' => [
            'label' => 'Total Templates',
            'query' => 'SELECT COUNT(*) as count FROM {table_name}',
            'color' => 'blue'
        ],
        'active_templates' => [
            'label' => 'Active',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE is_active = 1',
            'color' => 'green'
        ],
        'draft_templates' => [
            'label' => 'Draft',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE is_active = 0',
            'color' => 'gray'
        ],
        'content_templates' => [
            'label' => 'Blog Templates',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE prompt_type = "content"',
            'color' => 'blue'
        ],
        'product_templates' => [
            'label' => 'Product Templates',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE prompt_type = "generation"',
            'color' => 'green'
        ],
        'popular_templates' => [
            'label' => 'Popular (>10 uses)',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE usage_count > 10',
            'color' => 'purple'
        ]
    ],
    
    
    // Optimizer Suggestions KPIs
    'optimizer_suggestions' => [
        'total_suggestions' => [
            'label' => 'Total Suggestions',
            'query' => 'SELECT COUNT(*) as count FROM {table_name}',
            'color' => 'blue'
        ],
        'implemented' => [
            'label' => 'Implemented',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE status = "implemented"',
            'color' => 'green'
        ],
        'pending' => [
            'label' => 'Pending',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE status = "pending"',
            'color' => 'amber'
        ],
        'high_impact' => [
            'label' => 'High Impact',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE impact_level = "high"',
            'color' => 'purple'
        ],
        'avg_improvement' => [
            'label' => 'Avg Improvement',
            'query' => 'SELECT ROUND(AVG(improvement_score)) as count FROM {table_name} WHERE status = "implemented"',
            'color' => 'blue'
        ],
        'recent_suggestions' => [
            'label' => 'This Week',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)',
            'color' => 'green'
        ]
    ],
    
    // Linker Backlinks KPIs
    'linker_backlinks' => [
        'total_backlinks' => [
            'label' => 'Total Backlinks',
            'query' => 'SELECT COUNT(*) as count FROM {table_name}',
            'color' => 'blue'
        ],
        'active_backlinks' => [
            'label' => 'Active',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE status = "active"',
            'color' => 'green'
        ],
        'lost_backlinks' => [
            'label' => 'Lost',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE status = "lost"',
            'color' => 'amber'
        ],
        'avg_domain_authority' => [
            'label' => 'Avg DA',
            'query' => 'SELECT ROUND(AVG(domain_authority)) as count FROM {table_name} WHERE status = "active"',
            'color' => 'purple'
        ],
        'dofollow_links' => [
            'label' => 'DoFollow Links',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE link_type = "dofollow" AND status = "active"',
            'color' => 'blue'
        ],
        'recent_backlinks' => [
            'label' => 'This Week',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)',
            'color' => 'green'
        ]
    ],
    
    // Linker Campaigns KPIs
    'linker_campaigns' => [
        'total_campaigns' => [
            'label' => 'Total Campaigns',
            'query' => 'SELECT COUNT(*) as count FROM {table_name}',
            'color' => 'blue'
        ],
        'active_campaigns' => [
            'label' => 'Active',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE status = "active"',
            'color' => 'green'
        ],
        'completed_campaigns' => [
            'label' => 'Completed',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE status = "completed"',
            'color' => 'amber'
        ],
        'avg_links_per_campaign' => [
            'label' => 'Avg Links/Campaign',
            'query' => 'SELECT ROUND(AVG(links_acquired)) as count FROM {table_name}',
            'color' => 'purple'
        ],
        'success_rate' => [
            'label' => 'Success Rate %',
            'query' => 'SELECT ROUND((COUNT(CASE WHEN status = "completed" THEN 1 END) * 100.0 / COUNT(*))) as count FROM {table_name}',
            'color' => 'blue'
        ],
        'recent_campaigns' => [
            'label' => 'This Week',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)',
            'color' => 'green'
        ]
    ],
    
    // Personalize Rewrites KPIs
    'personalize_rewrites' => [
        'total_rewrites' => [
            'label' => 'Total Variations',
            'query' => 'SELECT COUNT(*) as count FROM {table_name}',
            'color' => 'blue'
        ],
        'this_month_rewrites' => [
            'label' => 'This Month',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE created_at >= DATE_FORMAT(NOW(), "%Y-%m-01")',
            'color' => 'amber'
        ],
        'total_ai_sessions' => [
            'label' => 'AI Sessions',
            'query' => 'SELECT COUNT(*) as count FROM {prefix}igny8_logs WHERE log_type = "field_detection" OR log_type = "content_generation"',
            'color' => 'green'
        ],
        'avg_sessions_per_rewrite' => [
            'label' => 'Avg Sessions/Rewrite',
            'query' => 'SELECT ROUND((SELECT COUNT(*) FROM {prefix}igny8_logs WHERE log_type = "field_detection" OR log_type = "content_generation") / GREATEST(COUNT(*), 1), 1) as count FROM {table_name}',
            'color' => 'purple'
        ],
        'recent_rewrites' => [
            'label' => 'This Week',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)',
            'color' => 'green'
        ],
        'successful_rewrites' => [
            'label' => 'Successful',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE status = "completed"',
            'color' => 'blue'
        ]
    ],
    
    // Personalize Tones KPIs
    'personalize_tones' => [
        'total_tones' => [
            'label' => 'Total Tones',
            'query' => 'SELECT COUNT(*) as count FROM {table_name}',
            'color' => 'blue'
        ],
        'active_tones' => [
            'label' => 'Active',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE status = "active"',
            'color' => 'green'
        ],
        'custom_tones' => [
            'label' => 'Custom',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE tone_type = "custom"',
            'color' => 'amber'
        ],
        'avg_usage_frequency' => [
            'label' => 'Avg Usage',
            'query' => 'SELECT ROUND(AVG(usage_count)) as count FROM {table_name}',
            'color' => 'purple'
        ],
        'popular_tones' => [
            'label' => 'Popular (>50 uses)',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE usage_count > 50',
            'color' => 'blue'
        ],
        'recent_tones' => [
            'label' => 'This Week',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)',
            'color' => 'green'
        ]
    ],
    
    // Personalization Data KPIs
    'personalize_data' => [
        'total_data_entries' => [
            'label' => 'Total Data Entries',
            'query' => 'SELECT COUNT(*) as count FROM {table_name}',
            'color' => 'blue'
        ],
        'personalization_data' => [
            'label' => 'Personalization Data',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE data_type = "personalization"',
            'color' => 'green'
        ],
        'field_data' => [
            'label' => 'Field Data',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE data_type = "fields"',
            'color' => 'amber'
        ],
        'recent_entries' => [
            'label' => 'Recent (7 days)',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)',
            'color' => 'purple'
        ],
        'active_entries' => [
            'label' => 'Active',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE status = "active"',
            'color' => 'green'
        ],
        'avg_usage_per_entry' => [
            'label' => 'Avg Usage/Entry',
            'query' => 'SELECT ROUND(AVG(usage_count)) as count FROM {table_name}',
            'color' => 'blue'
        ]
    ],
    
    // Personalization Variations KPIs
    'personalize_variations' => [
        'total_variations' => [
            'label' => 'Total Variations',
            'query' => 'SELECT COUNT(*) as count FROM {table_name}',
            'color' => 'blue'
        ],
        'unique_posts' => [
            'label' => 'Unique Posts',
            'query' => 'SELECT COUNT(DISTINCT post_id) as count FROM {table_name}',
            'color' => 'green'
        ],
        'recent_variations' => [
            'label' => 'Recent (7 days)',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)',
            'color' => 'amber'
        ],
        'avg_variations_per_post' => [
            'label' => 'Avg Variations/Post',
            'query' => 'SELECT ROUND(COUNT(*) / COUNT(DISTINCT post_id), 2) as count FROM {table_name}',
            'color' => 'purple'
        ],
        'published_variations' => [
            'label' => 'Published',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE status = "published"',
            'color' => 'green'
        ],
        'high_performing_variations' => [
            'label' => 'High Performing',
            'query' => 'SELECT COUNT(*) as count FROM {table_name} WHERE performance_score > 80',
            'color' => 'blue'
        ]
    ]
];

