<?php

namespace UltraguardPro\modules\upgrade_manager;

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

use UltraguardPro\Plugin;
use UltraguardPro\modules\core\AbstractModule;
use UltraguardPro\support\LicenseInfo;

class UpgradeManagerModule extends AbstractModule {

    public function __construct( Plugin $plugin ) {
        parent::__construct( $plugin );
        $this->plugin->settings->register_module_prefix( 'upgrade_manager_', 'upgrade_manager' );
    }

    public function get_id(): string {
        return 'upgrade_manager';
    }

    public function is_enabled(): bool {
        return true;
    }

    public function boot(): void {
        if ( method_exists( $this->plugin->settings, 'register_settings_schema' ) ) {
            $this->plugin->settings->register_settings_schema( 'upgrade_manager', 'upgrade_manager_', [
                'upgrade_manager_show_inline_nudges'   => [ 'type' => 'bool', 'default' => true ],
                'upgrade_manager_external_upgrade_url' => [ 'type' => 'text', 'default' => \UltraguardPro\support\UpgradeManager::EXTERNAL_UPGRADE_URL ],
                'upgrade_manager_billing_portal_url'   => [ 'type' => 'text', 'default' => '' ],
            'upgrade_manager_support_url'          => [ 'type' => 'text', 'default' => 'https://ultraguard.net/contact' ],
            ] );
        }

        ( new ajax\UpgradeManagerAjax( $this->plugin ) )->register();
        add_action( 'ultraguard_register_admin_pages', [ $this, 'register_pages' ], 100, 1 );
    }

    public function register_pages( $admin ): void {
        $is_pro = LicenseInfo::instance()->isPro();

        $admin->register_page( [
            'id'               => 'upgrade-manager',
            'page_title'       => __( 'License & Upgrade Center', 'ultraguard-security-suite' ),
            'menu_title'       => $is_pro ? __( 'License', 'ultraguard-security-suite' ) : __( 'License & Upgrade', 'ultraguard-security-suite' ),
            'capability'       => 'manage_options',
            'scheme_callback'  => [ new pages\UpgradeManagerDashboard( $this->plugin ), 'get_scheme' ],
            'parent_id'        => '',
            'priority'         => 100,
            'group'            => 'System',
            'icon'             => 'dashicons-awards',
            'show_free_footer' => false,
        ] );

        $admin->register_page( [
            'id'               => 'upgrade-manager-free',
            'page_title'       => __( 'Activation', 'ultraguard-security-suite' ),
            'menu_title'       => __( 'Activation', 'ultraguard-security-suite' ),
            'capability'       => 'manage_options',
            'scheme_callback'  => [ new pages\UpgradeManagerFree( $this->plugin ), 'get_scheme' ],
            'parent_id'        => 'upgrade-manager',
            'priority'         => 101,
            'group'            => 'System',
            'icon'             => 'dashicons-unlock',

            'show_free_footer' => false,
        ] );

        $admin->register_page( [
            'id'               => 'upgrade-manager-paid',
            'page_title'       => __( 'Subscription & Entitlements', 'ultraguard-security-suite' ),
            'menu_title'       => __( 'Subscription', 'ultraguard-security-suite' ),
            'capability'       => 'manage_options',
            'scheme_callback'  => [ new pages\UpgradeManagerPaid( $this->plugin ), 'get_scheme' ],
            'parent_id'        => 'upgrade-manager',
            'priority'         => 102,
            'group'            => 'System',
            'icon'             => 'dashicons-admin-network',
            'show_free_footer' => false,
        ] );

        $admin->register_page( [
            'id'               => 'upgrade-manager-settings',
            'page_title'       => __( 'License & Upgrade Settings', 'ultraguard-security-suite' ),
            'menu_title'       => __( 'Settings', 'ultraguard-security-suite' ),
            'capability'       => 'manage_options',
            'scheme_callback'  => [ new pages\UpgradeManagerSettings( $this->plugin ), 'get_scheme' ],
            'parent_id'        => 'upgrade-manager',
            'priority'         => 103,
            'group'            => 'System',
            'icon'             => 'dashicons-admin-settings',
            'show_free_footer' => false,
        ] );

        $admin->register_page( [
            'id'               => 'upgrade',
            'page_title'       => __( 'Upgrade to Pro', 'ultraguard-security-suite' ),
            'menu_title'       => __( 'Upgrade to Pro', 'ultraguard-security-suite' ),
            'capability'       => 'manage_options',
            'scheme_callback'  => [ new pages\UpgradeManagerDashboard( $this->plugin ), 'get_scheme' ],
            'parent_id'        => 'upgrade-manager',
            'priority'         => 104,
            'group'            => 'System',
            'icon'             => 'dashicons-awards',
            'is_hidden'        => true,
            'show_free_footer' => false,
        ] );
    }
}
