<?php
use BLR\Backline\Modules\Contacts\Contacts;
use BLR\Backline\Modules\Contacts\NewsletterDoubleOptIn;
use BLR\Backline\Modules\Contacts\Pages as ContactsPages;

if (!defined('ABSPATH')) exit;

$__blr_ba_data = isset($ctx['contact_view']) && is_array($ctx['contact_view']) ? $ctx['contact_view'] : ContactsPages::view_data($ctx);
foreach ($__blr_ba_data as $__blr_ba_key => $__blr_ba_value) {
  ${$__blr_ba_key} = $__blr_ba_value;
}
unset($__blr_ba_data, $__blr_ba_key, $__blr_ba_value);

if (!empty($not_found)) {
  echo '<div class="container-fluid p-0"><div class="alert alert-danger">' . esc_html__('Contact not found.', 'blr-backline') . '</div></div>';
  return;
}
?>

<div class="container-fluid p-0">
  <div class="d-flex align-items-center justify-content-between mb-3">
    <div>
      <h1 class="h3 mb-1"><?php echo esc_html(get_the_title($post)); ?></h1>
      <div class="text-muted"><?php echo esc_html(trim($role . ($company ? (' @ ' . $company) : '')) ?: '—'); ?></div>
    </div>
    <div class="d-flex gap-2">
      <a class="btn btn-outline-secondary" href="<?php echo esc_url($base . 'contacts/'); ?>"><?php echo esc_html__('Back', 'blr-backline'); ?></a>
      <?php if ($email): ?>
        <a class="btn btn-outline-primary" href="<?php echo esc_url($base . 'outreach/new/?contacts[]=' . (int)$id); ?>"><?php echo esc_html__('New Outreach', 'blr-backline'); ?></a>
      <?php endif; ?>
      <a class="btn btn-primary" href="<?php echo esc_url($edit_url); ?>"><?php echo esc_html__('Edit', 'blr-backline'); ?></a>
      <?php /* translators: %s: contact title used in the delete confirmation prompt. */ ?>
      <form method="post" action="<?php echo esc_url($delete_url); ?>" class="blr-ba-delete-form d-inline" data-item-label="<?php echo esc_attr(sprintf(__('contact “%s”', 'blr-backline'), get_the_title($post))); ?>">
        <?php wp_nonce_field('blr_ba_delete_blr_contact_' . (int)$id); ?>
        <button type="submit" class="btn btn-outline-danger"><?php echo esc_html__('Delete', 'blr-backline'); ?></button>
      </form>
    </div>
  </div>

  <div class="row g-3">
    <div class="col-12 col-lg-8">
      <div class="card">
        <div class="card-body">
          <div class="row g-3">
            <div class="col-12 col-md-6">
              <div class="text-muted small"><?php echo esc_html__('Email', 'blr-backline'); ?></div>
              <?php if ($email): ?>
                <a href="mailto:<?php echo esc_attr($email); ?>"><?php echo esc_html($email); ?></a>
              <?php else: ?>
                <div class="text-muted">—</div>
              <?php endif; ?>
            </div>
            <div class="col-12 col-md-6">
              <div class="text-muted small"><?php echo esc_html__('Phone', 'blr-backline'); ?></div>
              <div><?php echo esc_html($phone ?: '—'); ?></div>
            </div>
            <div class="col-12 col-md-4">
              <div class="text-muted small"><?php echo esc_html__('First Name', 'blr-backline'); ?></div>
              <div><?php echo esc_html($first_name ?: '—'); ?></div>
            </div>
            <div class="col-12 col-md-4">
              <div class="text-muted small"><?php echo esc_html__('Contact Type', 'blr-backline'); ?></div>
              <div><?php echo esc_html($type_label); ?></div>
            </div>
            <div class="col-12 col-md-4">
              <div class="text-muted small"><?php echo esc_html__('Newsletter Status', 'blr-backline'); ?></div>
              <div><?php echo esc_html($newsletter_label); ?></div>
            </div>
            <div class="col-12 col-md-6">
              <div class="text-muted small"><?php echo esc_html__('Location', 'blr-backline'); ?></div>
              <div><?php echo esc_html(trim($city . ($state ? (', ' . $state) : '')) ?: '—'); ?></div>
            </div>
            <div class="col-12 col-md-6">
              <div class="text-muted small"><?php echo esc_html__('Website', 'blr-backline'); ?></div>
              <?php if ($website): ?>
                <a href="<?php echo esc_url($website); ?>" target="_blank" rel="noopener"><?php echo esc_html($website); ?></a>
              <?php else: ?>
                <div class="text-muted">—</div>
              <?php endif; ?>
            </div>
            <div class="col-12 col-md-6">
              <div class="text-muted small"><?php echo esc_html__('Signup Requested', 'blr-backline'); ?></div>
              <div><?php echo esc_html($newsletter_requested ?: '—'); ?></div>
            </div>
            <div class="col-12 col-md-6">
              <div class="text-muted small"><?php echo esc_html__('Signup Confirmed', 'blr-backline'); ?></div>
              <div><?php echo esc_html($newsletter_confirmed ?: '—'); ?></div>
            </div>
          </div>

          <hr>

          <div class="text-muted small mb-2"><?php echo esc_html__('Notes', 'blr-backline'); ?></div>
          <?php if (trim((string)$post->post_content) !== ''): ?>
            <?php echo wp_kses_post(wpautop($post->post_content)); ?>
          <?php else: ?>
            <div class="text-muted"><?php echo esc_html__('No notes yet.', 'blr-backline'); ?></div>
          <?php endif; ?>

          <hr>

          <div class="text-muted small mb-2"><?php echo esc_html__('Internal Notes', 'blr-backline'); ?></div>
          <?php if (trim($internal_notes) !== ''): ?>
            <?php echo wp_kses_post(wpautop($internal_notes)); ?>
          <?php else: ?>
            <div class="text-muted"><?php echo esc_html__('No internal notes yet.', 'blr-backline'); ?></div>
          <?php endif; ?>
        </div>
      </div>
    </div>

    <div class="col-12 col-lg-4">
      <div class="card">
        <div class="card-header"><h5 class="card-title mb-0"><?php echo esc_html__('Outreach Snapshot', 'blr-backline'); ?></h5></div>
        <div class="card-body">
          <div class="mb-3">
            <div class="text-muted small"><?php echo esc_html__('Last Contacted', 'blr-backline'); ?></div>
            <div><?php echo esc_html($last_contacted ?: '—'); ?></div>
          </div>
          <div class="mb-3">
            <div class="text-muted small"><?php echo esc_html__('Last Reply', 'blr-backline'); ?></div>
            <div><?php echo esc_html($last_reply ?: '—'); ?></div>
          </div>
          <div class="mb-3">
            <div class="text-muted small"><?php echo esc_html__('Emails Sent', 'blr-backline'); ?></div>
            <div><?php echo esc_html($total_sent !== '' ? $total_sent : '0'); ?></div>
          </div>
          <div class="mb-0">
            <div class="text-muted small"><?php echo esc_html__('Outreach Status', 'blr-backline'); ?></div>
            <div><?php echo esc_html($outreach_status ?: '—'); ?></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
