<?php

use MintyPHP\Module\Security\Service\SecurityBcSettingsGateway;

$authMode = (string) ($authMode ?? SecurityBcSettingsGateway::AUTH_MODE_BASIC);
$odataBaseUrl = (string) ($odataBaseUrl ?? '');
$companyName = (string) ($companyName ?? '');
$basicUser = (string) ($basicUser ?? '');
$hasBasicPassword = (bool) ($hasBasicPassword ?? false);
$oauthTenantId = (string) ($oauthTenantId ?? '');
$oauthClientId = (string) ($oauthClientId ?? '');
$hasOAuthClientSecret = (bool) ($hasOAuthClientSecret ?? false);
$oauthTokenEndpoint = (string) ($oauthTokenEndpoint ?? '');
$configErrors = is_array($configErrors ?? null) ? $configErrors : [];
$isOauth = $authMode === SecurityBcSettingsGateway::AUTH_MODE_OAUTH2;
?>
<div class="app-details-container">
  <section data-tab-panel>
    <?php
    $titlebar = [
        'title' => t('Security settings'),
        'actions' => [
            ['label' => t('Save'), 'type' => 'submit', 'form' => 'security-settings-form', 'class' => 'primary', 'tone' => 'success'],
        ],
    ];
    require templatePath('partials/app-details-titlebar.phtml');
    ?>

    <?php if ($configErrors !== []): ?>
      <div class="notice" data-variant="warning" role="status">
        <p><strong><?php e(t('Connection not fully configured')); ?></strong></p>
        <ul>
          <?php foreach ($configErrors as $missing): ?><li><?php e((string) $missing); ?></li><?php endforeach; ?>
        </ul>
      </div>
    <?php endif; ?>

    <form method="post" id="security-settings-form" data-app-component="security-settings">
      <div class="app-details-card">
        <h3><?php e(t('Business Central connection')); ?></h3>

        <div class="security-field">
          <label for="security-auth-mode"><?php e(t('Authentication mode')); ?></label>
          <select id="security-auth-mode" name="auth_mode" data-security-auth-mode>
            <option value="basic"<?php if (!$isOauth): ?> selected<?php endif; ?>><?php e(t('Basic authentication')); ?></option>
            <option value="oauth2"<?php if ($isOauth): ?> selected<?php endif; ?>><?php e(t('OAuth2 (client credentials)')); ?></option>
          </select>
        </div>

        <div class="security-field">
          <label for="security-odata-url"><?php e(t('OData base URL')); ?></label>
          <input type="url" id="security-odata-url" name="odata_base_url" value="<?php e($odataBaseUrl); ?>" placeholder="https://bc.example.com:7048/BC/ODataV4">
        </div>

        <div class="security-field">
          <label for="security-company"><?php e(t('Company name')); ?></label>
          <input type="text" id="security-company" name="company_name" value="<?php e($companyName); ?>" autocomplete="off">
        </div>
      </div>

      <div class="app-details-card" data-security-auth-section="basic"<?php if ($isOauth): ?> hidden<?php endif; ?>>
        <h3><?php e(t('Basic authentication')); ?></h3>
        <div class="security-field">
          <label for="security-basic-user"><?php e(t('Username')); ?></label>
          <input type="text" id="security-basic-user" name="basic_user" value="<?php e($basicUser); ?>" autocomplete="off">
        </div>
        <div class="security-field">
          <label for="security-basic-pass"><?php e(t('Password')); ?></label>
          <input type="password" id="security-basic-pass" name="basic_password" value="" autocomplete="new-password"
                 placeholder="<?php e($hasBasicPassword ? t('•••••••• (leave blank to keep)') : ''); ?>">
        </div>
      </div>

      <div class="app-details-card" data-security-auth-section="oauth2"<?php if (!$isOauth): ?> hidden<?php endif; ?>>
        <h3><?php e(t('OAuth2 (client credentials)')); ?></h3>
        <div class="security-field">
          <label for="security-oauth-token-endpoint"><?php e(t('Token endpoint')); ?></label>
          <input type="url" id="security-oauth-token-endpoint" name="oauth_token_endpoint" value="<?php e($oauthTokenEndpoint); ?>"
                 placeholder="https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token">
        </div>
        <div class="security-field">
          <label for="security-oauth-tenant"><?php e(t('Directory (tenant) ID')); ?></label>
          <input type="text" id="security-oauth-tenant" name="oauth_tenant_id" value="<?php e($oauthTenantId); ?>" autocomplete="off">
        </div>
        <div class="security-field">
          <label for="security-oauth-client"><?php e(t('Client ID')); ?></label>
          <input type="text" id="security-oauth-client" name="oauth_client_id" value="<?php e($oauthClientId); ?>" autocomplete="off">
        </div>
        <div class="security-field">
          <label for="security-oauth-secret"><?php e(t('Client secret')); ?></label>
          <input type="password" id="security-oauth-secret" name="oauth_client_secret" value="" autocomplete="new-password"
                 placeholder="<?php e($hasOAuthClientSecret ? t('•••••••• (leave blank to keep)') : ''); ?>">
        </div>
      </div>

      <?php \MintyPHP\Session::getCsrfInput(); ?>
    </form>

    <div class="app-details-card">
      <h3><?php e(t('Test connection')); ?></h3>
      <p class="app-muted"><?php e(t('Save your settings first, then test the connection to Business Central.')); ?></p>
      <button type="button" class="secondary outline" data-security-test-connection
              data-url="<?php e(lurl('security/settings/test-connection-data')); ?>"
              data-text-testing="<?php e(t('Testing...')); ?>"
              data-text-ok="<?php e(t('Connection successful')); ?>"
              data-text-fail="<?php e(t('Connection failed')); ?>">
        <i class="bi bi-plug"></i> <?php e(t('Test connection')); ?>
      </button>
      <span data-security-test-result role="status" aria-live="polite"></span>
    </div>
  </section>
</div>
