<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- False positive: locally-scoped variables (template view-vars via TemplateLoader::render() extract+include within a method, or closure locals), not plugin globals.
defined( 'ABSPATH' ) || exit;
/**
 * Contact details partial.
 *
 * Expected variables:
 * - Optional: $bookaro_contacts
 * - Optional fallbacks: $provider, $provider_email, $provider_phone, $owner, $owner_name, $email, $customer_phone
 */
$bookaro_contacts = isset( $bookaro_contacts ) && is_array( $bookaro_contacts ) ? $bookaro_contacts : [];
if ( empty( $bookaro_contacts ) ) {
    $provider_name = isset( $provider ) ? (string) $provider : '';
    $provider_email = isset( $provider_email ) ? (string) $provider_email : '';
    $provider_phone = isset( $provider_phone ) ? (string) $provider_phone : '';
    $customer_name = isset( $owner_name ) && is_string( $owner_name ) && $owner_name !== ''
        ? $owner_name
        : ( isset( $owner ) ? (string) $owner : '' );
    $customer_email = isset( $email ) ? (string) $email : '';
    $customer_phone = isset( $customer_phone ) ? (string) $customer_phone : '';
    $bookaro_contacts = [
        'provider' => [
            'label' => __( 'Provider', 'mybookaro' ),
            'name' => $provider_name,
            'email' => $provider_email,
            'phone' => $provider_phone,
        ],
        'customer' => [
            'label' => __( 'Customer', 'mybookaro' ),
            'name' => $customer_name,
            'email' => $customer_email,
            'phone' => $customer_phone,
        ],
    ];
}

$bookaro_rows = [];
foreach ( $bookaro_contacts as $contact ) {
    if ( ! is_array( $contact ) ) {
        continue;
    }
    $label = isset( $contact['label'] ) ? trim( (string) $contact['label'] ) : '';
    if ( $label === '' ) {
        continue;
    }
    $parts = [];
    foreach ( [ 'name', 'email', 'phone' ] as $part_key ) {
        $part = isset( $contact[ $part_key ] ) ? trim( (string) $contact[ $part_key ] ) : '';
        if ( $part !== '' ) {
            $parts[] = $part;
        }
    }
    if ( empty( $parts ) ) {
        continue;
    }
    $bookaro_rows[] = [
        'label' => $label,
        'value' => implode( ' | ', $parts ),
    ];
}
if ( empty( $bookaro_rows ) ) {
    return;
}
?>
<h3 class="bookaro-email-subheading"><?php esc_html_e( 'Provider and customer contact', 'mybookaro' ); ?></h3>
<table class="details-table" cellpadding="0" cellspacing="0" role="presentation" style="width:100%; border-collapse:collapse; margin:14px 0 18px;">
    <tbody>
        <?php foreach ( $bookaro_rows as $bookaro_row ) : ?>
            <tr>
                <td style="padding:8px 10px; background:#f9fafb; font-weight:600; border-bottom:1px solid #e5e7eb; width:35%;">
                    <?php echo esc_html( $bookaro_row['label'] ); ?>
                </td>
                <td style="padding:8px 10px; border-bottom:1px solid #e5e7eb;">
                    <?php echo esc_html( $bookaro_row['value'] ); ?>
                </td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>
