<?php
/**
 * Template part – Newsletter / CTA Section
 *
 * Layout: Centered (free) | Split / Glassmorphism (Pro)
 *
 * @package Blog_Over
 * @since   1.1.0
 */

// ── Guard: enabled? ──────────────────────────────────────────────────────────
if ( ! get_theme_mod( 'blog_over_newsletter_enable', false ) ) {
    return;
}

// ── Guard: page visibility ───────────────────────────────────────────────────
$show_on    = get_theme_mod( 'blog_over_newsletter_show_on', array( 'home', 'blog' ) );
$show_on    = is_array( $show_on ) ? $show_on : array();
$should_show = false;

if ( in_array( 'home',    $show_on ) && is_front_page() )                                              $should_show = true;
if ( in_array( 'blog',    $show_on ) && is_home() && ! is_front_page() )                               $should_show = true;
if ( in_array( 'archive', $show_on ) && ( is_archive() || is_category() || is_tag() || is_date() ) )   $should_show = true;
if ( in_array( 'single',  $show_on ) && is_singular( 'post' ) )                                        $should_show = true;
if ( in_array( 'page',    $show_on ) && is_page() )                                                    $should_show = true;

if ( ! $should_show ) {
    return;
}

// ── Gather settings ──────────────────────────────────────────────────────────
$layout         = get_theme_mod( 'blog_over_newsletter_layout',          'centered' );

// Fallback to centered if Pro layout is selected but Pro plugin is inactive
if ( $layout !== 'centered' && ! has_action( 'blog_over_newsletter_layout_content' ) ) {
    $layout = 'centered';
}
$pre_heading    = get_theme_mod( 'blog_over_newsletter_pre_heading',     __( 'Join Our Community', 'blog-over' ) );
$heading        = get_theme_mod( 'blog_over_newsletter_heading',         __( 'Never Miss an Article', 'blog-over' ) );
$subtext        = get_theme_mod( 'blog_over_newsletter_subtext',         '' );
$shortcode      = get_theme_mod( 'blog_over_newsletter_shortcode',       '' );
$bg_type        = get_theme_mod( 'blog_over_newsletter_bg_type',         'gradient' );
$bg_color       = get_theme_mod( 'blog_over_newsletter_bg_color',        '#1e293b' );
$grad_start     = get_theme_mod( 'blog_over_newsletter_gradient_start',  '#1e40af' );
$grad_end       = get_theme_mod( 'blog_over_newsletter_gradient_end',    '#7c3aed' );
$text_mode      = get_theme_mod( 'blog_over_newsletter_text_color',      'light' );
$section_width  = get_theme_mod( 'blog_over_newsletter_section_width',   'boxed' );

// ── Inline background style ──────────────────────────────────────────────────
if ( $bg_type === 'gradient' ) {
    $bg_style = "background: linear-gradient(135deg, {$grad_start} 0%, {$grad_end} 100%);";
} elseif ( $bg_type === 'image_overlay' ) {
    $bg_image   = get_theme_mod( 'blog_over_newsletter_bg_image', '' );
    $bg_opacity = absint( get_theme_mod( 'blog_over_newsletter_bg_image_opacity', 70 ) );
    $overlay_rgba = 'rgba(0,0,0,' . round( $bg_opacity / 100, 2 ) . ')';
    $bg_style = $bg_image
        ? "background: linear-gradient({$overlay_rgba}, {$overlay_rgba}), url(" . esc_url( $bg_image ) . ") center/cover no-repeat;"
        : "background-color: {$bg_color};";
} elseif ( $bg_type === 'dark_glass' ) {
    $bg_style = "background: rgba(15, 23, 42, 0.85); backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);";
} else {
    $bg_style = "background-color: {$bg_color};";
}

// ── Container class ──────────────────────────────────────────────────────────
$container_class = ( $section_width === 'full-width' )
    ? 'blog-over-container blog-over-container-wide'
    : 'blog-over-container';

$layout_class    = 'blog-over-newsletter-layout-' . esc_attr( $layout );
$text_mode_class = 'blog-over-newsletter-text-' . esc_attr( $text_mode );
?>

<div class="blog-over-newsletter-wrapper <?php echo esc_attr( $layout_class . ' ' . $text_mode_class ); ?>"
     style="<?php echo esc_attr( $bg_style ); ?>">

    <div class="blog-over-newsletter-inner">
        <div class="<?php echo esc_attr( $container_class ); ?>">

            <?php if ( $layout === 'centered' ) : ?>

                <div class="blog-over-newsletter-centered">

                    <?php if ( $pre_heading ) : ?>
                        <p class="blog-over-newsletter-pre-heading"><?php echo esc_html( $pre_heading ); ?></p>
                    <?php endif; ?>

                    <?php if ( $heading ) : ?>
                        <h2 class="blog-over-newsletter-heading"><?php echo esc_html( $heading ); ?></h2>
                    <?php endif; ?>

                    <?php if ( $subtext ) : ?>
                        <p class="blog-over-newsletter-subtext"><?php echo esc_html( $subtext ); ?></p>
                    <?php endif; ?>

                    <?php if ( $shortcode ) : ?>
                        <div class="blog-over-newsletter-form">
                            <?php echo do_shortcode( wp_kses_post( $shortcode ) ); ?>
                        </div>
                    <?php else : ?>
                        <div class="blog-over-newsletter-form blog-over-newsletter-form-placeholder">
                            <p class="blog-over-newsletter-form-hint">
                                <i class="fas fa-info-circle" aria-hidden="true"></i>
                                <?php esc_html_e( 'Enter your newsletter form shortcode in the Customizer → Newsletter / CTA Section.', 'blog-over' ); ?>
                            </p>
                        </div>
                    <?php endif; ?>

                </div><!-- .blog-over-newsletter-centered -->

            <?php else : ?>
                <?php
                /**
                 * Pro layouts (split, glassmorphism, etc.) are rendered by the
                 * Pro plugin hooking into this action.
                 */
                do_action( 'blog_over_newsletter_layout_content', $layout, compact(
                    'pre_heading', 'heading', 'subtext', 'shortcode'
                ) );
                ?>
            <?php endif; ?>

        </div><!-- .container -->
    </div><!-- .blog-over-newsletter-inner -->

    <!-- Decorative background circles (CSS-driven) -->
    <span class="blog-over-newsletter-deco blog-over-newsletter-deco-1" aria-hidden="true"></span>
    <span class="blog-over-newsletter-deco blog-over-newsletter-deco-2" aria-hidden="true"></span>

</div><!-- .blog-over-newsletter-wrapper -->
