<?php
/**
 * Plugin Action Links Class
 *
 * @package SAAI\Classes
 */

namespace SAAI\Classes;

use SAAI\Helpers\Connect;
use SAAI\Traits\Singleton_Trait;

/**
 * Plugin Action Links Class
 *
 * This class adds action links to the plugin listing page.
 *
 * @since 1.0.2
 */
class Plugin_Action_Links {

    use Singleton_Trait;

    /**
     * Holds the class instance object
     *
     * @var Plugin_Action_Links $instance object
     * @since 1.0.2
     */
    protected static $instance;

    /**
     * Add hooks and filters.
     *
     * @since 1.0.2
     * @return void
     */
    public function run() {
        add_filter( 'plugin_action_links_' . SAAI_BASENAME, array( $this, 'add_action_links' ) );
    }

    /**
     * Add action links to the plugin list.
     *
     * @since 1.0.2
     * @param array $links Plugin action links.
     * @return array Modified action links.
     */
    public function add_action_links( $links ) {
        // Check if the plugin is connected.
        $is_connected = Connect::is_connected();

        // Determine the link text based on connection status.
        $link_text = $is_connected ? __( 'Settings', 'storeagent-ai-for-woocommerce' ) : __( 'Connect', 'storeagent-ai-for-woocommerce' );

        // Create the action link.
        $action_link = '<a href="' . esc_url( admin_url( 'admin.php?page=' . Admin_Menu_Handler::PARENT_SLUG ) ) . '"><b>' . esc_html( $link_text ) . '</b></a>';

        // Add our link to the beginning of the array.
        array_unshift( $links, $action_link );

        return $links;
    }
}
