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

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

$label        = sanitize_text_field( (string) ( $attributes['label']        ?? 'Book a Field' ) );
$url          = sanitize_url( (string) ( $attributes['url'] ?? '' ) ) ?: (string) get_permalink();
$size         = sanitize_key( (string) ( $attributes['size']         ?? 'large'   ) );
$style        = sanitize_key( (string) ( $attributes['style']        ?? 'primary'  ) );
$align        = sanitize_key( (string) ( $attributes['align']        ?? 'center'   ) );
$open_modal   = (bool) ( $attributes['openModal']   ?? false );
$full_width   = (bool) ( $attributes['fullWidth']   ?? false );
$field_id     = sanitize_text_field( (string) ( $attributes['fieldId'] ?? '' ) );
$bg_color     = sanitize_hex_color( (string) ( $attributes['bgColor']    ?? '' ) );
$text_color   = sanitize_hex_color( (string) ( $attributes['textColor']  ?? '' ) );
$border_radius = absint( $attributes['borderRadius'] ?? 0 );

$size_map = [
	'small'  => [ 'padding' => '8px 20px',  'font-size' => '13px', 'border-radius' => '8px'  ],
	'medium' => [ 'padding' => '12px 28px', 'font-size' => '15px', 'border-radius' => '8px'  ],
	'large'  => [ 'padding' => '16px 36px', 'font-size' => '16px', 'border-radius' => '10px' ],
];

$style_map = [
	'primary' => [ 'background' => '#0F172A', 'color' => '#ffffff', 'border' => 'none' ],
	'accent'  => [ 'background' => '#3B82F6', 'color' => '#ffffff', 'border' => 'none' ],
	'outline' => [ 'background' => 'transparent', 'color' => '#0F172A', 'border' => '2px solid #0F172A' ],
];

$sz = $size_map[ $size ]  ?? $size_map['large'];
$st = $style_map[ $style ] ?? $style_map['primary'];

// Custom colors override the preset
if ( $bg_color )   $st['background'] = $bg_color;
if ( $text_color ) $st['color']      = $text_color;

// Border radius override
$sz['border-radius'] = ( $border_radius > 0 ? $border_radius : (int) filter_var( $sz['border-radius'], FILTER_SANITIZE_NUMBER_INT ) ) . 'px';

$display = $full_width ? 'block;width:100%;text-align:center;box-sizing:border-box;' : 'inline-block;';

$btn_css = 'display:' . $display
         . 'padding:' . esc_attr( $sz['padding'] ) . ';'
         . 'font-size:' . esc_attr( $sz['font-size'] ) . ';'
         . 'border-radius:' . esc_attr( $sz['border-radius'] ) . ';'
         . 'background:' . esc_attr( $st['background'] ) . ';'
         . 'color:' . esc_attr( $st['color'] ) . ';'
         . 'border:' . esc_attr( $st['border'] ) . ';'
         . 'font-weight:700;text-decoration:none;cursor:pointer;transition:opacity .15s;font-family:inherit;';
?>
<div style="text-align:<?php echo esc_attr( $align ); ?>;padding:8px 0;">
<?php if ( $open_modal ) :
	$config     = playard_get_frontend_config();
	// Pre-select a specific field when fieldId is set
	if ( $field_id ) {
		$config['fields'] = $field_id;
	}
	$config_json = wp_json_encode( $config );
?>
  <button
    type="button"
    data-playard-modal-trigger="1"
    data-config="<?php echo esc_attr( $config_json ?: '{}' ); ?>"
    style="<?php echo esc_attr( $btn_css ); ?>"
    onmouseover="this.style.opacity='.85'"
    onmouseout="this.style.opacity='1'"
  ><?php echo esc_html( $label ); ?></button>
<?php else : ?>
  <a
    href="<?php echo esc_url( $url ); ?>"
    style="<?php echo esc_attr( $btn_css ); ?>"
    onmouseover="this.style.opacity='.85'"
    onmouseout="this.style.opacity='1'"
  ><?php echo esc_html( $label ); ?></a>
<?php endif; ?>
</div>
