<?php
/**
 * Template Renderer for BoostBot.
 *
 * Renders PHP template files with provided data.
 *
 * @package BoostBot
 * @since 1.1.0
 */

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Class BoostBot_Template_Renderer
 *
 * Handles the actual rendering of template files.
 *
 * @since 1.1.0
 */
class BoostBot_Template_Renderer {

    /**
     * Templates directory path.
     *
     * @var string
     */
    private $templates_dir;

    /**
     * Constructor.
     */
    public function __construct() {
        $this->templates_dir = BOOSTBOT_PLUGIN_DIR . 'includes/templates/views/';
    }

    /**
     * Render a template with data.
     *
     * @param string $template_id The template identifier (e.g., 'contact-card').
     * @param array  $data        The data to pass to the template.
     * @return string Rendered HTML.
     */
    public function render( string $template_id, array $data ): string {
        $template_file = $this->templates_dir . $template_id . '.php';

        if ( ! file_exists( $template_file ) ) {
            BoostBot_Logger::warning( 'Template file not found', [
                'template_id' => $template_id,
                'file'        => $template_file,
            ]);
            return $this->render_fallback( $template_id, $data );
        }

        // Start output buffering
        ob_start();

        // Extract data variables for use in template
        // EXTR_SKIP prevents overwriting existing variables
        extract( $data, EXTR_SKIP );

        // Include the template file
        include $template_file;

        // Get the buffered content and clean the buffer
        $output = ob_get_clean();

        return $output;
    }

    /**
     * Render a fallback for missing templates.
     *
     * During development, this shows the data structure.
     * In production, it could show a simple formatted version.
     *
     * @param string $template_id The template identifier.
     * @param array  $data        The data that was passed.
     * @return string Fallback HTML.
     */
    public function render_fallback( string $template_id, array $data ): string {
        // In production/dev mode, show a simple formatted version
        $output = '<div class="bb-template bb-template-fallback" data-template="' . esc_attr( $template_id ) . '">';
        
        // Try to display something useful based on common fields
        if ( ! empty( $data['name'] ) ) {
            $output .= '<strong>' . esc_html( $data['name'] ) . '</strong>';
        }
        if ( ! empty( $data['title'] ) ) {
            $output .= '<strong>' . esc_html( $data['title'] ) . '</strong>';
        }
        if ( ! empty( $data['description'] ) ) {
            $output .= '<p>' . esc_html( $data['description'] ) . '</p>';
        }
        if ( ! empty( $data['price'] ) ) {
            $output .= '<span>₪' . esc_html( number_format( $data['price'], 0 ) ) . '</span>';
        }
        
        // If nothing useful found, show template name
        if ( $output === '<div class="bb-template bb-template-fallback" data-template="' . esc_attr( $template_id ) . '">' ) {
            $output .= '<em>Template: ' . esc_html( $template_id ) . '</em>';
        }
        
        $output .= '</div>';

        return $output;
    }

    /**
     * Check if a template file exists.
     *
     * @param string $template_id The template identifier.
     * @return bool
     */
    public function template_file_exists( string $template_id ): bool {
        $template_file = $this->templates_dir . $template_id . '.php';
        return file_exists( $template_file );
    }

    /**
     * Get the path to a template file.
     *
     * @param string $template_id The template identifier.
     * @return string Full path to the template file.
     */
    public function get_template_path( string $template_id ): string {
        return $this->templates_dir . $template_id . '.php';
    }
}
