<?php

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

class AIEO_Business_Info_Block {

    private static $registered = false;

    public static function register() {
        if (self::$registered) { return; }
        self::$registered = true;
        add_action('init', array(__CLASS__, 'register_block'));
        add_shortcode('aieo_business_info', array(__CLASS__, 'shortcode'));
    }

    public static function register_block() {
        register_block_type('aieo/business-info', array(
            'api_version'     => 3,
            'title'           => __('AIEO Business Info', 'ai-eshop-optimizer'),
            'description'     => __("A store location's address, phone, hours and Google links — from your Locations & Reviews data.", 'ai-eshop-optimizer'),
            'category'        => 'widgets',
            'icon'            => 'store',
            'keywords'        => array('store', 'address', 'phone', 'hours', 'contact', 'info'),
            'supports'        => array('align' => array('wide')),
            'attributes'      => array(
                'locationSlug' => array('type' => 'string',  'default' => ''),
                'showHours'    => array('type' => 'boolean', 'default' => true),
                'showMaps'     => array('type' => 'boolean', 'default' => true),
                'showRateUs'   => array('type' => 'boolean', 'default' => true),
                'stacked'      => array('type' => 'boolean', 'default' => false),
                'heading'      => array('type' => 'string',  'default' => ''),
            ),
            'uses_context'    => array('postId'),
            'render_callback' => array(__CLASS__, 'render'),
        ));
    }

    public static function shortcode($atts) {
        $atts = shortcode_atts(array(
            'location' => '', 'show_hours' => 1, 'show_maps' => 1, 'show_rate_us' => 1, 'stacked' => 0, 'heading' => '',
        ), $atts, 'aieo_business_info');
        return self::render(array(
            'locationSlug' => (string) $atts['location'],
            'showHours'    => (bool) (int) $atts['show_hours'],
            'showMaps'     => (bool) (int) $atts['show_maps'],
            'showRateUs'   => (bool) (int) $atts['show_rate_us'],
            'stacked'      => (bool) (int) $atts['stacked'],
            'heading'      => (string) $atts['heading'],
        ));
    }

    public static function render($attrs, $content = '', $block = null) {
        if (!class_exists('AIEO_Google_Reviews_Helpers')) { return ''; }
        $attrs = wp_parse_args(is_array($attrs) ? $attrs : array(), array(
            'locationSlug' => '', 'showHours' => true, 'showMaps' => true, 'showRateUs' => true, 'stacked' => false, 'heading' => '',
        ));
        $loc = AIEO_Google_Reviews_Helpers::resolve_single($attrs['locationSlug'], $block);
        if (!$loc) { return ''; }

        $nap     = AIEO_Google_Reviews_Helpers::render_nap_html($loc, !empty($attrs['stacked']));
        $hours   = !empty($attrs['showHours']) ? AIEO_Google_Reviews_Helpers::render_hours_html($loc) : '';
        $actions = AIEO_Google_Reviews_Helpers::render_action_links_html($loc, !empty($attrs['showMaps']), !empty($attrs['showRateUs']));
        if ('' === $nap && '' === $hours && '' === $actions) { return ''; }

        AIEO_Google_Reviews_Helpers::enqueue_locations_assets();
        $wrap = function_exists('get_block_wrapper_attributes')
            ? get_block_wrapper_attributes(array('class' => 'aieo-business-info'))
            : 'class="aieo-business-info"';
        $out = '<div ' . $wrap . '>';
        if ('' !== (string) $attrs['heading']) { $out .= '<h3>' . esc_html($attrs['heading']) . '</h3>'; }
        $out .= $nap . $hours . $actions . '</div>';
        return $out;
    }
}
