<?php
/**
 * Plugin Activation Handler
 *
 * @package ZenoChatbot\Hooks\Handler\Plugin
 */

namespace ZenoChatbot\Hooks\Handler\Plugin;

use ZenoChatbot\Framework\Database\Migration\Migrator;
use ZenoChatbot\Hooks\Handler\BaseHandler;
use ZenoChatbot\Services\ConversationSummaryWatcher;
use ZenoChatbot\Services\Scheduler\Scheduler;

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Handles plugin activation tasks
 */
class ActivationHandler extends BaseHandler
{
    /**
     * Handle plugin activation
     */
    public function handle(...$args)
    {
        ob_start();

        try {
            try {
                Migrator::migrate($this->app->basePath('database/migrations'));
            } catch (\Throwable $e) {
                return;
            }

            try {
                Scheduler::boot();
                Scheduler::ensureCleanupTaskScheduled();
                ConversationSummaryWatcher::ensureMaintenanceScheduled();
            } catch (\Throwable $e) {
                // Do not block activation if background scheduling fails.
            }
        } finally {
            ob_end_clean();
        }
    }

}
