<?php
/**
 * Server-side render for playard/facility-info block.
 *
 * @var array<string,mixed> $attributes
 */
if ( ! defined( 'ABSPATH' ) ) exit;

// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

global $wpdb;

$show_address = (bool) ( $attributes['showAddress'] ?? true  );
$show_phone   = (bool) ( $attributes['showPhone']   ?? true  );
$show_email   = (bool) ( $attributes['showEmail']   ?? false );
$show_hours   = (bool) ( $attributes['showHours']   ?? true  );
$layout       = sanitize_key( (string) ( $attributes['layout'] ?? 'horizontal' ) );

// Color overrides (fall back to design-system defaults)
$bg_color     = sanitize_hex_color( (string) ( $attributes['bgColor']     ?? '' ) ) ?: '#FFFFFF';
$border_color = sanitize_hex_color( (string) ( $attributes['borderColor'] ?? '' ) ) ?: '#E2E8F0';
$icon_bg      = sanitize_hex_color( (string) ( $attributes['iconBg']      ?? '' ) ) ?: '#EFF6FF';
$icon_color   = sanitize_hex_color( (string) ( $attributes['iconColor']   ?? '' ) ) ?: '#3B82F6';
$title_color  = sanitize_hex_color( (string) ( $attributes['titleColor']  ?? '' ) ) ?: '#0F172A';
$text_color   = sanitize_hex_color( (string) ( $attributes['textColor']   ?? '' ) ) ?: '#0F172A';
$label_color  = sanitize_hex_color( (string) ( $attributes['labelColor']  ?? '' ) ) ?: '#94A3B8';

$facility = $wpdb->get_row(
	"SELECT name, address, phone, email, open_time, close_time, open_days
	 FROM {$wpdb->prefix}playard_facilities
	 WHERE status = 'active'
	 ORDER BY id ASC
	 LIMIT 1"
);

if ( ! $facility ) {
	echo '<p style="color:#94A3B8;font-size:14px;">' . esc_html__( 'Facility not configured yet.', 'playard' ) . '</p>';
	return;
}

$open_days = ! empty( $facility->open_days )
	? json_decode( (string) $facility->open_days, true )
	: [];

$day_names  = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ];
$days_label = '';
if ( is_array( $open_days ) && ! empty( $open_days ) ) {
	$named = array_map( function ( $d ) use ( $day_names ) {
		return isset( $day_names[ $d ] ) ? substr( $day_names[ $d ], 0, 3 ) : $d;
	}, $open_days );
	$days_label = implode( ', ', $named );
}

$hours_label = '';
if ( $facility->open_time && $facility->close_time ) {
	$fmt = function ( $t ) {
		$parts = explode( ':', $t );
		$h     = (int) $parts[0];
		$m     = $parts[1] ?? '00';
		$suf   = $h >= 12 ? 'PM' : 'AM';
		$h12   = $h % 12 ?: 12;
		return $h12 . ':' . $m . ' ' . $suf;
	};
	$hours_label = $fmt( $facility->open_time ) . ' – ' . $fmt( $facility->close_time );
	if ( $days_label ) $hours_label .= ' (' . $days_label . ')';
}

$uid   = 'pfi-' . wp_rand( 1000, 9999 );
$horiz = $layout === 'horizontal';

// Build scoped CSS in a buffer, then emit via wp_add_inline_style() —
// WordPress.org forbids raw inline <style> tags in markup.
ob_start();
?>
.<?php echo esc_attr( $uid ); ?>{font-family:inherit;background:<?php echo esc_attr( $bg_color ); ?>;border:1px solid <?php echo esc_attr( $border_color ); ?>;border-radius:12px;padding:20px 24px;}
.<?php echo esc_attr( $uid ); ?> .py-fi-wrap{display:flex;flex-wrap:wrap;gap:<?php echo $horiz ? '32px' : '12px'; ?>;<?php echo $horiz ? 'align-items:flex-start;' : 'flex-direction:column;'; ?>}
.<?php echo esc_attr( $uid ); ?> .py-fi-item{display:flex;align-items:flex-start;gap:10px;}
.<?php echo esc_attr( $uid ); ?> .py-fi-icon{flex-shrink:0;width:36px;height:36px;border-radius:8px;background:<?php echo esc_attr( $icon_bg ); ?>;display:flex;align-items:center;justify-content:center;}
.<?php echo esc_attr( $uid ); ?> .py-fi-icon svg{width:18px;height:18px;stroke:<?php echo esc_attr( $icon_color ); ?>;}
.<?php echo esc_attr( $uid ); ?> .py-fi-label{font-size:11px;font-weight:600;text-transform:uppercase;letter-spacing:.04em;color:<?php echo esc_attr( $label_color ); ?>;margin:0 0 2px;}
.<?php echo esc_attr( $uid ); ?> .py-fi-value{font-size:14px;font-weight:500;color:<?php echo esc_attr( $text_color ); ?>;margin:0;}
<?php
$playard_css    = ob_get_clean();
$playard_handle = 'playard-facility-info-block';
wp_register_style( $playard_handle, false, [], PLAYARD_VERSION );
wp_enqueue_style( $playard_handle );
wp_add_inline_style( $playard_handle, $playard_css );
?>
<div class="<?php echo esc_attr( $uid ); ?>">
  <p style="font-size:20px;font-weight:700;color:<?php echo esc_attr( $title_color ); ?>;margin:0 0 14px;"><?php echo esc_html( $facility->name ); ?></p>
  <div class="py-fi-wrap">

<?php if ( $show_address && $facility->address ) : ?>
    <div class="py-fi-item">
      <div class="py-fi-icon">
        <svg fill="none" viewBox="0 0 24 24" stroke-width="2">
          <path stroke-linecap="round" stroke-linejoin="round" d="M15 10.5a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z"/>
          <path stroke-linecap="round" stroke-linejoin="round" d="M19.5 10.5c0 7.142-7.5 11.25-7.5 11.25S4.5 17.642 4.5 10.5a7.5 7.5 0 1 1 15 0Z"/>
        </svg>
      </div>
      <div>
        <p class="py-fi-label"><?php esc_html_e( 'Address', 'playard' ); ?></p>
        <p class="py-fi-value"><?php echo esc_html( $facility->address ); ?></p>
      </div>
    </div>
<?php endif; ?>

<?php if ( $show_phone && $facility->phone ) : ?>
    <div class="py-fi-item">
      <div class="py-fi-icon">
        <svg fill="none" viewBox="0 0 24 24" stroke-width="2">
          <path stroke-linecap="round" stroke-linejoin="round" d="M2.25 6.75c0 8.284 6.716 15 15 15h2.25a2.25 2.25 0 0 0 2.25-2.25v-1.372c0-.516-.351-.966-.852-1.091l-4.423-1.106c-.44-.11-.902.055-1.173.417l-.97 1.293c-.282.376-.769.542-1.21.38a12.035 12.035 0 0 1-7.143-7.143c-.162-.441.004-.928.38-1.21l1.293-.97c.363-.271.527-.734.417-1.173L6.963 3.102a1.125 1.125 0 0 0-1.091-.852H4.5A2.25 2.25 0 0 0 2.25 4.5v2.25Z"/>
        </svg>
      </div>
      <div>
        <p class="py-fi-label"><?php esc_html_e( 'Phone', 'playard' ); ?></p>
        <p class="py-fi-value"><a href="tel:<?php echo esc_attr( $facility->phone ); ?>" style="color:#0F172A;text-decoration:none;"><?php echo esc_html( $facility->phone ); ?></a></p>
      </div>
    </div>
<?php endif; ?>

<?php if ( $show_email && $facility->email ) : ?>
    <div class="py-fi-item">
      <div class="py-fi-icon">
        <svg fill="none" viewBox="0 0 24 24" stroke-width="2">
          <path stroke-linecap="round" stroke-linejoin="round" d="M21.75 6.75v10.5a2.25 2.25 0 0 1-2.25 2.25h-15a2.25 2.25 0 0 1-2.25-2.25V6.75m19.5 0A2.25 2.25 0 0 0 19.5 4.5h-15a2.25 2.25 0 0 0-2.25 2.25m19.5 0v.243a2.25 2.25 0 0 1-1.07 1.916l-7.5 4.615a2.25 2.25 0 0 1-2.36 0L3.32 8.91a2.25 2.25 0 0 1-1.07-1.916V6.75"/>
        </svg>
      </div>
      <div>
        <p class="py-fi-label"><?php esc_html_e( 'Email', 'playard' ); ?></p>
        <p class="py-fi-value"><a href="mailto:<?php echo esc_attr( $facility->email ); ?>" style="color:#0F172A;text-decoration:none;"><?php echo esc_html( $facility->email ); ?></a></p>
      </div>
    </div>
<?php endif; ?>

<?php if ( $show_hours && $hours_label ) : ?>
    <div class="py-fi-item">
      <div class="py-fi-icon">
        <svg fill="none" viewBox="0 0 24 24" stroke-width="2">
          <path stroke-linecap="round" stroke-linejoin="round" d="M12 6v6h4.5m4.5 0a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/>
        </svg>
      </div>
      <div>
        <p class="py-fi-label"><?php esc_html_e( 'Opening Hours', 'playard' ); ?></p>
        <p class="py-fi-value"><?php echo esc_html( $hours_label ); ?></p>
      </div>
    </div>
<?php endif; ?>

  </div>
</div>
