<?php

defined('ABSPATH') || exit;

class Acadlix_Bootstrap_Activator
{
  /**
   * Registers all activation and lifecycle hooks.
   * Called exactly once from the main plugin file.
   */
  public static function init_hooks()
  {
    // Register the activation hook (requires the absolute path to the main file)
    register_activation_hook(ACADLIX_PLUGIN_FILE, [__CLASS__, 'activate']);

    // Register core lifecycle hooks
    add_action('upgrader_process_complete', [__CLASS__, 'mark_as_upgrade'], 10, 2);
    add_action('activated_plugin', [__CLASS__, 'activated_acadlix'], 10, 2);
    add_action('admin_init', [__CLASS__, 'handle_setup_redirect']);
  }

  /**
   * Fired when the plugin is activated
   */
  public static function activate($network_wide)
  {
    include_once ABSPATH . 'wp-admin/includes/plugin.php';

    $this_plugin = ACADLIX_PLUGIN_BASENAME;
    $free = 'acadlix/acadlix.php';
    $pro = 'acadlix-pro/acadlix.php';

    $sibling = ($this_plugin === $free) ? $pro : $free;

    // Deactivate sibling plugin conflict check
    if (is_plugin_active($sibling)) {
      wp_die(
        sprintf(
          'You cannot activate %s while %s is active. Please deactivate the other plugin first.',
          $this_plugin === $free ? 'Acadlix Free' : 'Acadlix Pro',
          $this_plugin === $free ? 'Acadlix Pro' : 'Acadlix Free'
        ),
        'Plugin conflict detected',
        ['back_link' => true]
      );
    }

    // Mark activation pending
    update_option('acadlix_activation_pending', [
      'plugin' => $this_plugin,
      'network' => (bool) $network_wide,
    ]);
  }

  /**
   * Handles the plugin upgrade transient detection
   */
  public static function mark_as_upgrade($upgrader_object, $options)
  {
    if ($options['action'] === 'update' && $options['type'] === 'plugin') {
      if (isset($options['plugins']) && in_array(ACADLIX_PLUGIN_BASENAME, $options['plugins'])) {
        set_transient('acadlix_updating_plugin', true, 60);
      }
    }
  }

  /**
   * Logic fired immediately after standard activation completes
   */
  public static function activated_acadlix($plugin, $network_wide = null)
  {
    if (ACADLIX_PLUGIN_BASENAME !== $plugin) {
      return;
    }

    if (get_transient('acadlix_updating_plugin')) {
      delete_transient('acadlix_updating_plugin');
      update_option('acadlix_wizard', 'active');
      return;
    }

    // Trigger safe transient flag for redirection
    set_transient('acadlix_activation_redirect', true, 30);
  }

  /**
   * Safe redirection to the setup wizard on the next admin page render
   */
  public static function handle_setup_redirect()
  {
    if (get_transient('acadlix_activation_redirect')) {
      delete_transient('acadlix_activation_redirect');

      if (!get_option('acadlix_wizard') && !wp_doing_ajax() && !(defined('WP_CLI') && WP_CLI)) {
        update_option('acadlix_wizard', 'active');
        wp_safe_redirect(admin_url('admin.php?page=acadlix-setup'));
        exit;
      }
    }
  }
}