<?php
$settings = is_array($settings ?? null) ? $settings : [];
$userEmail = trim((string) ($settings['user_email'] ?? ''));
$sender = trim((string) ($settings['sender'] ?? ''));
$hasPassword = (bool) ($settings['has_password'] ?? false);
$isActive = (bool) ($settings['is_active'] ?? true);
$convertToGsm7 = (bool) ($settings['convert_message_to_gsm7'] ?? false);
$lastTestAt = trim((string) ($settings['last_test_at'] ?? ''));
$lastTestStatus = trim((string) ($settings['last_test_status'] ?? ''));
$lastTestMessage = trim((string) ($settings['last_test_message'] ?? ''));
$lastTestHttpCode = $settings['last_test_http_code'] ?? null;
$lastMessageId = '';
if (str_starts_with($lastTestMessage, 'MessageId:')) {
    $lastMessageId = trim(substr($lastTestMessage, strlen('MessageId:')));
}
?>

<section class="card">
  <h2 class="section-title"><?= $e($t('settings.hostedsms.title')) ?></h2>
  <p class="muted mt-12"><?= $e($t('settings.hostedsms.description')) ?></p>

  <?php if (!empty($errorMessage)): ?>
    <div class="mt-12"><?php $type='danger'; $message=(string) $errorMessage; $dismissible=true; include dirname(__DIR__) . '/components/alert.php'; ?></div>
  <?php endif; ?>

  <?php if (!empty($successMessage)): ?>
    <div class="mt-12"><?php $type='success'; $message=(string) $successMessage; $dismissible=true; include dirname(__DIR__) . '/components/alert.php'; ?></div>
  <?php endif; ?>

  <?php if (!empty($testMessage)): ?>
    <div class="mt-12"><?php $type='info'; $message=(string) $testMessage; $dismissible=true; include dirname(__DIR__) . '/components/alert.php'; ?></div>
  <?php endif; ?>
</section>

<section class="card mt-16">
  <h3 class="section-title"><?= $e($t('settings.hostedsms.config.title')) ?></h3>

  <div class="muted mt-12">
    <?= $e($t('settings.hostedsms.status.password')) ?>:
    <strong><?= $e($hasPassword ? $t('settings.hostedsms.status.saved') : $t('settings.hostedsms.status.missing')) ?></strong>
    |
    <?= $e($t('settings.hostedsms.status.active')) ?>:
    <strong><?= $e($isActive ? $t('settings.integrations_hub.active.yes') : $t('settings.integrations_hub.active.no')) ?></strong>
  </div>

  <form class="statuses-form mt-16" action="/settings/integrations/hostedsms/save" method="post" novalidate>
    <input type="hidden" name="_token" value="<?= $e($csrfToken ?? '') ?>">

    <label class="form-field">
      <span class="field-label"><?= $e($t('settings.hostedsms.fields.user_email')) ?></span>
      <input class="form-control" type="email" name="user_email" maxlength="190" value="<?= $e($userEmail) ?>" required>
    </label>

    <label class="form-field">
      <span class="field-label"><?= $e($t('settings.hostedsms.fields.password')) ?></span>
      <input class="form-control" type="password" name="password" autocomplete="new-password" placeholder="<?= $hasPassword ? '********' : '' ?>" <?= $hasPassword ? '' : 'required' ?>>
      <span class="muted"><?= $e($hasPassword ? $t('settings.hostedsms.password.saved') : $t('settings.hostedsms.password.missing')) ?></span>
    </label>

    <label class="form-field">
      <span class="field-label"><?= $e($t('settings.hostedsms.fields.sender')) ?></span>
      <input class="form-control" type="text" name="sender" maxlength="32" value="<?= $e($sender) ?>" required>
      <span class="muted"><?= $e($t('settings.hostedsms.hints.sender')) ?></span>
    </label>

    <label class="form-field form-field--inline">
      <input type="checkbox" name="convert_message_to_gsm7" value="1"<?= $convertToGsm7 ? ' checked' : '' ?>>
      <span class="field-label"><?= $e($t('settings.hostedsms.fields.convert_message_to_gsm7')) ?></span>
    </label>

    <label class="form-field form-field--inline">
      <input type="checkbox" name="is_active" value="1"<?= $isActive ? ' checked' : '' ?>>
      <span class="field-label"><?= $e($t('settings.hostedsms.fields.is_active')) ?></span>
    </label>

    <div class="form-actions mt-16">
      <button type="submit" class="btn btn--primary"><?= $e($t('settings.hostedsms.actions.save')) ?></button>
    </div>
  </form>
</section>

<section class="card mt-16">
  <h3 class="section-title"><?= $e($t('settings.hostedsms.test.title')) ?></h3>
  <p class="muted mt-12"><?= $e($t('settings.hostedsms.test.description')) ?></p>

  <form class="statuses-form mt-16" action="/settings/integrations/hostedsms/test" method="post" novalidate>
    <input type="hidden" name="_token" value="<?= $e($csrfToken ?? '') ?>">

    <label class="form-field">
      <span class="field-label"><?= $e($t('settings.hostedsms.fields.test_phone')) ?></span>
      <input class="form-control" type="tel" name="phone" inputmode="tel" placeholder="48xxxxxxxxx" required>
    </label>

    <label class="form-field">
      <span class="field-label"><?= $e($t('settings.hostedsms.fields.test_message')) ?></span>
      <textarea class="form-control" name="message" rows="4" maxlength="4000" required>Test orderPRO HostedSMS</textarea>
    </label>

    <div class="form-actions mt-16">
      <button type="submit" class="btn btn--secondary"><?= $e($t('settings.hostedsms.actions.send_test')) ?></button>
    </div>
  </form>

  <?php if ($lastTestAt !== ''): ?>
    <div class="alert <?= $lastTestStatus === 'ok' ? 'alert--success' : 'alert--danger' ?> mt-16" role="status">
      <div>
        <strong><?= $e($t('settings.hostedsms.status.last_test')) ?>:</strong>
        <?= $e($lastTestAt) ?>
        <?php if ($lastTestStatus !== ''): ?>
          <span class="badge badge--<?= $lastTestStatus === 'ok' ? 'success' : 'muted' ?>"><?= $e(strtoupper($lastTestStatus)) ?></span>
        <?php endif; ?>
        <?php if ($lastTestHttpCode !== null): ?>
          <span class="badge badge--muted">HTTP <?= $e((string) $lastTestHttpCode) ?></span>
        <?php endif; ?>
      </div>
      <?php if ($lastMessageId !== ''): ?>
        <div class="mt-12">
          <?= $e($t('settings.hostedsms.status.message_id')) ?>:
          <code><?= $e($lastMessageId) ?></code>
        </div>
      <?php elseif ($lastTestMessage !== ''): ?>
        <div class="mt-12"><?= $e($lastTestMessage) ?></div>
      <?php endif; ?>
    </div>
  <?php endif; ?>
</section>
