<?php
/**
 * Template View: Contact Card
 *
 * Displays contact information for a person or representative.
 *
 * @package    BoostBot
 * @subpackage Templates/Views
 * @since      1.6.0
 *
 * @var string $name     Contact name.
 * @var string $role     Job title/role.
 * @var string $phone    Phone number.
 * @var string $email    Email address.
 * @var string $whatsapp WhatsApp number.
 * @var string $image    Profile image URL.
 */

// phpcs:ignoreFile WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template-scope variables set by the includer, not true globals.

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

// Generate initials for avatar fallback
$initials = '';
if ( ! empty( $name ) ) {
    $words = explode( ' ', $name );
    foreach ( $words as $word ) {
        if ( ! empty( $word ) ) {
            $initials .= mb_substr( $word, 0, 1 );
            if ( strlen( $initials ) >= 2 ) {
                break;
            }
        }
    }
}

// Format WhatsApp link
$whatsapp_link = '';
if ( ! empty( $whatsapp ) ) {
    $clean_number  = preg_replace( '/[^0-9]/', '', $whatsapp );
    $whatsapp_link = 'https://wa.me/' . $clean_number;
}
?>

<div class="bb-contact-card">
    <div class="bb-contact-header">
        <div class="bb-contact-avatar">
            <?php if ( ! empty( $image ) ) : ?>
                <img src="<?php echo esc_url( $image ); ?>" alt="<?php echo esc_attr( $name ); ?>" />
            <?php else : ?>
                <span class="bb-contact-initials"><?php echo esc_html( $initials ); ?></span>
            <?php endif; ?>
        </div>
        <div class="bb-contact-info">
            <h4 class="bb-contact-name"><?php echo esc_html( $name ); ?></h4>
            <?php if ( ! empty( $role ) ) : ?>
                <span class="bb-contact-role"><?php echo esc_html( $role ); ?></span>
            <?php endif; ?>
        </div>
    </div>

    <div class="bb-contact-details">
        <?php if ( ! empty( $phone ) ) : ?>
            <a href="tel:<?php echo esc_attr( $phone ); ?>" class="bb-contact-detail bb-contact-phone">
                <span class="bb-contact-icon">📞</span>
                <span class="bb-contact-value"><?php echo esc_html( $phone ); ?></span>
            </a>
        <?php endif; ?>

        <?php if ( ! empty( $email ) ) : ?>
            <a href="mailto:<?php echo esc_attr( $email ); ?>" class="bb-contact-detail bb-contact-email">
                <span class="bb-contact-icon">✉️</span>
                <span class="bb-contact-value"><?php echo esc_html( $email ); ?></span>
            </a>
        <?php endif; ?>

        <?php if ( ! empty( $whatsapp_link ) ) : ?>
            <a href="<?php echo esc_url( $whatsapp_link ); ?>" 
               class="bb-contact-detail bb-contact-whatsapp" 
               target="_blank" 
               rel="noopener noreferrer">
                <span class="bb-contact-icon">💬</span>
                <span class="bb-contact-value"><?php esc_html_e( 'שלח הודעה בוואטסאפ', 'boostbot' ); ?></span>
            </a>
        <?php endif; ?>
    </div>

    <?php if ( ! empty( $phone ) || ! empty( $whatsapp_link ) ) : ?>
        <div class="bb-contact-actions">
            <?php if ( ! empty( $phone ) ) : ?>
                <a href="tel:<?php echo esc_attr( $phone ); ?>" class="bb-contact-button bb-contact-button-call">
                    <span>📞</span>
                    <?php esc_html_e( 'התקשר', 'boostbot' ); ?>
                </a>
            <?php endif; ?>
            
            <?php if ( ! empty( $whatsapp_link ) ) : ?>
                <a href="<?php echo esc_url( $whatsapp_link ); ?>" 
                   class="bb-contact-button bb-contact-button-whatsapp"
                   target="_blank" 
                   rel="noopener noreferrer">
                    <span>💬</span>
                    <?php esc_html_e( 'וואטסאפ', 'boostbot' ); ?>
                </a>
            <?php endif; ?>
        </div>
    <?php endif; ?>
</div>
