<?php
/**
 * Plugin install helper.
 *
 * Generates Install / Activate / Deactivate buttons for theme plugins.
 * Based on the proven pattern from Formula theme.
 *
 * @package Blog_Over
 */

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

/**
 * Class Blog_Over_Plugin_Helper
 */
class Blog_Over_Plugin_Helper
{

    /**
     * Singleton instance.
     *
     * @var Blog_Over_Plugin_Helper|null
     */
    private static $instance;

    /**
     * Get singleton instance.
     *
     * @return Blog_Over_Plugin_Helper
     */
    public static function instance()
    {
        if (!isset(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Get the main plugin file path from a slug.
     *
     * @param string $slug Plugin slug.
     * @return string
     */
    public static function get_plugin_path($slug)
    {
        return $slug . '/' . $slug . '.php';
    }

    /**
     * Check plugin state: 'install', 'activate', or 'deactivate'.
     *
     * @param string $slug Plugin slug.
     * @return string
     */
    public function check_plugin_state($slug)
    {
        $plugin_path = self::get_plugin_path($slug);

        if (!function_exists('is_plugin_active')) {
            include_once ABSPATH . 'wp-admin/includes/plugin.php';
        }

        if (file_exists(WP_PLUGIN_DIR . '/' . $plugin_path)) {
            return is_plugin_active($plugin_path) ? 'deactivate' : 'activate';
        }

        return 'install';
    }

    /**
     * Generate the Install / Activate / Active button HTML.
     *
     * @param string $slug     Plugin slug.
     * @param string $redirect Optional redirect URL after activation.
     * @return string
     */
    public function get_button_html($slug, $redirect = '')
    {
        if (empty($slug)) {
            return '';
        }

        $state = $this->check_plugin_state($slug);
        $plugin_path = self::get_plugin_path($slug);

        $nonce_url = add_query_arg(
            array(
                'action' => 'activate',
                'plugin' => rawurlencode($plugin_path),
                'plugin_status' => 'all',
                'paged' => '1',
                '_wpnonce' => wp_create_nonce('activate-plugin_' . $plugin_path),
            ),
            network_admin_url('plugins.php')
        );

        $html = '<div class="plugin-card-' . esc_attr($slug) . '" style="padding: 8px 0 5px;">';

        switch ($state) {
            case 'install':
                $html .= '<a data-redirect="' . esc_url($redirect) . '" data-slug="' . esc_attr($slug) . '" class="install-now button" href="' . esc_url($nonce_url) . '" data-name="' . esc_attr($slug) . '">' . esc_html__('Install and Activate', 'blog-over') . '</a>';
                break;

            case 'activate':
                $html .= '<a data-redirect="' . esc_url($redirect) . '" data-slug="' . esc_attr($slug) . '" class="activate-now button button-primary" href="' . esc_url($nonce_url) . '">' . esc_html__('Activate', 'blog-over') . '</a>';
                break;

            case 'deactivate':
                $html .= '<span class="button button-disabled">' . esc_html__('Active', 'blog-over') . '</span>';
                break;
        }

        $html .= '</div>';

        return $html;
    }
}
