<?php
declare(strict_types=1);

require_once __DIR__ . '/common.php';

$config = kapvoe_load_config();

header('Content-Type: application/json; charset=utf-8');

$configuredToken = trim((string)($config['admin_tools_token'] ?? ''));
$providedToken = trim((string)($_GET['key'] ?? ''));

if ($configuredToken === '') {
    http_response_code(403);
    echo json_encode([
        'ok' => false,
        'error' => 'Falta configurar admin_tools_token a config.php',
    ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    exit;
}

if (!hash_equals($configuredToken, $providedToken)) {
    http_response_code(403);
    echo json_encode([
        'ok' => false,
        'error' => 'Token invalid',
    ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    exit;
}

$defaultRecipient = trim((string)($config['admin_notification_email'] ?? ''));
$toEmail = trim((string)($_GET['to'] ?? $defaultRecipient));

if ($toEmail === '' || !filter_var($toEmail, FILTER_VALIDATE_EMAIL)) {
    http_response_code(400);
    echo json_encode([
        'ok' => false,
        'error' => 'Cal una adreca de correu valida',
    ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    exit;
}

$transport = strtolower(trim((string)($config['mail_transport'] ?? 'auto')));
$smtpHost = trim((string)($config['smtp_host'] ?? ''));
$smtpPort = (int)($config['smtp_port'] ?? 0);

$subject = 'Prova SMTP Blood Bros Sports';
$htmlBody = '<!DOCTYPE html>
<html lang="ca">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Prova SMTP</title>
</head>
<body style="margin:0;padding:24px;background:#081221;font-family:Arial,Helvetica,sans-serif;color:#f4f7fb;">
  <div style="max-width:720px;margin:0 auto;border-radius:24px;border:1px solid rgba(255,255,255,.10);background:linear-gradient(180deg,#172544 0%,#0f1b32 100%);padding:24px;">
    <h1 style="margin:0 0 12px;font-size:28px;">Prova d\'enviament correcta</h1>
    <p style="margin:0 0 14px;line-height:1.6;color:#d8e3f3;">Aquest correu s\'ha enviat des de <strong>test-mail.php</strong> per comprovar la configuracio SMTP del projecte.</p>
    <div style="padding:16px;border-radius:16px;background:rgba(255,255,255,.05);line-height:1.7;">
      <div><strong>Data:</strong> ' . htmlspecialchars(date('Y-m-d H:i:s'), ENT_QUOTES, 'UTF-8') . '</div>
      <div><strong>Destinatari:</strong> ' . htmlspecialchars($toEmail, ENT_QUOTES, 'UTF-8') . '</div>
      <div><strong>Transport:</strong> ' . htmlspecialchars($transport, ENT_QUOTES, 'UTF-8') . '</div>
      <div><strong>SMTP host:</strong> ' . htmlspecialchars($smtpHost !== '' ? $smtpHost : '(buit)', ENT_QUOTES, 'UTF-8') . '</div>
      <div><strong>SMTP port:</strong> ' . htmlspecialchars((string)$smtpPort, ENT_QUOTES, 'UTF-8') . '</div>
      <div><strong>Servidor:</strong> ' . htmlspecialchars((string)($_SERVER['HTTP_HOST'] ?? 'localhost'), ENT_QUOTES, 'UTF-8') . '</div>
    </div>
  </div>
</body>
</html>';

$sent = kapvoe_send_html_email($config, $toEmail, $subject, $htmlBody);

echo json_encode([
    'ok' => $sent,
    'to' => $toEmail,
    'subject' => $subject,
    'transport' => $transport,
    'smtp_host' => $smtpHost,
    'smtp_port' => $smtpPort,
    'mail_log' => kapvoe_mail_log_path($config),
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
