<?php

namespace Fab\Controller;

use Fab\Interfaces\JIT_Interface;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * WC Quick Purchase Frontend
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class WC_Quick_Purchase_Frontend extends WC_Quick_Purchase_Type implements JIT_Interface {

    /**
     * Adds WooCommerce Features data to the FAB item instance.
     *
     * @param object $instance The FAB item instance.
     */
    public function add_fab_item_data( $instance ) {
        // Check if type is quick purchase.
        if ( $this->type !== $instance->getType() ) {
            return;
        }

        // Get data.
        $quick_purchase_id = get_post_meta( $instance->getID(), $this->post_meta['action']['meta_key'], true );
        $default_product   = get_post_meta( $instance->getID(), $this->post_meta['product']['meta_key'], true );

        // Early return if not on product page and no default product is set.
        if ( ! is_product() && ! $default_product ) {
            $instance->setToBeDisplayed( false );
            return;
        }

        // Set product id from default product if not on product page.
        $product_id = is_product() ? get_the_ID() : $default_product;

        // Set link.
        $instance->setLink( add_query_arg( $quick_purchase_id, $product_id, home_url( add_query_arg( null, null ) ) ) );

        // Set Link Href.
        $instance->setLinkHref( true );
    }

    /**
     * Handles quick purchase actions from URL parameters.
     *
     * This function checks for `fab_add_to_cart` or `fab_buy_now` parameters in the URL.
     * - If `fab_add_to_cart` is set, the product is added to the WooCommerce cart, and the user is redirected to the cart page.
     * - If `fab_buy_now` is set, the product is added to the WooCommerce cart, and the user is redirected to the checkout page.
     *
     * @return void
     */
    public function quick_purchase_from_url() {
        // Determine the parameter and its corresponding redirect URL.
        $actions = array(
            'fab_add_to_cart' => wc_get_cart_url(),
            'fab_buy_now'     => wc_get_checkout_url(),
        );

        foreach ( $actions as $param => $redirect_url ) {
            if ( isset( $_GET[ $param ] ) && ! empty( $_GET[ $param ] ) ) {
                $product_id = sanitize_text_field( wp_unslash( $_GET[ $param ] ) );

                // If "fab_buy_now", empty the cart before adding the product.
                if ( 'fab_buy_now' === $param ) {
                    WC()->cart->empty_cart();
                }

                // Add the product to the cart.
                WC()->cart->add_to_cart( $product_id );

                // Redirect to the appropriate page.
                wp_safe_redirect( $redirect_url );
                exit;
            }
        }
    }

    /*
    |--------------------------------------------------------------------------
    | Fulfill implemented interface contracts
    |--------------------------------------------------------------------------
     */

    /**
     * JIT Method.
     *
     * @return void
     */
    public static function jit() {
        // Prevent error if woocommerce plugin is not active.
        if ( ! FAB_WOOCOMMERCE_PLUGIN_ACTIVE ) {
            return;
        }

        // Add fab item data.
        add_action( 'fab_item_data', array( __CLASS__, 'jit_add_fab_item_data' ), 10, 1 );

        // Quick purchase from URL.
        add_action( 'template_redirect', array( __CLASS__, 'jit_quick_purchase_from_url' ) );
    }
}
