<?php

namespace Rtc\PlantShopWC\Admin\Modules;

use Rtc\PlantShopWC\Admin\Interfaces\ProductModule;
use WP_Post;

class PlantPassport implements ProductModule {


    public function slug(): string {
        return 'plant_passport';
    }

    public function label(): string {
        return __( 'Plant Passport', 'plantshop-wc' );
    }

    public function vars(): array {
        return array(
            array( 'traceability_code', __( 'Traceability code', 'plantshop-wc' ) ),
            array( 'origin_country', __( 'Origin country', 'plantshop-wc' ) ),
            array( 'part_e', __( 'Part E (QNIG NI→GB)', 'plantshop-wc' ) ),
        );
    }

    public function quick_edit_enabled(): bool {
        return true;
    }

    public function render( WP_Post $product ): void {

        $vars = self::vars();

        $values = self::get( $product->ID );

        ?>
        <div class="plantshop-wc-fields">
            <?php foreach ( $vars as $var_data ) { ?>
                <?php
                list( $var, $label ) = $var_data;

                $value = isset( $values[ $var ] ) ? $values[ $var ] : '';
                ?>
                <div class="plantshop-wc-field">
                    <label for="<?php echo esc_attr( 'plantshop-wc-' . $var ); ?>">
                        <?php echo esc_html( $label ); ?>
                    </label>
                    <input
                        type="text"
                        id="<?php echo esc_attr( 'plantshop-wc-' . $var ); ?>"
                        class="half"
                        name="<?php echo esc_attr( 'plantshop-wc_tab[' . $var . ']' ); ?>"
                        maxlength="10"
                        value="<?php echo esc_attr( $value ); ?>"
                    />
                    <?php if ( $var === 'part_e' ) { ?>
                        <p style="margin:4px 0 0; font-size:12px; color:#666;">
                            <?php echo esc_html__( 'Usually leave empty. Use GB(NI) when replacing a QNIG passport for NI→GB movement.', 'plantshop-wc' ); ?>
                        </p>
                    <?php } ?>
                </div>
            <?php } ?>
        </div>
        <?php
    }

    public function save( int $product_id, array $posted ): void {
        $vars     = self::vars();
        $slug     = self::slug();
        $existing = self::get( $product_id );
        $data     = is_array( $existing ) ? $existing : array();

        foreach ( $vars as $var_data ) {
            $var = $var_data[0];

            if ( ! array_key_exists( $var, $posted ) ) {
                continue;
            }

            $raw_value = $posted[ $var ];
            if ( is_array( $raw_value ) ) {
                $raw_value = '';
            }
            $data[ $var ] = sanitize_text_field( wp_unslash( $raw_value ) );
        }

        update_post_meta( $product_id, '_plantshop-wc_' . $slug, $data );
    }

    public function get( $product_id ): array {

        $slug = self::slug();

        $stored = get_post_meta( $product_id, '_plantshop-wc_' . $slug, true );
        return is_array( $stored ) ? $stored : array();
    }
}
