<?php

if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Handle the [storyposts] shortcode.
 *
 * @since      1.0.0
 * @package    Storyposts
 * @subpackage Storyposts/includes/shortcodes
 */
class Storyposts_Shortcode {
    /**
     * Register the shortcode.
     */
    public function __construct() {
        add_shortcode( 'storyposts', [$this, 'render'] );
    }

    /**
     * Render the shortcode.
     *
     * @param array $atts Shortcode attributes.
     * @return string HTML output.
     */
    public function render( $atts ) {
        $atts = shortcode_atts( [
            'context'         => 'collections',
            'type'            => '',
            'limit'           => '',
            'source'          => '',
            'product_filter'  => '',
            'layout'          => '',
            'slides_per_view' => 0,
            'slide_gap'       => -1,
            'user_id'         => 0,
            'cat'             => '',
        ], $atts, 'storyposts' );
        // shortcode_atts can only take one base name for filtering, but we keep storyposts as primary.
        // Prioritize context, fallback to type
        $type = ( !empty( $atts['type'] ) ? $atts['type'] : $atts['context'] );
        $type = sanitize_key( $type );
        $limit = absint( $atts['limit'] );
        $source = sanitize_key( $atts['source'] );
        $product_filter = sanitize_key( $atts['product_filter'] );
        $layout = sanitize_key( $atts['layout'] );
        $slides_per_view = absint( $atts['slides_per_view'] );
        $slide_gap = (int) $atts['slide_gap'];
        // Normalize type (singular/plural and mapping)
        $type_map = [
            'collection'  => 'collections',
            'collections' => 'collections',
        ];
        if ( storyposts_component_exists( 'user-stories' ) ) {
            $type_map['user'] = 'users';
            $type_map['users'] = 'users';
        }
        if ( storyposts_component_exists( 'blog-stories' ) ) {
            $type_map['blog'] = 'blog';
        }
        if ( storyposts_component_exists( 'product-stories' ) ) {
            $type_map['product'] = 'products';
            $type_map['products'] = 'products';
        }
        if ( isset( $type_map[$type] ) ) {
            $type = $type_map[$type];
        } else {
            $type = 'collections';
        }
        // If no limit provided, use settings based on type
        if ( $limit <= 0 ) {
            $limit = absint( Storyposts_Settings::get( 'collections_per_load', 12 ) );
            if ( $type === 'users' && storyposts_component_exists( 'user-stories' ) ) {
                $limit = absint( Storyposts_Settings::get( 'stories_per_load', 12 ) );
            }
            if ( $type === 'blog' && storyposts_component_exists( 'blog-stories' ) ) {
                $limit = absint( Storyposts_Settings::get( 'blog_sources_per_load', 12 ) );
            }
            if ( $type === 'products' && storyposts_component_exists( 'product-stories' ) ) {
                $limit = absint( Storyposts_Settings::get( 'product_sources_per_load', 12 ) );
            }
        }
        // Render — defaults to collections; Pro block only enters if Pro component is active
        $html = $this->render_collection_stories(
            $limit,
            $layout,
            $slides_per_view,
            $slide_gap
        );
        if ( $type === 'users' && storyposts_component_exists( 'user-stories' ) ) {
            $html = $this->render_user_stories__premium_only(
                $limit,
                $layout,
                $slides_per_view,
                $slide_gap
            );
        }
        if ( $type === 'blog' && storyposts_component_exists( 'blog-stories' ) ) {
            $html = $this->render_blog_stories__premium_only(
                $limit,
                $source,
                $layout,
                $slides_per_view,
                $slide_gap
            );
        }
        if ( $type === 'products' && storyposts_component_exists( 'product-stories' ) ) {
            $html = $this->render_product_stories__premium_only(
                $limit,
                $product_filter,
                $layout,
                $slides_per_view,
                $slide_gap
            );
        }
        // Ensure modals are rendered
        if ( function_exists( 'storyposts_render_modals' ) ) {
            ob_start();
            storyposts_render_modals( $type );
            $html .= ob_get_clean();
        }
        return $html;
    }

    /**
     * Render collection stories slider.
     *
     * @param int $limit Number of collections to load.
     * @return string HTML output.
     */
    private function render_collection_stories(
        $limit,
        $layout = '',
        $slides_per_view = 0,
        $slide_gap = -1
    ) {
        $mode = 'collections';
        $ajax_action = 'storyposts_get_collection_stories';
        if ( empty( $layout ) ) {
            $layout = Storyposts_Settings::get( 'collection_stories_layout', 'card' );
        }
        if ( $slides_per_view <= 0 ) {
            $slides_per_view = absint( Storyposts_Settings::get( "collection_slides_per_view_{$layout}", 5 ) );
        }
        if ( $slide_gap === -1 ) {
            $slide_gap = (int) Storyposts_Settings::get( "collection_slide_gap_{$layout}", 12 );
        }
        ob_start();
        $template = ( $layout === 'avatar' ? 'story-slider-avatar' : 'story-slider' );
        storyposts_get_template_part( $template, compact(
            'mode',
            'ajax_action',
            'limit',
            'layout',
            'slides_per_view',
            'slide_gap'
        ) );
        return ob_get_clean();
    }

}
