<?php

use MintyPHP\I18n;

$values = $user ?? [];
$profileKey = trim((string) ($profileKey ?? 'user-profile'));
if ($profileKey === '') {
  $profileKey = 'user-profile';
}
$name = trim(($values['first_name'] ?? '') . ' ' . ($values['last_name'] ?? ''));
$displayName = $name !== '' ? $name : ($values['email'] ?? t('Address book'));
$email = trim((string) ($values['email'] ?? ''));
$phone = trim((string) ($values['phone'] ?? ''));
$mobile = trim((string) ($values['mobile'] ?? ''));
$shortDial = trim((string) ($values['short_dial'] ?? ''));
$address = trim((string) ($values['address'] ?? ''));
$postalCode = trim((string) ($values['postal_code'] ?? ''));
$city = trim((string) ($values['city'] ?? ''));
$region = trim((string) ($values['region'] ?? ''));
$country = trim((string) ($values['country'] ?? ''));
$profileDescription = trim((string) ($values['profile_description'] ?? ''));
$jobTitle = trim((string) ($values['job_title'] ?? ''));
$hireDate = trim((string) ($values['hire_date'] ?? ''));
$hireDateLabel = '';
if ($hireDate !== '') {
  $format = (strpos((string) (I18n::$locale ?? ''), 'de') === 0) ? 'd.m.Y' : 'Y-m-d';
  try {
    $hireDateLabel = (new \DateTimeImmutable($hireDate))->format($format);
  } catch (\Exception $e) {
    $hireDateLabel = $hireDate;
  }
}
$avatarUuid = (string) ($values['uuid'] ?? '');
$hasAvatar = !empty($values['has_avatar']) && $avatarUuid !== '';

$initials = '';
if ($displayName !== '') {
  $parts = array_filter(array_map('trim', preg_split('/\s+/', $displayName) ?: []));
  $chars = '';
  foreach ($parts as $part) {
    if (function_exists('mb_substr')) {
      $chars .= mb_substr($part, 0, 1);
    } else {
      $chars .= substr($part, 0, 1);
    }
  }
  $initials = strtoupper($chars !== '' ? $chars : '?');
}
$tenantGroups = $values['tenant_groups'] ?? [];
$tenantGroups = is_array($tenantGroups) ? $tenantGroups : [];
$primaryTenantLabel = '';
$primaryTenantDepartments = [];
foreach ($tenantGroups as $group) {
  if (!empty($group['is_primary'])) {
    $primaryTenantLabel = trim((string) ($group['label'] ?? ''));
    $primaryDepts = $group['departments'] ?? [];
    $primaryTenantDepartments = is_array($primaryDepts) ? $primaryDepts : [];
    break;
  }
}

$hasContact = ($email !== '' || $phone !== '' || $mobile !== '');
$hasAddress = ($address !== '' || $postalCode !== '' || $city !== '' || $region !== '' || $country !== '');
// Organization tab only appears for multi-tenant users — single-tenant info
// is already communicated through the header chips, and redundancy hurts more
// than a minor asymmetry.
$hasOrganization = count($tenantGroups) > 1;
$hasAbout = ($profileDescription !== '' || $hireDateLabel !== '' || $shortDial !== '');
$hasAnyTab = ($hasAddress || $hasAbout || $hasOrganization);

$copyEmailLabel = t('Copy email address');
$copyPhoneLabel = t('Copy phone number');
$copyMobileLabel = t('Copy mobile number');

?>

<div class="app-details-container">
  <section>
    <div class="app-profile-card-container">
      <div class="app-profile-card">
        <div class="app-profile-banner"></div>
        <div class="app-profile-header">
          <div class="user-avatar-block avatar-round app-profile-avatar">
            <?php if ($hasAvatar): ?>
              <a data-fslightbox="<?php e($profileKey); ?>-avatar"
                href="admin/users/avatar-file?uuid=<?php e($avatarUuid); ?>&size=256">
                <img class="user-avatar-image" src="admin/users/avatar-file?uuid=<?php e($avatarUuid); ?>&size=160"
                  alt="">
              </a>
            <?php else: ?>
              <span class="user-avatar-placeholder"><?php e($initials ?: '?'); ?></span>
            <?php endif; ?>
          </div>

          <div class="app-profile-identity">
            <hgroup class="app-profile-identity-name">
              <h2><?php e($displayName); ?></h2>
              <?php if ($jobTitle !== ''): ?>
                <p><?php e($jobTitle); ?></p>
              <?php endif; ?>
            </hgroup>

            <?php if ($primaryTenantLabel !== '' || !empty($primaryTenantDepartments) || $hireDateLabel !== ''): ?>
              <div class="app-profile-chips" aria-label="<?php e(t('Summary')); ?>">
                <?php if ($primaryTenantLabel !== ''): ?>
                  <span class="badge" data-variant="neutral">
                    <i class="bi bi-building" aria-hidden="true"></i>
                    <?php e($primaryTenantLabel); ?>
                    <small>· <?php e(t('Primary')); ?></small>
                  </span>
                <?php endif; ?>
                <?php foreach (array_slice($primaryTenantDepartments, 0, 3) as $dept): ?>
                  <span class="badge" data-variant="neutral">
                    <i class="bi bi-tag" aria-hidden="true"></i>
                    <?php e($dept); ?>
                  </span>
                <?php endforeach; ?>
                <?php if ($hireDateLabel !== ''): ?>
                  <span class="badge" data-variant="neutral">
                    <i class="bi bi-calendar-event" aria-hidden="true"></i>
                    <?php e(t('Hired')); ?> <?php e($hireDateLabel); ?>
                  </span>
                <?php endif; ?>
              </div>
            <?php endif; ?>
          </div>

          <?php if ($hasContact): ?>
            <div class="app-profile-actions">
              <?php if ($email !== ''): ?>
                <a role="button" class="secondary outline small app-profile-action-button"
                   href="mailto:<?php e($email); ?>">
                  <i class="bi bi-envelope" aria-hidden="true"></i>
                  <span><?php e(t('Send email')); ?></span>
                </a>
              <?php endif; ?>
              <?php if ($phone !== '' || $mobile !== ''): ?>
                <?php $callTarget = $phone !== '' ? $phone : $mobile; ?>
                <a role="button" class="secondary outline small app-profile-action-button"
                   href="tel:<?php e($callTarget); ?>">
                  <i class="bi bi-telephone" aria-hidden="true"></i>
                  <span><?php e(t('Call')); ?></span>
                </a>
              <?php endif; ?>
            </div>
          <?php endif; ?>
        </div>

        <?php if ($hasContact): ?>
          <ul class="app-profile-contact-list" aria-label="<?php e(t('Contact')); ?>">
            <?php if ($email !== ''): ?>
              <li class="app-profile-contact-row">
                <a class="app-profile-contact-value" href="mailto:<?php e($email); ?>" aria-label="<?php e(t('Email')); ?>: <?php e($email); ?>">
                  <span class="app-profile-contact-icon" aria-hidden="true"
                        data-tooltip="<?php e(t('Email')); ?>" data-tooltip-pos="right">
                    <i class="bi bi-envelope"></i>
                  </span>
                  <span class="app-profile-contact-text"><?php e($email); ?></span>
                </a>
                <button type="button" class="app-copy-button" data-copy-button
                        data-copy-value="<?php e($email); ?>"
                        aria-label="<?php e($copyEmailLabel); ?>"
                        data-tooltip="<?php e($copyEmailLabel); ?>" data-tooltip-pos="left">
                  <i class="bi bi-copy" data-copy-icon-idle aria-hidden="true"></i>
                  <i class="bi bi-check2" data-copy-icon-done aria-hidden="true"></i>
                </button>
              </li>
            <?php endif; ?>
            <?php if ($phone !== ''): ?>
              <li class="app-profile-contact-row">
                <a class="app-profile-contact-value" href="tel:<?php e($phone); ?>" aria-label="<?php e(t('Phone')); ?>: <?php e($phone); ?>">
                  <span class="app-profile-contact-icon" aria-hidden="true"
                        data-tooltip="<?php e(t('Phone')); ?>" data-tooltip-pos="right">
                    <i class="bi bi-telephone"></i>
                  </span>
                  <span class="app-profile-contact-text"><?php e($phone); ?></span>
                </a>
                <button type="button" class="app-copy-button" data-copy-button
                        data-copy-value="<?php e($phone); ?>"
                        aria-label="<?php e($copyPhoneLabel); ?>"
                        data-tooltip="<?php e($copyPhoneLabel); ?>" data-tooltip-pos="left">
                  <i class="bi bi-copy" data-copy-icon-idle aria-hidden="true"></i>
                  <i class="bi bi-check2" data-copy-icon-done aria-hidden="true"></i>
                </button>
              </li>
            <?php endif; ?>
            <?php if ($mobile !== ''): ?>
              <li class="app-profile-contact-row">
                <a class="app-profile-contact-value" href="tel:<?php e($mobile); ?>" aria-label="<?php e(t('Mobile')); ?>: <?php e($mobile); ?>">
                  <span class="app-profile-contact-icon" aria-hidden="true"
                        data-tooltip="<?php e(t('Mobile')); ?>" data-tooltip-pos="right">
                    <i class="bi bi-phone"></i>
                  </span>
                  <span class="app-profile-contact-text"><?php e($mobile); ?></span>
                </a>
                <button type="button" class="app-copy-button" data-copy-button
                        data-copy-value="<?php e($mobile); ?>"
                        aria-label="<?php e($copyMobileLabel); ?>"
                        data-tooltip="<?php e($copyMobileLabel); ?>" data-tooltip-pos="left">
                  <i class="bi bi-copy" data-copy-icon-idle aria-hidden="true"></i>
                  <i class="bi bi-check2" data-copy-icon-done aria-hidden="true"></i>
                </button>
              </li>
            <?php endif; ?>
          </ul>
        <?php endif; ?>
      </div>
    </div>

    <?php if ($hasAnyTab): ?>
      <div class="app-profile-body-container">
        <div class="app-profile-body">
          <div class="app-tabs app-profile-tabs" data-tabs data-app-component="tabs"
               data-tabs-param="tab" data-tabs-storage-key="<?php e($profileKey); ?>">
            <div class="app-tabs-nav">
              <?php
                $firstTab = true;
                $renderTabButton = function (string $key, string $label) use (&$firstTab): void {
                  $default = $firstTab ? ' data-tab-default' : '';
                  $firstTab = false;
                  echo '<button type="button" data-tab="' . htmlspecialchars($key, ENT_QUOTES, 'UTF-8') . '"' . $default . '>'
                    . htmlspecialchars($label, ENT_QUOTES, 'UTF-8') . '</button>';
                };
              ?>
              <?php if ($hasAddress) { $renderTabButton('address', t('Address')); } ?>
              <?php if ($hasOrganization) { $renderTabButton('organization', t('Organization')); } ?>
              <?php if ($hasAbout) { $renderTabButton('about', t('About')); } ?>
            </div>

            <?php if ($hasAddress): ?>
              <div data-tab-panel="address" class="app-profile-panel">
                <address class="app-profile-address">
                  <?php if ($address !== ''): ?>
                    <span><?php e($address); ?></span>
                  <?php endif; ?>
                  <?php $line2 = trim($postalCode . ' ' . $city); ?>
                  <?php if ($line2 !== ''): ?>
                    <span><?php e($line2); ?></span>
                  <?php endif; ?>
                  <?php if ($region !== ''): ?>
                    <span><?php e($region); ?></span>
                  <?php endif; ?>
                  <?php if ($country !== ''): ?>
                    <span><?php e($country); ?></span>
                  <?php endif; ?>
                </address>
              </div>
            <?php endif; ?>

            <?php if ($hasOrganization): ?>
              <div data-tab-panel="organization" class="app-profile-panel">
                <ul class="app-profile-tenant-list">
                  <?php foreach ($tenantGroups as $group): ?>
                    <?php
                      $label = trim((string) ($group['label'] ?? ''));
                      if ($label === '') { continue; }
                      $departments = $group['departments'] ?? [];
                      $departments = is_array($departments) ? $departments : [];
                      $isPrimary = !empty($group['is_primary']);
                    ?>
                    <li class="app-profile-tenant-item">
                      <div class="app-profile-tenant-label">
                        <i class="bi bi-building" aria-hidden="true"></i>
                        <strong><?php e($label); ?></strong>
                        <?php if ($isPrimary): ?>
                          <small class="app-profile-tenant-primary">· <?php e(t('Primary')); ?></small>
                        <?php endif; ?>
                      </div>
                      <?php if (!empty($departments)): ?>
                        <div class="app-profile-tenant-departments">
                          <?php e(implode(', ', $departments)); ?>
                        </div>
                      <?php endif; ?>
                    </li>
                  <?php endforeach; ?>
                </ul>
              </div>
            <?php endif; ?>

            <?php if ($hasAbout): ?>
              <div data-tab-panel="about" class="app-profile-panel">
                <?php if ($hireDateLabel !== ''): ?>
                  <div class="app-profile-key-value">
                    <small><?php e(t('Hire date')); ?></small>
                    <p><?php e($hireDateLabel); ?></p>
                  </div>
                <?php endif; ?>
                <?php if ($shortDial !== ''): ?>
                  <div class="app-profile-key-value">
                    <small><?php e(t('Short dial')); ?></small>
                    <p><?php e($shortDial); ?></p>
                  </div>
                <?php endif; ?>
                <?php if ($profileDescription !== ''): ?>
                  <div class="app-profile-about-description">
                    <?php foreach (preg_split('/\\r\\n|\\r|\\n/', $profileDescription) as $line): ?>
                      <?php if (trim($line) !== ''): ?>
                        <p><?php e($line); ?></p>
                      <?php endif; ?>
                    <?php endforeach; ?>
                  </div>
                <?php endif; ?>
              </div>
            <?php endif; ?>
          </div>
        </div>
      </div>
    <?php endif; ?>
  </section>
</div>
