<?php
namespace Lean_CSNS;

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

/**
 * Lifecycle trait
 *
 * Holds the three WordPress plugin lifecycle callbacks. Kept as a trait so the
 * activate/deactivate/uninstall logic lives in one place, separate from the
 * bootstrap/loader concerns in Base.
 *
 * IMPORTANT split (was a bug before): deactivation is TEMPORARY and must never
 * delete data (users deactivate mid free<->pro upgrade swaps all the time).
 * The opt-in data wipe lives ONLY in on_uninstall(), which fires when the user
 * permanently deletes the plugin.
 */
trait Lifecycle {

    /**
     * Activation: create tables, seed version + first-install + default settings.
     */
    public static function on_activation() {
        // Database class is loaded early in Base::bootstrap(), so it's ready here
        // even though activation fires before plugins_loaded.

        // Set database version
        update_option('lean_csns_db_version', LEAN_CSNS_DB_VERSION);

        // Create/update database tables
        Database::get_instance()->create_tables();

        // Existing users may deactivate and reactivate the plugins multiple times. So we need to track installation time only if it's not already set.
        if (!get_option('lean_csns_first_install')) {
            update_option('lean_csns_first_install', gmdate('Y-m-d H:i:s'), '', false);
        }

        // Set default options using centralized settings
        $default_settings = include LEAN_CSNS_PL_DIR . 'includes/default-settings.php';
        if (!get_option('lean_csns_settings')) {
            add_option('lean_csns_settings', $default_settings, '', false);
        }
    }

    /**
     * Deactivation: temporary, reversible. Only flush rewrite rules.
     * Never wipe data here. Data deletion belongs in uninstall.php (permanent).
     */
    public static function on_deactivation() {
        flush_rewrite_rules();
    }

    // Note: uninstall/data-wipe lives in the plugin-root uninstall.php (self-contained,
    // WP loads it on delete). No on_uninstall() here on purpose.
}
