<?php
declare(strict_types=1);


namespace ProRank\SEO\Core\RestApi\Controllers;

use ProRank\SEO\Core\RestApi\BaseController;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}




class DashboardController extends BaseController {
    
    public function register_routes() {
        register_rest_route($this->namespace, '/dashboard/stats', [
            'methods' => \WP_REST_Server::READABLE,
            'callback' => [$this, 'get_stats'],
            'permission_callback' => [$this, 'check_admin_permission'],
        ]);
    }
    
    public function get_stats($request) {
        try {
            // Get various stats from services
            $modules_manager = $this->container->get('modules');
            $notifications_manager = $this->container->get('notifications');
            
            $stats = [
                'site_health' => $this->get_site_health_score(),
                'active_modules' => count($modules_manager->get_active_modules()),
                'total_modules' => count($modules_manager->get_modules()),
                'notifications' => [
                    'unread' => $notifications_manager->get_unread_count(),
                    'total' => $notifications_manager->get_total_count(),
                ],
                'quick_stats' => [
                    'indexed_pages' => $this->get_indexed_pages_count(),
                    'schema_types' => $this->get_schema_types_count(),
                    'broken_links' => 0, // Placeholder
                ],
            ];
            
            return $this->success($stats);
        } catch (\Exception $e) {
            return $this->error($e->getMessage(), 'dashboard_error', 500);
        }
    }
    
    private function get_site_health_score() {
        // Calculate a basic site health score based on various factors
        $score = 100;
        
        // Check if robots.txt is accessible
        $robots_url = home_url('/robots.txt');
        $response = wp_remote_get($robots_url);
        if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
            $score -= 10;
        }
        
        // Check if XML sitemap is enabled
        $sitemap_url = home_url('/sitemap.xml');
        $response = wp_remote_get($sitemap_url);
        if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
            $score -= 15;
        }
        
        // Check if SEO meta tags are properly configured
        $settings = get_option('prorank_settings', []);
        if (empty($settings['site_title']) && empty($settings['site_description'])) {
            $score -= 20;
        }
        
        return max(0, $score);
    }
    
    private function get_indexed_pages_count() {
        global $wpdb;
        
        // Count published posts and pages
        $count = $wpdb->get_var("
            SELECT COUNT(*) 
            FROM {$wpdb->posts} 
            WHERE post_status = 'publish' 
            AND post_type IN ('post', 'page')
        ");
        
        return intval($count);
    }
    
    private function get_schema_types_count() {
        // Placeholder - count different schema types in use
        return 3;
    }
}
