<?php
/**
 * Department Card Template
 *
 * Displays a department with its contacts.
 *
 * @package BoostBot
 * @since 1.1.0
 *
 * Available variables:
 * @var string $name     Department name (required)
 * @var string $email    Department email
 * @var string $phone    Department phone
 * @var array  $contacts Array of contacts in the department
 */

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

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
?>
<div class="bb-template bb-department-card" dir="rtl">
    <div class="bb-dept-header">
        <span class="bb-dept-icon">🏢</span>
        <h4 class="bb-dept-name"><?php echo esc_html( $name ?? '' ); ?></h4>
    </div>
    
    <?php if ( ! empty( $email ) || ! empty( $phone ) ) : ?>
        <div class="bb-dept-contact">
            <?php if ( ! empty( $email ) ) : ?>
                <a href="mailto:<?php echo esc_attr( $email ); ?>" class="bb-dept-field">
                    📧 <?php echo esc_html( $email ); ?>
                </a>
            <?php endif; ?>
            
            <?php if ( ! empty( $phone ) ) : ?>
                <a href="tel:<?php echo esc_attr( preg_replace( '/[^0-9+]/', '', $phone ) ); ?>" class="bb-dept-field">
                    📱 <?php echo esc_html( $phone ); ?>
                </a>
            <?php endif; ?>
        </div>
    <?php endif; ?>
    
    <?php if ( ! empty( $contacts ) && is_array( $contacts ) ) : ?>
        <div class="bb-dept-team">
            <h5 class="bb-team-title"><?php esc_html_e( 'צוות המחלקה:', 'boostbot' ); ?></h5>
            <div class="bb-team-list">
                <?php foreach ( $contacts as $contact ) : ?>
                    <div class="bb-team-member">
                        <?php
                        // Get initials
                        $contact_name = $contact['name'] ?? '';
                        $words = explode( ' ', $contact_name );
                        $initials = '';
                        foreach ( $words as $word ) {
                            $initials .= mb_substr( $word, 0, 1 );
                            if ( strlen( $initials ) >= 2 ) break;
                        }
                        ?>
                        <div class="bb-member-avatar">
                            <?php if ( ! empty( $contact['image'] ) ) : ?>
                                <img src="<?php echo esc_url( $contact['image'] ); ?>" alt="<?php echo esc_attr( $contact_name ); ?>">
                            <?php else : ?>
                                <span><?php echo esc_html( $initials ); ?></span>
                            <?php endif; ?>
                        </div>
                        <div class="bb-member-info">
                            <span class="bb-member-name"><?php echo esc_html( $contact_name ); ?></span>
                            <?php if ( ! empty( $contact['role'] ) ) : ?>
                                <span class="bb-member-role"><?php echo esc_html( $contact['role'] ); ?></span>
                            <?php endif; ?>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    <?php endif; ?>
</div>
