<?php
/**
 * Structure Viewer — Plugin bootstrap
 *
 * Registers all WordPress hooks in one place.
 * No business logic lives here — only add_action / add_filter calls.
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit( 'Direct access denied.' );
}

final class SV_Plugin {

    /**
     * Register all plugin hooks. Called once from structure-viewer.php.
     */
    public static function init() {
        // Admin menu
        add_action( 'admin_menu', [ 'SV_AdminPage', 'register_menu' ] );

        // Asset enqueue
        add_action( 'admin_enqueue_scripts', [ 'SV_Loader', 'enqueue' ] );

        // Plugin listing action link
        add_filter(
            'plugin_action_links_' . plugin_basename( STRUCTUREVIEWER_PLUGIN_DIR . 'structure-viewer.php' ),
            [ __CLASS__, 'add_action_link' ]
        );

        // ── AJAX: Explorer ───────────────────────────────────────────
        add_action( 'wp_ajax_structureviewer_get_item_list',  [ 'SV_AdminPage',  'ajax_get_item_list' ] );
        add_action( 'wp_ajax_structureviewer_load_tree',      [ 'SV_Scanner',    'ajax_load_tree' ] );
        add_action( 'wp_ajax_structureviewer_export',         [ 'SV_Exporter',   'ajax_export' ] );
        add_action( 'wp_ajax_structureviewer_copy_clipboard', [ 'SV_Exporter',   'ajax_copy_clipboard' ] );
        add_action( 'wp_ajax_structureviewer_view_file',      [ 'SV_FileViewer', 'ajax_view_file' ] );
        add_action( 'wp_ajax_structureviewer_download_file',  [ 'SV_FileViewer', 'ajax_download_file' ] );

        // ── AJAX: Search ─────────────────────────────────────────────
        add_action( 'wp_ajax_structureviewer_search',                    [ 'SV_Search',  'ajax_search' ] );

        // ── AJAX: Dashboard ──────────────────────────────────────────
        add_action( 'wp_ajax_structureviewer_get_top_plugins', [ 'SV_Dashboard', 'ajax_get_top_plugins' ] );

        // ── AJAX: Settings ───────────────────────────────────────────
        add_action( 'wp_ajax_structureviewer_save_settings', [ 'SV_Settings', 'ajax_save' ] );

        // ── Freemius license activation dialog ───────────────────────
        // Freemius only injects the dialog HTML + its inline JS click handler on
        // the native plugins list page. On our custom settings page it is never
        // added, so "Activate License" / "Change License" buttons do nothing.
        // We inject it ourselves via admin_footer, scoped to our page only.
        add_action( 'admin_footer', [ __CLASS__, 'inject_license_dialog' ] );

        // ── Post-purchase welcome modal ───────────────────────────────
        // after_license_change passes ( $plan_change_string, $plan_object ).
        // We only need to know a licence became active, so we check is_premium()
        // inside the callback rather than inspecting the parameters.
        if ( function_exists( 'structureviewer_fs' ) ) {
            structureviewer_fs()->add_action( 'after_license_change', [ __CLASS__, 'on_license_activated' ], 10, 2 );
        }
    }

    public static function add_action_link( $links ) {
        $explore = '<a href="' . esc_url( admin_url( 'tools.php?page=structure-viewer' ) ) . '">'
                 . esc_html__( 'Explore', 'structure-viewer' ) . '</a>';
        array_unshift( $links, $explore );
        return $links;
    }

    /**
     * Inject the Freemius license activation dialog into admin_footer on our
     * settings page. Freemius only adds this on the plugins list page by default.
     */
    public static function inject_license_dialog() {
        $screen = get_current_screen();
        if ( ! $screen ) {
            return;
        }

        $sv_screens = [ 'tools_page_structure-viewer', 'toplevel_page_structure-viewer' ];
        if ( ! in_array( $screen->id, $sv_screens, true ) ) {
            return;
        }

        if ( function_exists( 'structureviewer_fs' ) && ( structureviewer_fs()->is_premium() || structureviewer_fs()->has_paid_plan() ) ) {
            structureviewer_fs()->_add_license_activation_dialog_box();
        }
    }

    /**
     * Fires when Freemius changes the licence state.
     *
     * NOTE: after_license_change passes ( string $plan_change, FS_Plan $plan ).
     * It does NOT pass licence objects — checking $plan_change is the correct way
     * to determine whether a licence became active.
     *
     * @param string   $plan_change  e.g. 'plan_upgraded', 'trial_started', etc.
     * @param FS_Plan  $plan         The current plan object.
     */
    public static function on_license_activated( $plan_change, $plan ) {
        // Only set the transient for genuine upgrades/activations, not downgrades.
        $activation_events = [ 'plan_upgraded', 'trial_started', 'trial_promotion' ];
        if ( ! in_array( $plan_change, $activation_events, true ) ) {
            return;
        }

        $user_id = get_current_user_id();
        if ( $user_id ) {
            // Expires after 1 hour — survives the post-activation redirect.
            set_transient( 'sv_welcome_modal_' . $user_id, 1, HOUR_IN_SECONDS );
        }
    }
}