<?php
namespace EssentialBlocks\Blocks;

use EssentialBlocks\Utils\Helper;
use EssentialBlocks\Core\Block;

class PostMeta extends Block
{
    protected $frontend_styles = [ 'essential-blocks-fontawesome' ];

    /**
     * Default attributes
     *
     * @var array
     */
    protected $default_attributes;

    public function __construct()
    {
        $this->default_attributes = [
            'metaDisplay'     => 'inline',
            'showAuthor'      => true,
            'authorLabel'     => __( 'Author: ', 'essential-blocks' ),
            'showDate'        => true,
            'dateLabel'       => __( "Published Date: ", 'essential-blocks' ),
            'showProductSku'  => true,
            'productSkuLabel' => __( "SKU: ", 'essential-blocks' ),
            'type'            => 'post',
            'enableContents'  => [ "author", "date", "product_sku" ],
            'showMetaIcon'    => true,
            'authorIcon'      => 'far fa-circle-user',
            'dateIcon'        => 'far fa-calendar-days',
            'skuIcon'         => 'fas fa-barcode',
            'showAuthorPicture' => false,
            'authorPictureLink' => true,
            'authorPictureBorderRadius' => 50
         ];
    }

    /**
     * Unique name of the block.
     * @return string
     */
    public function get_name()
    {
        return 'post-meta';
    }

    /**
     * Check if we're in a Loop Builder context
     *
     * @param array $context
     * @return bool
     */
    private function is_in_loop_builder_context( $context )
    {
        return isset( $context[ 'essential-blocks/isLoopBuilder' ] ) &&
            $context[ 'essential-blocks/isLoopBuilder' ] === true;
    }

    /**
     * Get post data based on context (Loop Builder or normal)
     *
     * @param array $context
     * @param bool $is_in_loop_builder
     * @return array
     */
    private function get_post_data( $context, $is_in_loop_builder )
    {
        if ( $is_in_loop_builder ) {
            // In Loop Builder context - use context post data
            $post_id = isset( $context[ 'essential-blocks/postId' ] ) ?
            $context[ 'essential-blocks/postId' ] :
            ( isset( $context[ 'postId' ] ) ? $context[ 'postId' ] : get_the_ID() );

            $post_type = isset( $context[ 'essential-blocks/postType' ] ) ?
            $context[ 'essential-blocks/postType' ] :
            ( isset( $context[ 'postType' ] ) ? $context[ 'postType' ] : get_post_type() );

            // Ensure we have a valid post ID before trying to get the post
            $current_post = ( $post_id && $post_id > 0 ) ? get_post( $post_id ) : null;
        } else {
            // Normal context - use current queried object
            $current_post = get_queried_object();

            // Fallback to global $post if queried object is not a post
            if ( ! $current_post || ! isset( $current_post->ID ) ) {
                global $post;
                $current_post = $post;
            }

            $post_id      = $current_post ? $current_post->ID : get_the_ID();
            $post_type    = $current_post ? $current_post->post_type : get_post_type();
        }

        return [
            'post'      => $current_post,
            'post_id'   => $post_id,
            'post_type' => $post_type
         ];
    }

    /**
     * Block render callback.
     *
     * Handles both normal post meta display and Loop Builder context.
     * When in Loop Builder context, uses the post data from the loop iteration
     * instead of the current queried object.
     *
     * @param mixed $attributes Block attributes
     * @param mixed $content Block content (unused but required by WP)
     * @param mixed $block Block object containing context
     * @return mixed Rendered HTML
     */
    public function render_callback( $attributes, $content, $block = null )
    {
        if ( is_admin() ) {
            return;
        }

        $attributes = wp_parse_args( $attributes, $this->default_attributes );

        $className = isset( $attributes[ "className" ] ) ? $attributes[ "className" ] : "";
        $classHook = isset( $attributes[ 'classHook' ] ) ? $attributes[ 'classHook' ] : '';

        // Check if we're in a Loop Builder context
        $context            = isset( $block->context ) ? $block->context : [  ];
        $is_in_loop_builder = $this->is_in_loop_builder_context( $context );

        // Get the appropriate post data based on context
        $post_data = $this->get_post_data( $context, $is_in_loop_builder );

        // Extract post data
        $current_post = $post_data[ 'post' ];
        $post_id      = $post_data[ 'post_id' ];
        $post_type    = $post_data[ 'post_type' ];

        // Check if we have a valid post object
        if ( ! $current_post || ! isset( $current_post->post_author ) ) {
            // Return empty data if no valid post is available
            return '';
        }

        // Get author name
        $author_name = get_the_author_meta( 'display_name', $current_post->post_author );

        // Get author avatar URL
        $author_avatar_url = get_avatar_url( $current_post->post_author, array( 'size' => 96 ) );

        // Get author URL
        $author_url = get_author_posts_url( $current_post->post_author );

        // Get published date with WordPress date format
        $date_format  = get_option( 'date_format' );
        $publish_date = isset( $current_post->post_date ) ? date_i18n( $date_format, strtotime( $current_post->post_date ) ) : '';

        // Get product SKU if applicable
        $product_sku = '';
        if ( $post_type === 'product' && function_exists( 'wc_get_product' ) ) {
            $product = wc_get_product( $post_id );
            if ( $product ) {
                $product_sku = $product->get_sku();
            }
        }

        $author_icon = sprintf(
            '%1$s',
            Helper::eb_render_icon( Helper::eb_get_icon_type( $attributes[ 'authorIcon' ] ), 'eb-post-metadata-icon', $attributes[ 'authorIcon' ] )
        );
        $date_icon = sprintf(
            '%1$s',
            Helper::eb_render_icon( Helper::eb_get_icon_type( $attributes[ 'dateIcon' ] ), 'eb-post-metadata-icon', $attributes[ 'dateIcon' ] )
        );
        $sku_icon = sprintf(
            '%1$s',
            Helper::eb_render_icon( Helper::eb_get_icon_type( $attributes[ 'skuIcon' ] ), 'eb-post-metadata-icon', $attributes[ 'skuIcon' ] )
        );

        // Decode unified meta items (Select2-driven canonical list).
        $parsed_meta_items = null;
        if ( ! empty( $attributes[ 'metaItems' ] ) && \is_string( $attributes[ 'metaItems' ] ) ) {
            $decoded = json_decode( $attributes[ 'metaItems' ], true );
            if ( \is_array( $decoded ) ) {
                $parsed_meta_items = $decoded;
            }
        }

        // Decode legacy custom / ACF fields (used as fallback when metaItems is empty).
        $parsed_custom_fields = [];
        if ( ! empty( $attributes[ 'customFields' ] ) && \is_string( $attributes[ 'customFields' ] ) ) {
            $decoded = json_decode( $attributes[ 'customFields' ], true );
            if ( \is_array( $decoded ) ) {
                $parsed_custom_fields = $decoded;
            }
        }

        // Build the effective ordered item list. Once metaItems has been touched
        // (even cleared to []), treat it as canonical so the user can intentionally
        // show zero items. Only fall through to legacy when never set (null).
        $effective_items = [];
        if ( $parsed_meta_items !== null ) {
            $effective_items = $parsed_meta_items;
        } else {
            $enable_contents = isset( $attributes[ 'enableContents' ] ) && \is_array( $attributes[ 'enableContents' ] )
                ? $attributes[ 'enableContents' ]
                : [];
            foreach ( $enable_contents as $key ) {
                if ( $key === 'author' && ! empty( $attributes[ 'showAuthor' ] ) ) {
                    $effective_items[] = [ 'value' => 'author', 'label' => '' ];
                } elseif ( $key === 'date' && ! empty( $attributes[ 'showDate' ] ) ) {
                    $effective_items[] = [ 'value' => 'date', 'label' => '' ];
                } elseif (
                    $key === 'product_sku'
                    && ! empty( $attributes[ 'showProductSku' ] )
                    && $post_type === 'product'
                ) {
                    $effective_items[] = [ 'value' => 'product_sku', 'label' => '' ];
                }
            }
            foreach ( $parsed_custom_fields as $field ) {
                $effective_items[] = $field;
            }
        }

        // Run any non-builtin keys through the meta markup filter so the Pro
        // plugin's ACF integration (AcfData::eb_acf_dynamic_posts_meta) can
        // generate HTML for ACF / dynamic tag values.
        $builtin_keys = [ 'author', 'date', 'product_sku' ];
        $custom_meta_html = [];
        $custom_meta_keys = [];
        $custom_icon_html = [];
        foreach ( $effective_items as $item ) {
            if ( isset( $item[ 'value' ] ) && ! \in_array( $item[ 'value' ], $builtin_keys, true ) ) {
                $custom_meta_keys[] = $item[ 'value' ];
                if ( ! empty( $item[ 'icon' ] ) && \is_string( $item[ 'icon' ] ) ) {
                    $custom_icon_html[ $item[ 'value' ] ] = Helper::eb_render_icon(
                        Helper::eb_get_icon_type( $item[ 'icon' ] ),
                        'eb-post-metadata-icon',
                        $item[ 'icon' ]
                    );
                }
            }
        }
        if ( ! empty( $custom_meta_keys ) ) {
            $custom_meta_html = apply_filters(
                'eb_post_grid_meta_markup',
                [],
                $post_id,
                $custom_meta_keys
            );
        }

        $data = [
            'author'             => $author_name,
            'author_avatar_url'  => $author_avatar_url,
            'author_url'         => $author_url,
            'date'               => $publish_date,
            'product_sku'        => $product_sku,
            'show_meta_icon'     => $attributes[ 'showMetaIcon' ],
            'author_icon'        => $author_icon,
            'date_icon'          => $date_icon,
            'sku_icon'           => $sku_icon,
            'is_in_loop_builder' => $is_in_loop_builder,
            'post_id'            => $post_id,
            'post_type'          => $post_type,
            'effective_items'    => $effective_items,
            'custom_meta_html'   => $custom_meta_html,
            'custom_icon_html'   => $custom_icon_html
         ];

        ob_start();
        Helper::views( 'post-meta', array_merge( $attributes, [
            'className'    => $className,
            'classHook'    => $classHook,
            'meta_data'    => $data,
            'block_object' => $this
         ] ) );

        return ob_get_clean();
    }
}
