<?php
/**
 * TitanCart product option template (single option row).
 *
 * Override: {theme}/titancart/product/option.php
 *
 * @var array $option      Option row from ProductRepository::get_options().
 * @var int   $product_id
 * @package TitanCart
 */

defined( 'ABSPATH' ) || exit;

$option_id   = (int) $option['id'];
$type        = $option['type'] ?? 'S';
$name        = $option['name'] ?? '';
$required    = ! empty( $option['required'] );
$variants    = $option['variants'] ?? [];
$input_name  = "tc_option[{$option_id}]";
$input_id    = "tc-option-{$product_id}-{$option_id}";
$_show_mods  = $show_option_modifiers ?? \TitanCart\Services\SettingsService::bool( 'general', 'catalog_display_option_modifiers', true );

// S141 — Sequential Options PDP wiring. Encode the option's dependency edges
// onto the wrapper as a `data-tc-deps` attribute so the sequential-options JS
// module can build its evaluation map without a roundtrip to the REST API.
//
// Shape: array of { parent_option_id: int, parent_variant_id: int }, mirroring
// the REST contract from ProductRepository::get_options(). Empty deps array
// emits no attribute (cleaner DOM; the JS module treats absent attribute as
// "no deps" — same as `[]`). Most products on most stores will have zero deps,
// so absent attribute is the common case.
$_tc_deps = isset( $option['dependencies'] ) && is_array( $option['dependencies'] )
    ? $option['dependencies']
    : [];
// WP.org R2 item 6: hoist the JSON value (not an attribute fragment) so the
// echo site can esc_attr inline — always emit the attribute; empty deps become
// data-tc-deps="" which the JS already treats as no-deps.
$_tc_deps_json = ! empty( $_tc_deps ) ? (string) wp_json_encode( $_tc_deps, JSON_UNESCAPED_SLASHES ) : '';
?>
<div class="tc-product-option tc-product-option--<?php echo esc_attr( strtolower( $type ) ); ?>"
     data-option-id="<?php echo esc_attr( $option_id ); ?>"
     data-required="<?php echo $required ? '1' : '0'; ?>"<?php if ( $_tc_deps_json !== '' ) : ?> data-tc-deps="<?php echo esc_attr( $_tc_deps_json ); ?>"<?php endif; ?>>

    <label class="tc-product-option__label">
        <?php echo esc_html( $name ); ?>
        <?php if ( $required ) : ?>
            <span class="tc-product-option__required" aria-hidden="true">*</span>
        <?php endif; ?>
    </label>

    <?php if ( in_array( $type, [ 'S', 'R' ], true ) && ! empty( $variants ) ) : ?>

        <?php if ( $type === 'S' ) : ?>
            <select name="<?php echo esc_attr( $input_name ); ?>"
                    id="<?php echo esc_attr( $input_id ); ?>"
                    class="tc-product-option__select"
                    <?php if ( $required ) echo 'required'; ?>>
                <option value=""><?php esc_html_e( '-- Select --', 'titancart-lite' ); ?></option>
                <?php foreach ( $variants as $v ) :
                    if ( $v['status'] !== 'active' ) continue; ?>
                    <option value="<?php echo esc_attr( $v['id'] ); ?>"
                            data-modifier="<?php echo esc_attr( $v['modifier'] ); ?>"
                            data-modifier-type="<?php echo esc_attr( $v['modifier_type'] ); ?>">
                        <?php echo esc_html( $v['name'] ); ?>
                        <?php if ( $_show_mods && (float) $v['modifier'] !== 0.0 ) :
                            $sign = (float) $v['modifier'] > 0 ? '+' : '';
                            $mod  = $v['modifier_type'] === 'P'
                                ? $sign . (float) $v['modifier'] . '%'
                                : $sign . number_format( (float) $v['modifier'], 2 );
                            echo esc_html( " ({$mod})" );
                        endif; ?>
                    </option>
                <?php endforeach; ?>
            </select>

        <?php else : // Radio buttons ?>
            <div class="tc-product-option__radios" role="radiogroup" aria-label="<?php echo esc_attr( $name ); ?>">
                <?php foreach ( $variants as $v ) :
                    if ( $v['status'] !== 'active' ) continue;
                    $radio_id = $input_id . '-' . $v['id'];
                ?>
                    <label class="tc-product-option__radio-label" for="<?php echo esc_attr( $radio_id ); ?>">
                        <input type="radio"
                               id="<?php echo esc_attr( $radio_id ); ?>"
                               name="<?php echo esc_attr( $input_name ); ?>"
                               value="<?php echo esc_attr( $v['id'] ); ?>"
                               class="tc-product-option__radio"
                               data-modifier="<?php echo esc_attr( $v['modifier'] ); ?>"
                               data-modifier-type="<?php echo esc_attr( $v['modifier_type'] ); ?>"
                               <?php if ( $required ) echo 'required'; ?> />
                        <?php echo esc_html( $v['name'] ); ?>
                        <?php if ( $_show_mods && (float) $v['modifier'] !== 0.0 ) :
                            $sign = (float) $v['modifier'] > 0 ? '+' : '';
                            $mod  = $v['modifier_type'] === 'P'
                                ? $sign . (float) $v['modifier'] . '%'
                                : $sign . number_format( (float) $v['modifier'], 2 );
                        ?>
                            <span class="tc-product-option__mod"><?php echo esc_html( "({$mod})" ); ?></span>
                        <?php endif; ?>
                    </label>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>

    <?php elseif ( $type === 'C' && ! empty( $variants ) ) : ?>
        <div class="tc-product-option__checkboxes">
            <?php foreach ( $variants as $v ) :
                if ( $v['status'] !== 'active' ) continue;
                $cb_id = $input_id . '-' . $v['id'];
            ?>
                <label class="tc-product-option__checkbox-label" for="<?php echo esc_attr( $cb_id ); ?>">
                    <input type="checkbox"
                           id="<?php echo esc_attr( $cb_id ); ?>"
                           name="<?php echo esc_attr( $input_name ); ?>[]"
                           value="<?php echo esc_attr( $v['id'] ); ?>"
                           class="tc-product-option__checkbox"
                           data-modifier="<?php echo esc_attr( $v['modifier'] ); ?>"
                           data-modifier-type="<?php echo esc_attr( $v['modifier_type'] ); ?>" />
                    <?php echo esc_html( $v['name'] ); ?>
                    <?php if ( $_show_mods && (float) $v['modifier'] !== 0.0 ) :
                        $sign = (float) $v['modifier'] > 0 ? '+' : '';
                        $mod  = $v['modifier_type'] === 'P'
                            ? $sign . (float) $v['modifier'] . '%'
                            : $sign . number_format( (float) $v['modifier'], 2 );
                    ?>
                        <span class="tc-product-option__mod"><?php echo esc_html( "({$mod})" ); ?></span>
                    <?php endif; ?>
                </label>
            <?php endforeach; ?>
        </div>

    <?php elseif ( $type === 'I' ) : ?>
        <input type="text"
               name="<?php echo esc_attr( $input_name ); ?>"
               id="<?php echo esc_attr( $input_id ); ?>"
               class="tc-product-option__input"
               <?php if ( $required ) echo 'required'; ?> />

    <?php elseif ( $type === 'T' ) : ?>
        <textarea name="<?php echo esc_attr( $input_name ); ?>"
                  id="<?php echo esc_attr( $input_id ); ?>"
                  class="tc-product-option__textarea"
                  rows="4"
                  <?php if ( $required ) echo 'required'; ?>></textarea>

    <?php elseif ( $type === 'F' ) : ?>
        <input type="file"
               name="<?php echo esc_attr( $input_name ); ?>"
               id="<?php echo esc_attr( $input_id ); ?>"
               class="tc-product-option__file"
               <?php if ( $required ) echo 'required'; ?> />
    <?php endif; ?>

</div>
