<?php

namespace FleekDash\Services;

use FleekDash\Services\GrowthAchievements\GrowthAchievementsEngineCore;
use FleekDash\Services\GrowthAchievements\GrowthAchievementsCalculator;
use FleekDash\Services\GrowthAchievements\GrowthAchievementsDashboard;

/**
 * Site Growth Achievements Engine (V2.2 Refactored)
 *
 * Facade/Wrapper for the Growth Achievements system
 * Delegates to specialized services for calculations, dashboard, and core logic
 *
* V2.2 REFACTORED:
 * - Split into 3 specialized services (Calculator, Dashboard, Core)
 * - All Eloquent patterns replaced with WordPress-native $wpdb
 * - File size reduced from 752 to ~100 lines
 * - Better separation of concerns
 * - Improved testability
 *
 * @package FleekDash\Services
 * @since V2.2
 */
class SiteGrowthAchievementsEngine
{
    /**
     * Auto-create achievements for all active events
     * Delegates to Core service
     *
     * @return array Summary of created achievements
     */
    public static function autoCreateAchievementsForAllEvents()
    {
        return GrowthAchievementsEngineCore::autoCreateAchievementsForAllEvents();
    }

    /**
     * Process monthly achievements check
     * Delegates to Core service
     *
     * @param string|null $month_period Specific month to check (YYYY-MM-01) or null for current
     * @return array Summary of achievements processed
     */
    public static function processMonthlyAchievements($month_period = null)
    {
        return GrowthAchievementsEngineCore::processMonthlyAchievements($month_period);
    }

    /**
     * Calculate monthly value for specific event and configuration
     * Delegates to Calculator service
     *
     * @param string $event_type Event type
     * @param array $event_config Event configuration
     * @param string $month_start Start of month (YYYY-MM-DD HH:MM:SS)
     * @param string $month_end End of month (YYYY-MM-DD HH:MM:SS)
     * @return int Monthly count for this event+config
     */
    public static function calculateMonthlyValue($event_type, $event_config, $month_start, $month_end)
    {
        return GrowthAchievementsCalculator::calculateMonthlyValue(
            $event_type,
            $event_config,
            $month_start,
            $month_end
        );
    }

    /**
     * Hook into event creation to auto-create achievements
     * Delegates to Core service
     *
     * @param string $event_type Event type
     * @param array $event_config Event configuration
     * @return array|false Created achievement or false
     */
    public static function onEventCreated($event_type, $event_config = [])
    {
        return GrowthAchievementsEngineCore::onEventCreated($event_type, $event_config);
    }

    /**
     * Process achievement updates when events occur
     * Delegates to Core service
     *
     * @param string $event_type Event type that occurred
     * @param array $event_config Event configuration
     * @return array Summary of achievements affected
     */
    public static function onEventOccurred($event_type, $event_config = [])
    {
        return GrowthAchievementsEngineCore::onEventOccurred($event_type, $event_config);
    }

    /**
     * Get achievements dashboard data with caching
     * Delegates to Dashboard service
     *
     * @return array Comprehensive dashboard data
     */
    public static function getDashboardData()
    {
        return GrowthAchievementsDashboard::getDashboardData();
    }

    /**
     * Get recent achievements with rich data
     * Delegates to Dashboard service
     *
     * @param int $days Number of days to look back
     * @param int $limit Maximum number of achievements
     * @return array Recent achievements
     */
    public static function getRecentAchievements($days = 7, $limit = 5)
    {
        return GrowthAchievementsDashboard::getRecentAchievements($days, $limit);
    }

    /**
     * Force recalculation of all achievements (maintenance function)
     * Delegates to Core service
     *
     * @return array Summary of recalculation results
     */
    public static function recalculateAllAchievements()
    {
        return GrowthAchievementsEngineCore::recalculateAllAchievements();
    }

    /**
     * Bootstrap achievements system
     * Delegates to Core service
     *
     * @return array Bootstrap results
     */
    public static function bootstrap()
    {
        return GrowthAchievementsEngineCore::bootstrap();
    }

    /**
     * Get achievements summary for API responses
     * Delegates to Core service
     *
     * @return array API summary
     */
    public static function getApiSummary()
    {
        return GrowthAchievementsEngineCore::getApiSummary();
    }

    /**
     * Clear all caches
     * Clears caches in all services
     *
     * @return void
     */
    public static function clearAllCaches()
    {
        GrowthAchievementsCalculator::clearAllCache();
        GrowthAchievementsDashboard::clearAllCache();
    }

    /**
     * Clear cache for specific event
     * Delegates to Calculator service
     *
     * @param string $event_type Event type
     * @param array $event_config Event configuration
     * @return void
     */
    public static function clearCacheForEvent($event_type, $event_config)
    {
        GrowthAchievementsCalculator::clearCacheForEvent($event_type, $event_config);
        GrowthAchievementsDashboard::clearDashboardCache();
    }
}
