<?php
/**
 * Template Name: Affiliate Registration
 *
 * Self-service affiliate registration page
 */

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

// Enqueue WordPress password strength meter
wp_enqueue_script( 'password-strength-meter' );
wp_enqueue_script( 'zxcvbn-async' );

get_header(); ?>

<main id="primary" class="site-main">

<?php
// Display verification and error messages from plugin redirects.
if ( isset( $_GET['verification-sent'] )
    && isset( $_GET['_wpnonce'] )
    && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'strp_sub_affiliate_redirect' )
    && 'true' === sanitize_text_field( wp_unslash( $_GET['verification-sent'] ) )
) {
    ?>
    <div class="custom-login-container">
        <div class="woocommerce">
            <div class="strp-sub-custom-notice strp-sub-notice-success">
                <div class="notice-icon-wrapper">
                    <i data-lucide="mail"></i>
                </div>
                <span><?php esc_html_e( 'Account created! We have sent you a verification email. Click the link in the email to activate your account.', 'subscription-manager-for-stripe' ); ?></span>
                <button type="button" class="strp-sub-notice-close-btn" aria-label="<?php esc_attr_e( 'Close', 'subscription-manager-for-stripe' ); ?>">
                    <i data-lucide="x"></i>
                </button>
            </div>
        </div>
    </div>
    <?php
}

if ( isset( $_GET['verified'] )
    && isset( $_GET['_wpnonce'] )
    && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'strp_sub_affiliate_redirect' )
    && 'true' === sanitize_text_field( wp_unslash( $_GET['verified'] ) )
) {
    ?>
    <div class="custom-login-container">
        <div class="woocommerce">
            <div class="strp-sub-custom-notice strp-sub-notice-success">
                <div class="notice-icon-wrapper">
                    <i data-lucide="check-circle"></i>
                </div>
                <span><?php esc_html_e( 'Your account has been verified! You can now log in.', 'subscription-manager-for-stripe' ); ?></span>
                <button type="button" class="strp-sub-notice-close-btn" aria-label="<?php esc_attr_e( 'Close', 'subscription-manager-for-stripe' ); ?>">
                    <i data-lucide="x"></i>
                </button>
            </div>
        </div>
    </div>
    <?php
}

if ( isset( $_GET['error'] )
    && isset( $_GET['_wpnonce'] )
    && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'strp_sub_affiliate_redirect' )
) {
    $error_code    = sanitize_text_field( wp_unslash( $_GET['error'] ) );
    $allowed_errors = array( 'invalid-token', 'already-verified', 'disabled' );
    if ( ! in_array( $error_code, $allowed_errors, true ) ) {
        $error_code = 'unknown';
    }
    switch ( $error_code ) {
        case 'invalid-token':
            $error_message = __( 'Invalid or expired verification link. Please request a new verification email.', 'subscription-manager-for-stripe' );
            break;
        case 'already-verified':
            $error_message = __( 'This account is already verified. You can log in.', 'subscription-manager-for-stripe' );
            break;
        case 'disabled':
            $error_message = __( 'Affiliate registrations are currently disabled. Please contact us for more information.', 'subscription-manager-for-stripe' );
            break;
        default:
            $error_message = __( 'An error occurred. Please try again.', 'subscription-manager-for-stripe' );
    }
    ?>
    <div class="custom-login-container">
        <div class="woocommerce">
            <div class="strp-sub-custom-notice strp-sub-notice-error">
                <div class="notice-icon-wrapper">
                    <i data-lucide="alert-triangle"></i>
                </div>
                <span><?php echo esc_html( $error_message ); ?></span>
                <button type="button" class="strp-sub-notice-close-btn" aria-label="<?php esc_attr_e( 'Close', 'subscription-manager-for-stripe' ); ?>">
                    <i data-lucide="x"></i>
                </button>
            </div>
        </div>
    </div>
    <?php
}
?>

<?php if ( ! is_user_logged_in() ) : ?>

<div class="custom-login-container">
    <div class="woocommerce">
        <?php
        $template = locate_template( 'template-parts/affiliate-registration-form.php' );
        if ( ! $template ) {
            $template = STRP_SUB_PLUGIN_DIR . 'templates/affiliate-registration-form.php';
        }
        if ( file_exists( $template ) ) {
            include $template;
        }
        ?>

        <p style="text-align: center; margin-top: 30px; padding-top: 20px; border-top: 1px solid var(--glass-border);">
            <?php
            $myaccount_url = function_exists( 'wc_get_page_permalink' )
                ? wc_get_page_permalink( 'myaccount' )
                : home_url( '/' );
            printf(
                /* translators: %s: login URL */
                esc_html__( 'Already have an account? %sLog in here.%s', 'subscription-manager-for-stripe' ),
                '<a href="' . esc_url( $myaccount_url ) . '">',
                '</a>'
            );
            ?>
        </p>
    </div>
</div>

<?php else : ?>

    <div class="custom-account-container">
        <div class="custom-account-content" style="max-width: 800px; margin: 0 auto; text-align: center; padding: 60px 20px;">
            <div class="strp-sub-custom-notice strp-sub-notice-info">
                <div class="notice-icon-wrapper">
                    <i data-lucide="info"></i>
                </div>
                <span>
                    <?php
                    $myaccount_url = function_exists( 'wc_get_page_permalink' )
                        ? wc_get_page_permalink( 'myaccount' )
                        : home_url( '/' );
                    printf(
                        /* translators: %s: account page URL */
                        esc_html__( 'You are already logged in. Go to your %saccount page%s to view your affiliate dashboard.', 'subscription-manager-for-stripe' ),
                        '<a href="' . esc_url( $myaccount_url ) . '">',
                        '</a>'
                    );
                    ?>
                </span>
            </div>
        </div>
    </div>

<?php endif; ?>

</main>

<?php get_footer(); ?>
