<?php
defined( 'ABSPATH' ) || exit;

class Jalpi_Element_Text extends Jalpi_Element {
    public static function type(): string { return 'text'; }

    public function render( array $node, Jalpi_Renderer $renderer ): string {
        $id       = sanitize_html_class( $node['id'] ?? '' );
        $content  = $node['content']  ?? [];
        $styles   = $node['styles']   ?? [];
        $settings = $node['settings'] ?? [];
        $html     = wp_kses_post( $renderer->tags( $renderer->get_current_post_id() )->resolve( $content['html'] ?? $content['text'] ?? '' ) );
        $a        = $renderer->get_css_attrs( $node );

        if ( '' === trim( wp_strip_all_tags( $html ) ) ) {
            return '';
        }

        $sel   = '.jalpi-el.jalpi-' . $id;
        $bg_n  = $settings['bgNormal']      ?? [];
        $bg_h  = $settings['bgHover']       ?? [];
        $ov_n  = $settings['overlayNormal'] ?? [];
        $ov_h  = $settings['overlayHover']  ?? [];
        $bd_n  = $settings['borderNormal']  ?? [];
        $bd_h  = $settings['borderHover']   ?? [];
        $el_w  = sanitize_text_field( $settings['elWidth'] ?? '' );

        $css = "{$sel}{display:block;box-sizing:border-box;";
        if ( $el_w ) $css .= 'width:' . esc_attr( $el_w ) . ';';
        $css .= $renderer->bg_data_to_css( $bg_n );
        $css .= $renderer->border_data_to_css( $bd_n );
        $css .= $renderer->styles_to_css( $styles );
        $css .= '}';

        $hover = $renderer->bg_data_to_css( $bg_h ) . $renderer->border_data_to_css( $bd_h );
        if ( $hover ) $css .= "{$sel}:hover{{$hover}}";

        // Overlay via ::before pseudo-element
        $has_ov = ! empty( $ov_n['color'] ) || ! empty( $ov_n['imageUrl'] ) || ! empty( $ov_n['gradient'] );
        if ( $has_ov ) {
            $css .= "{$sel}{position:relative;overflow:hidden;}";
            $css .= "{$sel}::before{content:'';position:absolute;inset:0;z-index:0;pointer-events:none;";
            $css .= $renderer->bg_data_to_css( $ov_n );
            $css .= 'opacity:' . floatval( $ov_n['opacity'] ?? 0.5 ) . ';}';
            $ov_h_css = $renderer->bg_data_to_css( $ov_h );
            if ( $ov_h_css ) {
                $op_h = floatval( $ov_h['opacity'] ?? 0.5 );
                $css .= "{$sel}:hover::before{{$ov_h_css}opacity:{$op_h};}";
            }
        }

        // Text-color hover, link color, paragraph spacing rules.
        if ( ! empty( $styles['colorHover'] ) ) {
            $ch = Jalpi_Sanitizer::sanitize_color( $styles['colorHover'] );
            if ( $ch ) $css .= "{$sel}:hover{color:{$ch};}";
        }
        if ( ! empty( $styles['linkColor'] ) ) {
            $lc = Jalpi_Sanitizer::sanitize_color( $styles['linkColor'] );
            if ( $lc ) $css .= "{$sel} a{color:{$lc};}";
        }
        if ( ! empty( $styles['linkColorHover'] ) ) {
            $lch = Jalpi_Sanitizer::sanitize_color( $styles['linkColorHover'] );
            if ( $lch ) $css .= "{$sel} a:hover{color:{$lch};}";
        }
        if ( ! empty( $styles['paragraphSpacing'] ) ) {
            $ps = sanitize_text_field( $styles['paragraphSpacing'] );
            if ( $ps ) $css .= "{$sel} p{margin-bottom:{$ps};}";
        }

        $renderer->add_css( $css );

        // Video background rendered as absolute-positioned <video> inside the div
        $vid_html = '';
        if ( ( $bg_n['type'] ?? '' ) === 'video' && ! empty( $bg_n['videoUrl'] ) ) {
            $vu       = esc_url( $bg_n['videoUrl'] );
            $vid_html = '<video class="jalpi-el-bg-video" autoplay muted loop playsinline aria-hidden="true">'
                      . '<source src="' . $vu . '"></video>';
        }

        return '<div class="jalpi-el jalpi-' . esc_attr( $id ) . $a['extra_cls'] . '"' . $a['id_attr'] . '>'
             . $vid_html
             . $html
             . '</div>';
    }
}
