<?php

namespace WPVOID\StockBeacon\Core;

use WPVOID\StockBeacon\Settings;
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Shared rules for storefront AJAX and REST signup so both paths stay aligned.
 */
class SignupValidator {
    /**
     * Canonical email for storage and lookups (trim, sanitize, lowercase).
     */
    public static function normalize_email( string $raw ) : string {
        $e = sanitize_email( trim( wp_unslash( $raw ) ) );
        if ( !is_email( $e ) ) {
            return '';
        }
        return strtolower( $e );
    }

    public static function is_waitlist_hidden( int $product_id ) : bool {
        return 'yes' === get_post_meta( $product_id, '_stockbeacon_hide_waitlist', true );
    }

    /**
     * Whether the merchant requires an explicit consent flag on subscribe.
     */
    public static function consent_required() : bool {
        return !empty( Settings::get()['show_consent_checkbox'] );
    }

    /**
     * Whether a waitlist may be shown and accept signups for this stock line.
     * Out-of-stock products are available in every edition. The force and
     * low-stock paths are retained only in the paid package by Freemius.
     */
    public static function is_waitlist_available( \WC_Product $product, int $configuration_product_id = 0 ) : bool {
        if ( !$product->is_in_stock() ) {
            return true;
        }
        $available = false;
        return $available;
    }

    /**
     * @param int $product_id Product post ID.
     * @param int $variation_id Variation ID, or 0 for simple products.
     * @return true|\WP_Error True if eligible; WP_Error with message + optional status in data.
     */
    public static function validate_subscribe_eligibility( int $product_id, int $variation_id ) {
        $product = wc_get_product( $product_id );
        if ( !$product instanceof \WC_Product ) {
            return new \WP_Error('stockbeacon_invalid_product', __( 'This product is not currently eligible for waitlist signup.', 'stockbeacon-product-waitlists-for-woocommerce' ), array(
                'status' => 400,
            ));
        }
        if ( 'publish' !== get_post_status( $product_id ) ) {
            return new \WP_Error('stockbeacon_invalid_product', __( 'This product is not currently eligible for waitlist signup.', 'stockbeacon-product-waitlists-for-woocommerce' ), array(
                'status' => 400,
            ));
        }
        if ( self::is_waitlist_hidden( $product_id ) ) {
            return new \WP_Error('stockbeacon_waitlist_hidden', __( 'This product is not currently eligible for waitlist signup.', 'stockbeacon-product-waitlists-for-woocommerce' ), array(
                'status' => 400,
            ));
        }
        if ( $variation_id > 0 ) {
            $variation = wc_get_product( $variation_id );
            if ( !$variation instanceof \WC_Product_Variation ) {
                return new \WP_Error('stockbeacon_invalid_variation', __( 'This product is not currently eligible for waitlist signup.', 'stockbeacon-product-waitlists-for-woocommerce' ), array(
                    'status' => 400,
                ));
            }
            if ( (int) $variation->get_parent_id() !== $product_id ) {
                return new \WP_Error('stockbeacon_variation_mismatch', __( 'This product is not currently eligible for waitlist signup.', 'stockbeacon-product-waitlists-for-woocommerce' ), array(
                    'status' => 400,
                ));
            }
            if ( !self::is_waitlist_available( $variation, $product_id ) ) {
                return new \WP_Error('stockbeacon_not_eligible', __( 'This product is not currently eligible for waitlist signup.', 'stockbeacon-product-waitlists-for-woocommerce' ), array(
                    'status' => 400,
                ));
            }
            return true;
        }
        if ( !self::is_waitlist_available( $product, $product_id ) ) {
            return new \WP_Error('stockbeacon_not_eligible', __( 'This product is not currently eligible for waitlist signup.', 'stockbeacon-product-waitlists-for-woocommerce' ), array(
                'status' => 400,
            ));
        }
        return true;
    }

}
