<?php
namespace Softech_Addons_for_Elementor\Includes\Dynamic_Tags\Tags\Post_Tags;

use Elementor\Core\DynamicTags\Tag;
use Elementor\Modules\DynamicTags\Module;
use Softech_Addons_for_Elementor\Includes\Dynamic_Tags\Groups\Softech_Post_Group;

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

class Softech_Post_Content extends Tag {

    public function get_name() {
        return 'softech-post-content';
    }

    public function get_title() {
        return esc_html__( 'Post Content', 'softech-addons-for-elementor' );
    }

    public function get_group() {
        return Softech_Post_Group::get_name();
    }

    public function get_categories() {
        return [ Module::TEXT_CATEGORY ];
    }

    public function register_controls() {

        $this->add_control(
            'content_length',
            [
                'label'   => esc_html__( 'Word Limit (0 = full content)', 'softech-addons-for-elementor' ),
                'type'    => \Elementor\Controls_Manager::NUMBER,
                'min'     => 0,
                'max'     => 500,
                'default' => 0,
            ]
        );

        $this->add_control(
            'content_suffix',
            [
                'label'   => esc_html__( 'Suffix (e.g. ...)', 'softech-addons-for-elementor' ),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => '...',
            ]
        );

        $this->add_control(
            'allow_html',
            [
                'label'        => esc_html__( 'Allow HTML Tags', 'softech-addons-for-elementor' ),
                'type'         => \Elementor\Controls_Manager::SWITCHER,
                'label_on'     => esc_html__( 'Yes', 'softech-addons-for-elementor' ),
                'label_off'    => esc_html__( 'No', 'softech-addons-for-elementor' ),
                'return_value' => 'yes',
                'default'      => '',
            ]
        );

    }

public function get_value( array $options = [] ) {

    $length     = (int) $this->get_settings( 'content_length' );
    $suffix     = $this->get_settings( 'content_suffix' );
    $allow_html = $this->get_settings( 'allow_html' );

    // ✅ Get the post content safely
    $post    = get_post();
    $content = isset( $post->post_content ) ? $post->post_content : '';

    // ✅ Check if this post is built with Elementor — if so, fall back to excerpt
    $is_elementor = get_post_meta( get_the_ID(), '_elementor_edit_mode', true );

    if ( $is_elementor ) {
        // Elementor pages store JSON/CSS in post_content — use excerpt instead
        $content = get_the_excerpt();
    } else {
        // Non-Elementor post: safe to apply content filters
        if ( 'yes' === $allow_html ) {
            $content = apply_filters( 'the_content', $content );
        }
    }

    // Strip HTML if not allowed
    if ( 'yes' !== $allow_html ) {
        $content = wp_strip_all_tags( $content );
    }

    $content = trim( $content );

    // Return full content if no word limit
    if ( $length < 1 ) {
        return $content;
    }

    // Trim to word limit
    $words   = explode( ' ', $content );
    $trimmed = array_slice( $words, 0, $length );
    $output  = implode( ' ', $trimmed );

    // Append suffix only if actually trimmed
    if ( count( $words ) > $length ) {
        $output .= $suffix;
    }

    return $output;
}

public function render() {
    $allow_html      = $this->get_settings( 'allow_html' );
    $is_elementor    = get_post_meta( get_the_ID(), '_elementor_edit_mode', true );

    if ( 'yes' === $allow_html && ! $is_elementor ) {
        echo wp_kses_post( $this->get_value() );
    } else {
        echo esc_html( $this->get_value() );
    }
}

}