<?php

namespace Twnt4All\Update;

use Twnt4All\Support\WpBridge;

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

/**
 * LicenseManager - minimal stub.
 *
 * The original implementation used the Easy Digital Downloads Software
 * Licensing API to activate, deactivate and validate license keys against
 * pay4all.ch, and bundled the EDD_SL_Plugin_Updater to auto-update the
 * plugin from outside WordPress.org. Both have been removed.
 *
 * This stub keeps the public surface (get_instance / get_license /
 * activate_license / deactivate_license / valid_license /
 * network_valid_license) so existing callers in AdminAjax, WalletRequest and
 * WalletSubscription keep working. License keys entered through the wizard
 * are stored as-is in wp_options under the plugin prefix; no remote call is
 * made and the key is always considered valid.
 */
class LicenseManager
{
    private        $prefix;
    private static $instance;

    public function __construct()
    {
        $this->prefix = T4A_PREFIX;
    }

    /**
     * Get single instance of this class.
     *
     * @return LicenseManager
     */
    public static function get_instance()
    {
        if ( empty( static::$instance ) ) {
            static::$instance = new static();
        }

        return static::$instance;
    }

    /**
     * Always valid - no remote validation.
     *
     * @param bool $force_update
     * @return bool
     */
    public function network_valid_license( $force_update = true )
    {
        return true;
    }

    /**
     * Always valid - no remote validation.
     *
     * @param int $blog_id
     * @return bool
     */
    public function valid_license( $blog_id = 1 )
    {
        return true;
    }

    /**
     * Stores the license key locally without contacting any remote service.
     *
     * @param string $license
     * @param int    $blog_id
     * @return array{status:bool,message:string}
     */
    public function activate_license( $license, $blog_id = 1 )
    {
        $license = sanitize_text_field( $license );

        $license_options = static::get_option( $this->prefix . '_license_options', $blog_id );
        if ( !is_array( $license_options ) ) {
            $license_options = [];
        }
        $license_options[ 'license_status' ] = true;
        $license_options[ 'license_key' ]    = $license;
        $license_options[ 'license_prev' ]   = $license;
        $license_options[ 'status' ]         = 'valid';

        static::update_option( $this->prefix . '_license_options', $license_options, $blog_id );

        return [
            'status'  => true,
            'message' => __( 'License saved.', 'pay4all-for-twint' ),
        ];
    }

    /**
     * Clears the locally stored license key.
     *
     * @param int $blog_id
     * @return array{status:bool,message:string}
     */
    public function deactivate_license( $blog_id = 1 )
    {
        static::update_option( $this->prefix . '_license_options', [], $blog_id );

        return [
            'status'  => true,
            'message' => __( 'License removed.', 'pay4all-for-twint' ),
        ];
    }

    /**
     * No-op stub kept for legacy callers.
     *
     * @param int $blog_id
     * @return array{status:bool,message:string}
     */
    public function check_license( $blog_id = 1 )
    {
        return [
            'status'  => true,
            'message' => __( 'License active.', 'pay4all-for-twint' ),
        ];
    }

    /**
     * Read the stored license options array.
     *
     * @param int $blog_id
     * @return array|false
     */
    public function get_license_options( $blog_id = 1 )
    {
        return static::get_option( $this->prefix . '_license_options', $blog_id );
    }

    /**
     * Return the stored license key, or false when none is set.
     *
     * @param int $blog_id
     * @return string|false
     */
    public function get_license( $blog_id = 1 )
    {
        $license_options = $this->get_license_options( $blog_id );

        if ( is_array( $license_options ) && isset( $license_options[ 'license_key' ] ) ) {
            return $license_options[ 'license_key' ];
        }

        return false;
    }

    /**
     * Multisite-aware get_option proxy.
     *
     * @param string $option
     * @param int|null $blog_id
     * @return mixed
     */
    public static function get_option( $option, $blog_id = null )
    {
        if ( is_multisite() && $blog_id ) {
            return WpBridge::get_blog_option( $blog_id, $option );
        }
        return get_option( $option );
    }

    /**
     * Multisite-aware update_option proxy.
     *
     * @param string $option
     * @param mixed  $value
     * @param int|null $blog_id
     * @return bool
     */
    public static function update_option( $option, $value, $blog_id = null )
    {
        if ( is_multisite() && $blog_id && function_exists( 'update_blog_option' ) ) {
            return update_blog_option( $blog_id, $option, $value );
        }
        return update_option( $option, $value );
    }
}
