<?php

namespace fluXtore\Core\Bricks;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( ! class_exists( 'fluXtore_Bricks_Integration' ) ) {
    class fluXtore_Bricks_Integration {
        /**
         * @var self|null
         */
        private static $instance = null;

        private function __construct() {}

        public static function init() {
            if ( null !== self::$instance ) {
                return;
            }

            self::$instance = new self();

            add_action( 'after_setup_theme', [ self::$instance, 'setup_bricks' ] );
        }

        public function setup_bricks() {
            if ( ! defined( 'FLUXTORE_DEFAULT_EDITIOR' ) || 'Bricks' !== FLUXTORE_DEFAULT_EDITIOR ) {
                return;
            }

            $theme        = wp_get_theme();
            $parent_theme = is_child_theme() && $theme->parent() ? $theme->parent()->get( 'Name' ) : $theme->get( 'Name' );

            if ( 'Bricks' !== $parent_theme ) {
                return;
            }

            // Ensure fluxtore_step is enabled in Bricks global settings
            $this->enable_fluxtore_step_in_settings();

            add_filter( 'bricks/builder/post_types', [ $this, 'enable_fluxtore_step_post_type' ] );
            add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_back_button_script' ] );
        }

        /**
         * Enable fluxtore_step post type in Bricks global settings
         */
        private function enable_fluxtore_step_in_settings() {
            $global_settings = get_option( 'bricks_global_settings', [] );

            // Ensure postTypes key exists
            if ( ! isset( $global_settings['postTypes'] ) ) {
                $global_settings['postTypes'] = [];
            }

            // Add fluxtore_step if not already present
            if ( ! in_array( 'fluxtore_step', $global_settings['postTypes'], true ) ) {
                $global_settings['postTypes'][] = 'fluxtore_step';
                update_option( 'bricks_global_settings', $global_settings );
            }
        }

        /**
         * Enable Bricks builder for fluxtore_step post type
         *
         * @param array $post_types Array of post types enabled for Bricks
         * @return array Modified array of post types
         */
        public function enable_fluxtore_step_post_type( $post_types ) {
            if ( ! in_array( 'fluxtore_step', $post_types, true ) ) {
                $post_types[] = 'fluxtore_step';
            }
            return $post_types;
        }

        public function enqueue_back_button_script() {
            if ( is_admin() ) {
                return;
            }

            global $post;

            if ( ! $post || 'fluxtore_step' !== get_post_type( $post ) ) {
                return;
            }

            $funnel_id = get_post_meta( $post->ID, '__fluxtore__flow', true );

            if ( empty( $funnel_id ) ) {
                return;
            }

            $back_url = admin_url( 'admin.php?page=fluxtore#/' . $funnel_id . '/' . $post->ID . '/editstep' );

            wp_enqueue_script( 'custom-back-button', FLUXTORE_ASSETS_URL . '/js/custom-back-button.js', [ 'jquery' ], null, true );
            wp_localize_script(
                'custom-back-button',
                'fluxtoreVars',
                [
                    'funnelId' => $funnel_id,
                    'backUrl'  => $back_url,
                ]
            );
        }
    }
}

fluXtore_Bricks_Integration::init();
