<?php
/**
 * DBA_LATAM_Shortcodes
 *
 * Additional shortcodes specific to the LATAM module.
 * The existing [damalion_geo_blog country="brazil"] already works
 * for single-country display. This file adds:
 *
 *   [damalion_latam_blog]          — All LATAM posts across all countries
 *   [damalion_latam_blog limit=6]  — With custom limit
 *
 * @since 2.11.0
 */

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

class DBA_LATAM_Shortcodes {

    private static $instance = null;

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

    private function __construct() {
        add_shortcode( 'damalion_latam_blog', array( $this, 'render_latam_grid' ) );
    }

    /**
     * Render a grid of all LATAM investment posts across all countries.
     *
     * Usage:
     *   [damalion_latam_blog]
     *   [damalion_latam_blog limit="12"]
     *   [damalion_latam_blog country="brazil,mexico,argentina"]
     */
    public function render_latam_grid( $atts ) {
        $atts = shortcode_atts( array(
            'limit'   => 12,
            'country' => '',  // comma-separated filter
        ), $atts );

        $meta_query = array(
            array( 'key' => '_dba_latam_auto_created', 'compare' => 'EXISTS' ),
        );

        // Filter to specific countries
        if ( ! empty( $atts['country'] ) ) {
            $country_keys = array_map( 'sanitize_key', explode( ',', $atts['country'] ) );
            $meta_query[] = array(
                'key'     => '_dba_latam_country',
                'value'   => $country_keys,
                'compare' => 'IN',
            );
        }

        $query = new WP_Query( array(
            'post_type'      => 'post',
            'posts_per_page' => intval( $atts['limit'] ),
            'post_status'    => 'publish',
            'meta_query'     => $meta_query,
            'orderby'        => 'date',
            'order'          => 'DESC',
        ) );

        if ( ! $query->have_posts() ) {
            return '<p style="text-align:center;color:#666;padding:40px;">No Latin America investment posts available yet.</p>';
        }

        ob_start();
        ?>
        <div class="dba-latam-grid" style="display:grid;gap:24px;grid-template-columns:repeat(auto-fill,minmax(340px,1fr));margin:20px 0;">
            <?php while ( $query->have_posts() ) : $query->the_post();
                $country_key = get_post_meta( get_the_ID(), '_dba_latam_country', true );
                $city        = get_post_meta( get_the_ID(), '_dba_latam_city', true );
                $region      = get_post_meta( get_the_ID(), '_dba_latam_region', true );
                $config      = $country_key ? DBA_LATAM_Registry::get_country( $country_key ) : null;
                $label       = $config ? $config['label'] : ucfirst( $country_key );
            ?>
            <article class="dba-latam-card" style="background:#fff;border:1px solid #e0e0e0;border-radius:10px;overflow:hidden;box-shadow:0 2px 12px rgba(0,0,0,0.08);transition:transform .2s;">
                <?php if ( has_post_thumbnail() ) : ?>
                <div style="height:220px;overflow:hidden;">
                    <?php the_post_thumbnail( 'medium_large', array( 'style' => 'width:100%;height:220px;object-fit:cover;' ) ); ?>
                </div>
                <?php endif; ?>
                <div style="padding:20px;">
                    <?php if ( $label ) : ?>
                    <span style="display:inline-block;background:#1a365d;color:#fff;padding:3px 10px;border-radius:4px;font-size:11px;text-transform:uppercase;margin-bottom:8px;"><?php echo esc_html( $label ); ?></span>
                    <?php endif; ?>
                    <?php if ( $city ) : ?>
                    <span style="display:inline-block;background:#4a7c59;color:#fff;padding:3px 10px;border-radius:4px;font-size:11px;text-transform:uppercase;margin-bottom:8px;margin-left:6px;"><?php echo esc_html( $city ); ?></span>
                    <?php endif; ?>
                    <h3 style="margin:10px 0;line-height:1.3;">
                        <a href="<?php the_permalink(); ?>" style="text-decoration:none;color:#1a202c;"><?php the_title(); ?></a>
                    </h3>
                    <p style="color:#666;font-size:14px;line-height:1.5;margin:0 0 15px;"><?php echo wp_trim_words( get_the_excerpt(), 25 ); ?></p>
                    <div style="display:flex;justify-content:space-between;align-items:center;">
                        <span style="color:#999;font-size:12px;"><?php echo get_the_date(); ?></span>
                        <a href="<?php the_permalink(); ?>" style="color:#1a365d;font-weight:600;text-decoration:none;font-size:14px;">Read more →</a>
                    </div>
                </div>
            </article>
            <?php endwhile; wp_reset_postdata(); ?>
        </div>
        <?php
        return ob_get_clean();
    }
}
