<?php

defined( 'ABSPATH' ) || exit;
/**
 * My Account integration.
 *
 * FREE:  Read-only status notice on Account Details page.
 * PRO:   Dedicated "Tax Exemption" tab with submission form.
 */
class Rankd_Tx_My_Account {
    const ENDPOINT = 'tax-exemption';

    public static function init() {
        // Always: status notice on Account Details.
        add_action( 'woocommerce_edit_account_form_start', array(__CLASS__, 'render_status_notice') );
        add_action( 'wp_enqueue_scripts', array(__CLASS__, 'enqueue_assets') );
        // PRO only: dedicated tab.
        if ( function_exists( 'rankd_tx_fs' ) && rankd_tx_fs()->can_use_premium_code__premium_only() ) {
            add_action( 'init', array(__CLASS__, 'register_endpoint__premium_only') );
            add_filter( 'woocommerce_account_menu_items', array(__CLASS__, 'add_menu_item__premium_only') );
            add_action( 'woocommerce_account_' . self::ENDPOINT . '_endpoint', array(__CLASS__, 'render_tab__premium_only') );
        }
    }

    // -------------------------------------------------------------------------
    // FREE: Status notice on Account Details
    // -------------------------------------------------------------------------
    public static function render_status_notice() {
        $user_id = get_current_user_id();
        if ( !$user_id || !Rankd_Tx_Capabilities::is_in_scope( $user_id ) ) {
            return;
        }
        $exempt = Rankd_Tx_Exemption::is_exempt( $user_id );
        ?>
		<div class="rankd-tx-status-box <?php 
        echo ( $exempt ? 'rankd-tx-exempt' : 'rankd-tx-not-exempt' );
        ?>">
			<span class="rankd-tx-status-icon"><?php 
        echo ( $exempt ? '&#10003;' : '&#9711;' );
        ?></span>
			<span>
				<?php 
        if ( $exempt ) {
            ?>
					<strong><?php 
            esc_html_e( 'Your account is tax exempt.', 'rankd-tax-exemption-certificates-for-woocommerce' );
            ?></strong>
					<?php 
            esc_html_e( 'Tax will not be applied to your orders.', 'rankd-tax-exemption-certificates-for-woocommerce' );
            ?>
				<?php 
        } else {
            ?>
					<?php 
            esc_html_e( 'Your account is not currently tax exempt.', 'rankd-tax-exemption-certificates-for-woocommerce' );
            ?>
					<?php 
            // Brace block (not alternative syntax) so Freemius strips
            // it cleanly from the free build.
            if ( function_exists( 'rankd_tx_fs' ) && rankd_tx_fs()->can_use_premium_code__premium_only() ) {
                echo '<a href="' . esc_url( wc_get_account_endpoint_url( self::ENDPOINT ) ) . '">' . esc_html__( 'Submit a tax exemption certificate →', 'rankd-tax-exemption-certificates-for-woocommerce' ) . '</a>';
            }
            ?>
				<?php 
        }
        ?>
			</span>
		</div>
		<?php 
    }

    // -------------------------------------------------------------------------
    // Assets
    // -------------------------------------------------------------------------
    public static function enqueue_assets() {
        if ( !is_account_page() ) {
            return;
        }
        wp_enqueue_style(
            'rankd-tx-frontend',
            RANKD_TX_PLUGIN_URL . 'assets/css/frontend.css',
            array(),
            RANKD_TX_VERSION
        );
        if ( function_exists( 'rankd_tx_fs' ) && rankd_tx_fs()->can_use_premium_code__premium_only() ) {
            wp_enqueue_script(
                'rankd-tx-frontend',
                RANKD_TX_PLUGIN_URL . 'assets/js/pro/frontend.js',
                array('jquery'),
                RANKD_TX_VERSION,
                true
            );
            $js_data = Rankd_Tx_Country_Data::get_js_data();
            wp_localize_script( 'rankd-tx-frontend', 'rankdTxFrontend', array(
                'nonce'        => wp_create_nonce( 'rankd_tx_submit_cert' ),
                'ajaxUrl'      => admin_url( 'admin-ajax.php' ),
                'statesMap'    => $js_data['statesMap'],
                'typesMap'     => $js_data['typesMap'],
                'genericTypes' => $js_data['genericTypes'],
                'strings'      => array(
                    'uploading'          => __( 'Uploading...', 'rankd-tax-exemption-certificates-for-woocommerce' ),
                    'fileTooLarge'       => __( 'File must be under 5MB.', 'rankd-tax-exemption-certificates-for-woocommerce' ),
                    'invalidType'        => __( 'Only PDF and JPG files are accepted.', 'rankd-tax-exemption-certificates-for-woocommerce' ),
                    'ajaxError'          => __( 'An error occurred. Please try again.', 'rankd-tax-exemption-certificates-for-woocommerce' ),
                    'selectCountryFirst' => __( '— Select country first —', 'rankd-tax-exemption-certificates-for-woocommerce' ),
                    'selectState'        => __( '— Select —', 'rankd-tax-exemption-certificates-for-woocommerce' ),
                    'selectType'         => __( '— Select —', 'rankd-tax-exemption-certificates-for-woocommerce' ),
                    'pastExpiry'         => __( 'The expiry date must be today or in the future. Please check your certificate.', 'rankd-tax-exemption-certificates-for-woocommerce' ),
                ),
            ) );
        }
    }

}
