<?php
/**
 * _explore_card.php — Reusable member card for the explore directory
 * Expected vars: $user (assoc array), $reason (string, match reason or empty)
 * Version: 1.0.0
 */
$card_name    = clean_out($user['org_name'] ?? 'Member');
$card_county  = clean_out($user['county'] ?? '');
$card_desig   = clean_out($user['designation'] ?? '');
$card_bio     = clean_out($user['bio'] ?? '');
$card_type    = $user['user_type'] ?? 'organization';
$card_slug    = $user['slug'] ?? '';
$card_id      = $user['id'] ?? 0;
$card_website = $user['website'] ?? '';
$card_created = $user['created_at'] ?? '';

// Profile link
$card_link = !empty($card_slug) ? '/profile/' . urlencode($card_slug) : '/profile.php?user_id=' . $card_id;

// Decode service chains for pills
$chains = [];
$sc_raw = $user['service_chains'] ?? '';
if (!empty($sc_raw)) {
    $decoded = json_decode($sc_raw, true);
    $chains  = is_array($decoded) ? array_slice($decoded, 0, 3) : [];
}

// Type indicator color
$type_color = $card_type === 'individual' ? '#7c3aed' : '#2563eb';
$type_icon  = $card_type === 'individual' ? 'fa-user' : 'fa-building';
$type_label = $card_type === 'individual' ? 'Individual' : 'Organization';

// Privacy – check if details should show
$privacy = [];
if (!empty($user['privacy_settings'])) {
    $privacy = json_decode($user['privacy_settings'], true) ?: [];
}
$show_email = ($privacy['show_email'] ?? true) ? true : false;
$show_phone = ($privacy['show_phone'] ?? true) ? true : false;
?>
<div class="member-card flex flex-col">
    <div class="p-5 flex flex-col flex-grow">
        <!-- Top row: type badge + joined date -->
        <div class="flex items-center justify-between mb-3">
            <span class="pill flex items-center gap-1" style="background:<?= $type_color ?>12;color:<?= $type_color ?>">
                <i class="fa-solid <?= $type_icon ?>" style="font-size:8px"></i>
                <?= $type_label ?>
            </span>
            <?php if ($card_created): ?>
                <span class="text-[10px] text-slate-400">Joined <?= date('M Y', strtotime($card_created)) ?></span>
            <?php endif; ?>
        </div>

        <!-- Name -->
        <h3 class="font-bold text-slate-800 text-sm leading-snug mb-1 line-clamp-2"><?= $card_name ?></h3>

        <!-- Meta: county / designation -->
        <div class="text-xs text-slate-400 mb-2 flex items-center gap-1 flex-wrap">
            <?php if ($card_county): ?>
                <span><i class="fa-solid fa-location-dot mr-0.5"></i><?= $card_county ?></span>
            <?php endif; ?>
            <?php if ($card_desig): ?>
                <span>· <?= $card_desig ?></span>
            <?php endif; ?>
        </div>

        <!-- Bio snippet -->
        <?php if ($card_bio): ?>
            <p class="text-xs text-slate-500 line-clamp-2 mb-3 leading-relaxed"><?= $card_bio ?></p>
        <?php endif; ?>

        <!-- Service chain pills -->
        <?php if (!empty($chains)): ?>
            <div class="flex flex-wrap gap-1 mb-3">
                <?php foreach ($chains as $ch): ?>
                    <span class="pill bg-teal-50 text-teal-700"><?= clean_out($ch) ?></span>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>

        <!-- Match reason badge (search mode only) -->
        <?php if (!empty($reason)): ?>
            <div class="mb-3">
                <span class="match-badge">
                    <i class="fa-solid fa-lightbulb" style="color:#f59e0b;font-size:9px"></i>
                    <?= htmlspecialchars($reason) ?>
                </span>
            </div>
        <?php endif; ?>

        <!-- Spacer to push button to bottom -->
        <div class="flex-grow"></div>

        <!-- Contact preview (logged-in only) + view link -->
        <div class="flex items-center justify-between gap-2 pt-3 border-t border-slate-100">
            <?php if ($is_logged_in): ?>
                <div class="flex items-center gap-3 text-[10px] text-slate-400 overflow-hidden">
                    <?php
                    $contact_email = $user['company_email'] ?? ($user['personal_email'] ?? ($user['email'] ?? ''));
                    $contact_phone = $user['company_phone'] ?? ($user['personal_phone'] ?? ($user['phone'] ?? ''));
                    if ($show_email && $contact_email): ?>
                        <span class="truncate max-w-[100px]" title="<?= clean_out($contact_email) ?>">
                            <i class="fa-solid fa-envelope mr-0.5"></i><?= clean_out($contact_email) ?>
                        </span>
                    <?php endif; ?>
                    <?php if ($show_phone && $contact_phone): ?>
                        <span class="truncate max-w-[80px]" title="<?= clean_out($contact_phone) ?>">
                            <i class="fa-solid fa-phone mr-0.5"></i><?= clean_out($contact_phone) ?>
                        </span>
                    <?php endif; ?>
                </div>
            <?php else: ?>
                <span class="text-[10px] text-slate-400 italic"><i class="fa-solid fa-lock mr-1"></i>Login to see details</span>
            <?php endif; ?>

            <a href="<?= $card_link ?>"
                class="flex-shrink-0 text-xs font-bold px-3 py-1.5 rounded-lg transition-all hover:shadow-md"
                style="background:linear-gradient(90deg,#008080,#0097a7);color:#fff">
                View <i class="fa-solid fa-arrow-right ml-1" style="font-size:9px"></i>
            </a>
        </div>
    </div>
</div>
