<?php
require_once 'auth/session.php';
require_once 'mail/MailService.php';

$success = '';
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $message = trim($_POST['message'] ?? '');

    if (empty($name) || empty($email) || empty($message)) {
        $error = 'All fields are required.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = 'Invalid email address.';
    } else {
        $res = MailService::sendContactMessage($name, $email, $message);
        if (!empty($res['success'])) {
            $success = 'Your message has been sent successfully!';
        } else {
            $error = 'Failed to send message: ' . ($res['error'] ?? 'Unknown error');
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en" data-theme="dark">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Contact Us - Flatlogic Discord</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="assets/css/discord.css?v=<?php echo time(); ?>">
    <style>
        body {
            background-color: var(--bg-chat);
            color: var(--text-primary);
            display: flex;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
        }
        .contact-card {
            background-color: var(--bg-channels);
            border-radius: 8px;
            padding: 32px;
            width: 100%;
            max-width: 480px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.2);
        }
        .form-control {
            background-color: #1e1f22;
            border: none;
            color: white;
            padding: 10px;
        }
        .form-control:focus {
            background-color: #1e1f22;
            color: white;
            box-shadow: none;
            border: 1px solid var(--blurple);
        }
        .btn-primary {
            background-color: var(--blurple);
            border: none;
            padding: 10px;
            font-weight: bold;
        }
        .btn-primary:hover {
            background-color: #4752c4;
        }
    </style>
</head>
<body>
    <div class="contact-card">
        <h3 class="text-center mb-4">Contact Us</h3>
        
        <?php if ($success): ?>
            <div class="alert alert-success"><?php echo htmlspecialchars($success); ?></div>
            <div class="text-center mt-3">
                <a href="index.php" class="btn btn-link text-white">Back to App</a>
            </div>
        <?php else: ?>
            <?php if ($error): ?>
                <div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
            <?php endif; ?>
            
            <form method="POST">
                <div class="mb-3">
                    <label class="form-label text-uppercase small fw-bold">Name</label>
                    <input type="text" name="name" class="form-control" required value="<?php echo htmlspecialchars($_POST['name'] ?? ''); ?>">
                </div>
                <div class="mb-3">
                    <label class="form-label text-uppercase small fw-bold">Email</label>
                    <input type="email" name="email" class="form-control" required value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>">
                </div>
                <div class="mb-3">
                    <label class="form-label text-uppercase small fw-bold">Message</label>
                    <textarea name="message" class="form-control" rows="5" required><?php echo htmlspecialchars($_POST['message'] ?? ''); ?></textarea>
                </div>
                <button type="submit" class="btn btn-primary w-100 mb-3">Send Message</button>
                <div class="text-center">
                    <a href="index.php" class="btn btn-link text-muted small text-decoration-none">Cancel</a>
                </div>
            </form>
        <?php endif; ?>
        
        <div class="mt-4 p-3 rounded" style="background-color: rgba(255,255,255,0.05); font-size: 0.8em;">
            <p class="mb-0 text-muted">This is for testing purposes only — Flatlogic does not guarantee usage of the mail server. Please set up your own SMTP in .env with our AI Agent.</p>
        </div>
    </div>
</body>
</html>
