<?php

namespace Rtc\PlantShopWC\Admin\Controllers;

use Rtc\PlantShopWC\Admin\Models\ProductModel;
use Rtc\PlantShopWC\Admin\Views\ProductListView;
use WP_Post;

class ProductListController {


    private array $modules;
    private ProductListView $view;

    public function __construct() {

        $this->modules = self::set_quick_edit_modules();
        $this->view    = new ProductListView();
    }

    public function set_quick_edit_modules(): array {

        $quickEditModules = array();

        foreach ( ( new ProductModel() )->get() as $module ) {

            if ( $module->quick_edit_enabled() ) {
                $quickEditModules[] = $module;
            }
        }

        return $quickEditModules;
    }

    public function register_ajax_hooks(): void {

        add_action( 'save_post_product', array( $this, 'save_quick_edit' ), 10, 2 );
        add_action( 'manage_product_posts_custom_column', array( $this, 'inline_data' ), 10, 2 );
    }


    public function register_hooks(): void {

        add_action( 'woocommerce_product_quick_edit_end', array( $this, 'render_quick_edit' ) );
        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
    }

    public function render_quick_edit() {

        $this->view->render( $this->modules );
    }

    public function save_quick_edit( int $post_id, WP_Post $post ): void {

        // Standard save guards
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return;
        }
        if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) {
            return;
        }
        if ( $post->post_type !== 'product' ) {
            return;
        }
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }

        if (
            ! isset( $_POST['plantshop_wc_quick_edit_nonce'] ) ||
            ! wp_verify_nonce(
                sanitize_text_field( wp_unslash( $_POST['plantshop_wc_quick_edit_nonce'] ) ),
                'plantshop_wc_quick_edit'
            )
        ) {
            return;
        }

        $payload = array();
        if ( isset( $_POST['plantshop-wc'] ) && is_array( $_POST['plantshop-wc'] ) ) {
            $raw_payload = wp_unslash( $_POST['plantshop-wc'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitization handled by sanitize_deep().
            $payload     = self::sanitize_deep( $raw_payload );
        }

        foreach ( $this->modules as $m ) {
            $slug   = $m->slug();
            $posted = isset( $payload[ $slug ] ) && is_array( $payload[ $slug ] ) ? $payload[ $slug ] : array();
            $m->save( $post_id, $posted );
        }
    }

    public function inline_data( $column, $product_id ): void {

        if ( $column !== 'name' ) {
            return;
        }

        echo '<div class="hidden" id="plantshop-wc-inline_' . esc_html( $product_id ) . '">';

        foreach ( $this->modules as $m ) {

            foreach ( $m->get( $product_id ) as $key => $value ) {
                if ( is_array( $value ) ) {
                    continue;
                }
                ?>
                <span class="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $value ); ?></span>
                <?php
            }
        }

        echo '</div>';
    }

    public function enqueue( $hook ): void {

        global $typenow;

        if ( $hook === 'edit.php' && $typenow === 'product' ) {

            wp_enqueue_script( 'plantshop-wc-quick-edit', PLANTSHOP_WC_PUBLIC_URL . 'admin/js/plantshop-wc-quick-edit.js', array( 'jquery', 'inline-edit-post' ), '1.0', true );

        }
    }

    private static function sanitize_deep( $value ): array|string {
        if ( is_array( $value ) ) {
            foreach ( $value as $k => $v ) {
                $value[ $k ] = self::sanitize_deep( $v );
            }
            return $value;
        }
        // Scalars only; adjust if you need to allow HTML via wp_kses().
        return sanitize_text_field( $value );
    }
}
