<?php
require 'functions.php';
$user = current_user();
if (!$user) header('Location: index.php');
$full_name = htmlspecialchars(trim($user['first_name'] . ' ' . $user['last_name']));
// external_mail.php - Standalone External Email Sender

// Brevo API Key - REPLACE WITH YOUR REAL KEY
$api_key = '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';

// Verified sender email (must be verified in Brevo dashboard)
$sender_email = $user['email'] ?? 'no-reply@BCA.server.edu';
$sender_name = $full_name;;

$message = '';
$success = false;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $to_email = trim($_POST['to_email'] ?? '');
    $subject = trim($_POST['subject'] ?? '');
    $body = trim($_POST['body'] ?? '');

    if (!filter_var($to_email, FILTER_VALIDATE_EMAIL) || !$subject || !$body) {
        $message = '<div class="alert alert-danger">All fields are required and email must be valid.</div>';
    } else {
        $html_body = "
        <html>
            <body style='font-family: Arial, sans-serif; max-width: 600px; margin: auto; padding: 20px; background: #f9f9f9; border-radius: 10px;'>
                <div style='background: white; padding: 30px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1);'>
                    <h2 style='color: #d93025; margin-top: 0;'>$subject</h2>
                    <p><strong>From:</strong> $sender_name &lt;$sender_email&gt;</p>
                    <hr style='border: 1px solid #eee;'>
                    <div style='line-height: 1.6; color: #333;'>
                        " . nl2br(htmlspecialchars($body, ENT_QUOTES, 'UTF-8')) . "
                    </div>
                    <hr style='border: 1px solid #eee;'>
                    <p style='color: #777; font-size: 0.9em;'>Sent via Bethany Christian Academy Mail System</p>
                </div>
            </body>
        </html>";

        $data = [
            'sender' => ['name' => $sender_name, 'email' => $sender_email],
            'to' => [['email' => $to_email]],
            'subject' => $subject,
            'htmlContent' => $html_body,
            'replyTo' => ['email' => $sender_email, 'name' => $sender_name]
        ];

        $url = 'https://api.brevo.com/v3/smtp/email';

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'accept: application/json',
            'api-key: ' . $api_key,
            'content-type: application/json'
        ]);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);

        $response = curl_exec($ch);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if ($http_code === 201) {
            $success = true;
            $message = '<div class="alert alert-success">Email sent successfully to ' . htmlspecialchars($to_email) . '!</div>';
        } else {
            $message = '<div class="alert alert-danger">Failed to send email. Please try again later.</div>';
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Send External Email - Bethany Christian Academy</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" rel="stylesheet">
    <style>
        body { background: #f6f8fc; font-family: 'Roboto', Arial, sans-serif; }
        .card { max-width: 700px; margin: 50px auto; box-shadow: 0 4px 20px rgba(0,0,0,0.1); }
        .header { background: #d93025; color: white; border-radius: 0.5rem 0.5rem 0 0; }
    </style>
</head>
<body>
    <div class="container">
        <div class="card">
            <div class="card-header header text-center py-4">
                <h3><i class="fas fa-paper-plane me-2"></i> Send External Email</h3>
                <p class="mb-0">Send emails to parents, guardians, or anyone outside the school system</p>
            </div>
            <div class="card-body p-5">
                <?php echo $message; ?>

                <?php if (!$success): ?>
                <form method="POST">
                    <div class="mb-3">
                        <label class="form-label fw-bold">To</label>
                        <input type="email" name="to_email" class="form-control form-control-lg" placeholder="parent@gmail.com" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label fw-bold">Subject</label>
                        <input type="text" name="subject" class="form-control form-control-lg" required>
                    </div>

                    <div class="mb-4">
                        <label class="form-label fw-bold">Message</label>
                        <textarea name="body" class="form-control" rows="10" required></textarea>
                    </div>

                    <div class="d-grid">
                        <button type="submit" class="btn btn-primary btn-lg">
                            <i class="fas fa-paper-plane me-2"></i> Send Email
                        </button>
                    </div>
                </form>
                <?php else: ?>
                    <div class="text-center py-5">
                        <i class="fas fa-check-circle text-success" style="font-size: 5rem;"></i>
                        <h4 class="mt-3">Email Sent!</h4>
                        <a href="external_mail.php" class="btn btn-primary mt-3">Send Another Email</a>
                        <a href="dashboard.php" class="btn btn-outline-secondary mt-3 ms-2">Back to Mail</a>
                    </div>
                <?php endif; ?>
            </div>
            <div class="card-footer text-center text-muted small">
                Powered by Bethany Christian Academy Mail System
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>