<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Detect which page builder/editor is used for a given post.
 *
 * @since 2.1.0
 */
class AI_Magic_Editor_Detector {

    /**
     * Detect editor for a post.
     *
     * @param int $post_id
     * @return string elementor|gutenberg|wpbakery|html
     */
    public static function detect_editor( $post_id ) {

        // Elementor stores JSON layout in _elementor_data meta
        if ( metadata_exists( 'post', $post_id, '_elementor_data' ) ) {
            $data = get_post_meta( $post_id, '_elementor_data', true );
            if ( ! empty( $data ) ) {
                return 'elementor';
            }
        }

        $content = get_post_field( 'post_content', $post_id );

        // Gutenberg blocks contain <!-- wp:... -->
        if ( is_string( $content ) && strpos( $content, '<!-- wp:' ) !== false ) {
            return 'gutenberg';
        }

        // WPBakery (Visual Composer) uses vc_row / vc_column shortcodes
        if ( is_string( $content ) && ( has_shortcode( $content, 'vc_row' ) || has_shortcode( $content, 'vc_column' ) ) ) {
            return 'wpbakery';
        }

        // Fallback: classic editor / plain HTML
        return 'html';
    }

}
