<?php

declare(strict_types=1);

namespace Duplicator\Addons\ProBase\Telemetry;

use Duplicator\Utils\CronUtils;

/**
 * Wires the telemetry subsystem: cron handlers, activation/deactivation
 * hooks, and event hooks.
 *
 * This addon hooks into the core lifecycle hooks (`duplicator_after_activation`,
 * `duplicator_after_deactivation`) instead of being called from the core —
 * core must not depend on addons.
 */
class TelemetryBootstrap
{
    const SNAPSHOT_CRON_HOOK = 'duplicator_telemetry_snapshot_cron';
    const NUDGES_CRON_HOOK   = 'duplicator_telemetry_nudges_cron';

    const DEFAULT_WORKER_URL = 'https://duplicator-telemetry.seedprod-account.workers.dev';

    /**
     * Hook everything. Called once from ProBase::init().
     *
     * @return void
     */
    public static function init(): void
    {
        if (!defined('DUPLICATOR_TELEMETRY_WORKER_URL')) {
            define('DUPLICATOR_TELEMETRY_WORKER_URL', self::DEFAULT_WORKER_URL);
        }

        // Cron handlers must be registered every pageload so a previously
        // scheduled cron still drains correctly when telemetry is later
        // disabled (the handlers themselves no-op via isEnabled()).
        add_action(self::SNAPSHOT_CRON_HOOK, [TelemetrySnapshot::class, 'cronSend']);
        add_action(self::NUDGES_CRON_HOOK, [TelemetryNudges::class, 'cronFetch']);

        // Cron (de)scheduling is gated by plugin lifecycle, not by admin
        // pageloads — mirrors StatsBootstrap::cronActivatie / cronDeactivate.
        add_action('duplicator_after_activation', [self::class, 'cronActivate']);
        add_action('duplicator_after_deactivation', [self::class, 'cronDeactivate']);

        TelemetryEvents::init();
    }

    /**
     * Schedule the weekly snapshot and the daily nudge fetch on plugin
     * activation/upgrade. No-op when telemetry is disabled so a misconfigured
     * site never schedules.
     *
     * Also fires the first snapshot synchronously on activation/upgrade:
     * the FIRST_SENT_OPTION flag makes it idempotent across upgrades.
     *
     * The snapshot cron is independent from Connect's
     * `duplicator_usage_tracking_cron` so we don't inherit Connect's
     * randomized weekly delay (plan §"Snapshot trigger").
     *
     * @return void
     */
    public static function cronActivate(): void
    {
        if (!TelemetryClient::isEnabled()) {
            return;
        }

        CronUtils::scheduleEvent(time() + HOUR_IN_SECONDS, CronUtils::INTERVAL_WEEKLY, self::SNAPSHOT_CRON_HOOK);
        CronUtils::scheduleEvent(time() + HOUR_IN_SECONDS, CronUtils::INTERVAL_DAILY, self::NUDGES_CRON_HOOK);

        TelemetrySnapshot::maybeSendFirstSnapshot();
    }

    /**
     * Drop the crons on plugin deactivation. The persistent options
     * (first-sent flag, failure streak) are intentionally kept so a
     * re-activation does not duplicate the first-send signal.
     *
     * @return void
     */
    public static function cronDeactivate(): void
    {
        CronUtils::unscheduleEvent(self::SNAPSHOT_CRON_HOOK);
        CronUtils::unscheduleEvent(self::NUDGES_CRON_HOOK);
    }
}
