<?php
/**
 * Author: Rymera Web Co.
 *
 * @package SAAI\Classes
 */

namespace SAAI\Helpers;

use SAAI\REST\Connect as REST_Connect;
use SAAI\Traits\Singleton_Trait;

/**
 * Connect class.
 *
 * @since 1.0.0
 */
class Connect {

    use Singleton_Trait;

    /**
     * Holds the class instance object
     *
     * @var Connect $instance object
     * @since 1.0.0
     */
    protected static $instance;

    /**
     * Consumer key name.
     *
     * @since 1.0.0
     */
    public const CONSUMER_KEY_NAME = '_storeagent_consumer_key';

    /**
     * Consumer secret key name.
     *
     * @since 1.0.0
     */
    public const CONSUMER_SECRET_KEY_NAME = '_storeagent_consumer_secret';

    /**
     * Consumer key ID name.
     *
     * @since 1.0.0
     */
    public const CONSUMER_KEY_ID_NAME = '_storeagent_consumer_key_id';

    /**
     * Is the store connected to StoreAgent API service.
     *
     * @since 1.0.0
     *
     * @return bool
     */
    public static function is_connected() {

        return self::get_consumer_key_id() && self::get_consumer_key() && self::get_consumer_secret();
    }

    /**
     * Update StoreAgent connect status.
     *
     * @param null|bool $status The status of the connection.
     *
     * @since 1.0.0
     * @return bool
     */
    public static function update_connect_status( $status = null ) {

        return update_option( 'storeagent_api_connected', $status, false );
    }

    /**
     * Get the unescaped auth connect URL.
     *
     * @since 1.0.0
     * @return string
     */
    public static function get_url() {

        $login_url  = Helper::get_storeagent_base_url( 'storeagent-auth' ) . '/v1/login';
        $rest       = REST_Connect::instance();
        $return_url = add_query_arg(
            array(
                'page'       => 'storeagent-dashboard',
                'saai_nonce' => wp_create_nonce( 'storeagent-auth' ),
            ),
            admin_url( 'admin.php' )
        );

        $user_id = get_current_user_id();
        if ( ! $user_id ) {
            /***************************************************************************
             * Set the user ID to 1 if the current user is not logged in.
             ***************************************************************************
             *
             * This is possible for example if plugin is activated via WP-CLI without
             * a user context (i.e. not using the --user flag).
             *
             * Not necessarily needed for the StoreAgent AI plugin, but it's required
             * in the StoreAgent auth.
             */
            $user_id = 1;
        }

        return add_query_arg(
            array(
                'app_name'       => rawurlencode( 'StoreAgent AI for WooCommerce: ' . preg_replace( '/[^\p{L}\p{N}\s-]/u', '', get_bloginfo( 'name' ) ) ),
                'user_id'        => $user_id,
                'scope'          => 'read_write',
                'return_url'     => rawurlencode( $return_url ),
                'disconnect_url' => esc_url( $rest->api_url . 'auth/disconnect/webhook/' ),
            ),
            $login_url
        );
    }

    /**
     * Update the consumer key ID.
     *
     * @param int $id The consumer key ID.
     *
     * @return bool
     */
    public static function update_consumer_key_id( $id ) {

        if ( empty( $id ) ) {
            return delete_option( self::CONSUMER_KEY_ID_NAME );
        }

        return update_option( self::CONSUMER_KEY_ID_NAME, $id, false );
    }

    /**
     * Get the consumer key ID.
     *
     * @return int
     */
    public static function get_consumer_key_id() {

        return (int) get_option( self::CONSUMER_KEY_ID_NAME, 0 );
    }

    /**
     * Update the consumer key.
     *
     * @param string $consumer_key The consumer key.
     *
     * @since 1.0.0
     * @return bool
     */
    public static function update_consumer_key( $consumer_key ) {

        if ( empty( $consumer_key ) ) {
            return delete_option( self::CONSUMER_KEY_NAME );
        }

        return update_option( self::CONSUMER_KEY_NAME, $consumer_key, false );
    }

    /**
     * Get the consumer key.
     *
     * @since 1.0.0
     * @return mixed
     */
    public static function get_consumer_key() {

        return get_option( self::CONSUMER_KEY_NAME, null );
    }

    /**
     * Get the truncated key.
     *
     * @return string
     */
    public static function get_truncated_key() {

        $consumer_key = self::get_consumer_key();

        if ( empty( $consumer_key ) ) {
            return '';
        }

        return mb_substr( $consumer_key, -7 );
    }

    /**
     * Update the consumer secret.
     *
     * @param string $consumer_secret The consumer secret.
     *
     * @since 1.0.0
     * @return bool
     */
    public static function update_consumer_secret( $consumer_secret ) {

        if ( empty( $consumer_secret ) ) {
            return delete_option( self::CONSUMER_SECRET_KEY_NAME );
        }

        return update_option( self::CONSUMER_SECRET_KEY_NAME, $consumer_secret, false );
    }

    /**
     * Get the consumer secret.
     *
     * @since 1.0.0
     * @return mixed
     */
    public static function get_consumer_secret() {

        return get_option( self::CONSUMER_SECRET_KEY_NAME, null );
    }
}
