<?php
session_start();

$step = isset($_GET['step']) ? (int)$_GET['step'] : 1;
$error = '';
$success = '';

$configFile = __DIR__ . '/config.php';
$envFile = __DIR__ . '/../.env';

if ($step === 2 && $_SERVER['REQUEST_METHOD'] === 'POST') {
    $transport = $_POST['transport'] ?? 'smtp';
    $host = $_POST['host'] ?? '';
    $port = $_POST['port'] ?? '587';
    $secure = $_POST['secure'] ?? 'tls';
    $user = $_POST['user'] ?? '';
    $pass = $_POST['pass'] ?? '';
    $from = $_POST['from'] ?? '';
    $from_name = $_POST['from_name'] ?? '';

    $envContent = "MAIL_TRANSPORT=$transport\n";
    $envContent .= "SMTP_HOST=$host\n";
    $envContent .= "SMTP_PORT=$port\n";
    $envContent .= "SMTP_SECURE=$secure\n";
    $envContent .= "SMTP_USER=$user\n";
    $envContent .= "SMTP_PASS=$pass\n";
    $envContent .= "MAIL_FROM=$from\n";
    $envContent .= "MAIL_FROM_NAME=$from_name\n";

    if (file_put_contents($envFile, $envContent)) {
        header('Location: ' . $_SERVER['SCRIPT_NAME'] . '?step=3');
        exit;
    } else {
        $error = 'Failed to write .env file. Check permissions.';
    }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Mail Service Installation</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container mt-5">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card shadow">
                <div class="card-header bg-primary text-white">
                    <h3 class="mb-0">Mail Service Installation - Step <?= $step ?></h3>
                </div>
                <div class="card-body">
                    <?php if ($error): ?>
                        <div class="alert alert-danger"><?= $error ?></div>
                    <?php endif; ?>

                    <?php if ($step === 1): ?>
                        <h5>Welcome to Mail Service Setup</h5>
                        <p>This wizard will help you configure your SMTP settings.</p>
                        <a href="<?= htmlspecialchars($_SERVER['SCRIPT_NAME']) ?>?step=2" class="btn btn-primary">Start Configuration</a>

                    <?php elseif ($step === 2): ?>
                        <form method="POST" action="<?= htmlspecialchars($_SERVER['SCRIPT_NAME']) ?>?step=2">
                            <div class="mb-3">
                                <label class="form-label">Transport</label>
                                <select name="transport" class="form-select">
                                    <option value="smtp">SMTP</option>
                                    <option value="sendmail">Sendmail</option>
                                </select>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">SMTP Host</label>
                                <input type="text" name="host" class="form-control" required>
                            </div>
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label class="form-label">SMTP Port</label>
                                    <input type="text" name="port" class="form-control" value="587" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label class="form-label">Encryption</label>
                                    <select name="secure" class="form-select">
                                        <option value="tls">TLS</option>
                                        <option value="ssl">SSL</option>
                                        <option value="">None</option>
                                    </select>
                                </div>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">SMTP Username</label>
                                <input type="text" name="user" class="form-control" required>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">SMTP Password</label>
                                <input type="password" name="pass" class="form-control" required>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">From Email</label>
                                <input type="email" name="from" class="form-control" required>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">From Name</label>
                                <input type="text" name="from_name" class="form-control" required>
                            </div>
                            <button type="submit" class="btn btn-primary">Save Settings</button>
                        </form>

                    <?php elseif ($step === 3): ?>
                        <div class="alert alert-success">
                            Configuration saved successfully!
                        </div>
                        <p>The mail service is now ready to use.</p>
                        <a href="../login.php" class="btn btn-success">Go to Login</a>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
