<?php
/**
 * Property Agent Card Template Part
 *
 * @package HomeProz
 */

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

$agent = isset($args['agent']) ? $args['agent'] : null;
$property_id = isset($args['property_id']) ? $args['property_id'] : get_the_ID();

// Get company contact as fallback
$phone = homeproz_get_option('phone');
$email = homeproz_get_option('email');

// If we have an agent user
if ($agent && is_array($agent)) {
    $agent_name = $agent['display_name'];
    $agent_email = $agent['user_email'];
    // Try to get agent phone from user meta
    $agent_phone = get_user_meta($agent['ID'], 'phone', true) ?: $phone;
} else {
    $agent_name = 'HomeProz Real Estate';
    $agent_email = $email;
    $agent_phone = $phone;
}
?>

<div class="agent-card card">
    <div class="agent-card-header">
        <h3 class="agent-card-title">Contact Agent</h3>
    </div>

    <div class="agent-card-content">
        <div class="agent-info">
            <div class="agent-avatar">
                <?php if ($agent && is_array($agent)) : ?>
                    <?php echo get_avatar($agent['ID'], 80); ?>
                <?php else : ?>
                    <div class="agent-avatar-placeholder">
                        <svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">
                            <path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/>
                            <circle cx="12" cy="7" r="4"/>
                        </svg>
                    </div>
                <?php endif; ?>
            </div>
            <div class="agent-details">
                <p class="agent-name"><?php echo esc_html($agent_name); ?></p>
                <p class="agent-role">Listing Agent</p>
            </div>
        </div>

        <div class="agent-contact">
            <?php if ($agent_phone) : ?>
                <a href="tel:<?php echo esc_attr(preg_replace('/[^0-9]/', '', $agent_phone)); ?>" class="btn btn-primary agent-phone-btn">
                    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">
                        <path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7A2 2 0 0 1 22 16.92z"/>
                    </svg>
                    <?php echo esc_html($agent_phone); ?>
                </a>
            <?php endif; ?>

            <?php if ($agent_email) : ?>
                <a href="mailto:<?php echo esc_attr($agent_email); ?>?subject=<?php echo esc_attr('Inquiry about ' . get_the_title($property_id)); ?>" class="btn btn-secondary agent-email-btn">
                    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">
                        <path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/>
                        <polyline points="22,6 12,13 2,6"/>
                    </svg>
                    Send Email
                </a>
            <?php endif; ?>
        </div>
    </div>

    <!-- Quick Inquiry Form (optional - can be expanded) -->
    <div class="agent-card-footer">
        <p class="agent-card-note">Interested in this property? Contact us today for a showing.</p>
    </div>
</div>
