<?php

/**
 * Term meta sanitizer.
 *
 * @package B3WVS\VariationSwatches\Admin\TermMeta
 */
namespace B3WVS\VariationSwatches\Admin\TermMeta;

defined( 'ABSPATH' ) || exit;
/**
 * Term Meta Sanitizer - Handles saving and sanitizing term meta
 */
class Sanitizer {
    /**
     * Allowed swatch types
     */
    const ALLOWED_TYPES = array('', 'color', 'button');

    /**
     * Save term meta fields
     *
     * @param int $term_id Term ID.
     */
    public static function save( $term_id ) {
        if ( !self::verify_nonce() ) {
            return;
        }
        $type = self::sanitize_type();
        update_term_meta( $term_id, 'b3_wvs_type', $type );
        self::save_label_data( $term_id );
        // Default: clear all assets so frontend doesn't infer a type.
        if ( '' === $type ) {
            self::clear_all_meta( $term_id );
            \b3wvs_flush_usage_stats_cache();
            return;
        }
        // Handle each type-specific data.
        switch ( $type ) {
            case 'color':
            case 'button':
                self::save_color_data( $term_id );
                break;
            default:
                self::clear_all_meta( $term_id );
                break;
        }
        \b3wvs_flush_usage_stats_cache();
    }

    /**
     * Verify nonce
     *
     * @return bool
     */
    private static function verify_nonce() {
        return isset( $_POST['b3wvs_term_meta_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['b3wvs_term_meta_nonce'] ) ), 'b3wvs_save_term_meta' );
    }

    /**
     * Sanitize and validate the type field
     *
     * @return string
     */
    private static function sanitize_type() {
        // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified in save() before calling this method.
        $type = ( isset( $_POST['b3_wvs_type'] ) ? sanitize_text_field( (string) wp_unslash( $_POST['b3_wvs_type'] ) ) : '' );
        if ( !in_array( $type, self::ALLOWED_TYPES, true ) ) {
            $resolved_type = '';
            $type = $resolved_type;
        }
        return $type;
    }

    /**
     * Clear all term meta
     *
     * @param int $term_id Term ID.
     */
    private static function clear_all_meta( $term_id ) {
        delete_term_meta( $term_id, 'b3_wvs_color' );
    }

    /**
     * Save color/button data
     *
     * @param int $term_id Term ID.
     */
    private static function save_color_data( $term_id ) {
        // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified in save() before calling this method.
        $color = ( isset( $_POST['b3_wvs_color'] ) ? sanitize_text_field( (string) wp_unslash( $_POST['b3_wvs_color'] ) ) : '' );
        if ( '' === trim( $color ) ) {
            delete_term_meta( $term_id, 'b3_wvs_color' );
        } else {
            update_term_meta( $term_id, 'b3_wvs_color', $color );
        }
    }

    /**
     * Save taxonomy button label data.
     *
     * @param int $term_id Term ID.
     * @return void
     */
    private static function save_label_data( $term_id ) {
        // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified in save() before calling this method.
        $label = ( isset( $_POST['b3_wvs_label'] ) ? sanitize_text_field( (string) wp_unslash( $_POST['b3_wvs_label'] ) ) : '' );
        if ( '' === trim( $label ) ) {
            delete_term_meta( $term_id, 'b3_wvs_label' );
            return;
        }
        update_term_meta( $term_id, 'b3_wvs_label', $label );
    }

}
