<?php

defined( 'ABSPATH' ) or exit;

final class PWLPAR_Actions {

    function __construct() {
        add_action( 'woocommerce_init', array( $this, 'woocommerce_init' ) );
    }

    function woocommerce_init() {
        if ( !pwlpar_is_active_or_admin() ) {
            return;
        }

        add_action( 'template_redirect', array( $this, 'handle_actions' ) );
    }

    function handle_actions() {
        // Nonce validation is not required for these actions since they are string comparisons.
        // phpcs:disable WordPress.Security.NonceVerification.Recommended
        if ( isset( $_REQUEST['pwlpar_action'] ) ) {
            switch ( $_REQUEST['pwlpar_action'] ) {
                case 'join':
                    if ( !is_user_logged_in() ) {
                        // Let the user sign in or create an account.
                        $redirect = add_query_arg( 'pwlpar_action', 'join', pwlpar_get_home_url() );

                        $url = add_query_arg( 'pwlpar_redirect', urlencode( $redirect ), wc_get_page_permalink( 'myaccount' ) );

                        wp_safe_redirect( $url );
                        die();
                    } else if ( !pwlpar_current_user_is_rewards_member() ) {

                        $result = pwlpar_enroll_user( get_current_user_id() );
                        if ( $result === true ) {
                            /* translators: %s is the rewards program name */
                            wc_add_notice( sprintf( __( 'Welcome to %s!', 'pw-loyalty-points-and-rewards-for-woocommerce' ), pwlpar_program_name(), 'success' ) );
                        } else {
                            wc_add_notice( $result, 'error' );
                        }
                    }
                break;

                case 'apply_reward':
                    if ( isset( $_REQUEST['reward_id'] ) ) {
                        $reward = pwlpar_get_reward( absint( $_REQUEST['reward_id'] ) );
                        if ( $reward && $reward->get_user_id() == get_current_user_id() ) {
                            pwlpar_apply_reward( $reward );
                        } else {
                            wc_add_notice( __( 'You do not have permission to apply this reward.', 'pw-loyalty-points-and-rewards-for-woocommerce' ), 'error' );
                        }
                    }
                break;

                case 'remove_reward':
                    if ( isset( $_REQUEST['reward_id'] ) ) {
                        $reward = pwlpar_get_reward( absint( $_REQUEST['reward_id'] ) );
                        if ( pwlpar_remove_reward( $reward ) ) {
                            wc_add_notice( __( 'Reward coupon removed from cart.', 'pw-loyalty-points-and-rewards-for-woocommerce' ), 'success' );
                        }
                    }
                break;

                case 'claim_offer':
                    if ( isset( $_REQUEST['offer_id'] ) ) {
                        $offer_id = absint( $_REQUEST['offer_id'] );
                        $result = pwlpar_claim_offer( $offer_id );

                        if ( $result !== true ) {
                            pwlpar_log( 'Offer could not be claimed: ' . $result );
                            /* translators: %s is the error message. */
                            wc_add_notice( sprintf( __( 'Offer could not be claimed. %s', 'pw-loyalty-points-and-rewards-for-woocommerce' ), $result ), 'error' );
                        }
                    }
                break;
            }

            wp_safe_redirect( pwlpar_get_home_url() );
            die();
        }
        // phpcs:enable
    }
}

return new PWLPAR_Actions();
