<?php
session_start();

require_once 'php.php';

$error = "";

if (
    empty($_SESSION['signup_full_name']) ||
    empty($_SESSION['signup_email']) ||
    empty($_SESSION['signup_password'])
) {
    header("Location: SS_Signup_pg1.php");
    exit();
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $income_frequency = $_POST['income_frequency'] ?? "";
    $annual_salary    = $_POST['annual_salary'] ?? "";

    $full_name = $_SESSION['signup_full_name'] ?? "";
    $email     = $_SESSION['signup_email'] ?? "";
    $password  = $_SESSION['signup_password'] ?? "";

    $Q1 = $_SESSION['signup_Q1'] ?? "";
    $Q2 = $_SESSION['signup_Q2'] ?? "";
    $Q3 = $_SESSION['signup_Q3'] ?? "";
    $Q4 = $_SESSION['signup_Q4'] ?? "";

    if ($income_frequency === "" || $annual_salary === "") {
        $error = "Please fill in both income frequency and annual salary.";
    } elseif (
        $full_name === "" || $email === "" || $password === "" ||
        $Q1 === "" || $Q2 === "" || $Q3 === "" || $Q4 === ""
    ) {
        $error = "Some signup information is missing. Please restart the signup (pg1 → pg2 → pg3 → pg4).";
    } else {
        
        // Hash password and security questions
        $password_hash      = password_hash($password, PASSWORD_DEFAULT);
        $SQ_oldest_sibling  = password_hash($Q1, PASSWORD_DEFAULT);
        $SQ_city            = password_hash($Q2, PASSWORD_DEFAULT);
        $SQ_dream_job       = password_hash($Q3, PASSWORD_DEFAULT);
        $SQ_first_job_title = password_hash($Q4, PASSWORD_DEFAULT);

        $sql = "
            INSERT INTO users
            (name, email, password,
             SQ_oldest_sibling, SQ_city, SQ_dream_job, SQ_first_job_title,
             income_frequency, annual_salary)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
        ";

        $stmt = $conn->prepare($sql);

        if (!$stmt) {
            die(
                "<h3>SQL prepare failed</h3>" .
                "<p><strong>MySQL error:</strong> " . htmlspecialchars($conn->error) . "</p>" .
                "<pre>" . htmlspecialchars($sql) . "</pre>"
            );
        }

        $annual_salary_float = (float)$annual_salary;

        $stmt->bind_param(
            "ssssssssd",
            $full_name,
            $email,
            $password_hash,
            $SQ_oldest_sibling,
            $SQ_city,
            $SQ_dream_job,
            $SQ_first_job_title,
            $income_frequency,
            $annual_salary_float
        );

        if ($stmt->execute()) {
            $new_user_id = $conn->insert_id;

            $_SESSION['user_id'] = $new_user_id;
            $_SESSION['name']    = $full_name;
            $_SESSION['email']   = $email;

            unset(
                $_SESSION['signup_full_name'],
                $_SESSION['signup_email'],
                $_SESSION['signup_password'],
                $_SESSION['signup_Q1'],
                $_SESSION['signup_Q2'],
                $_SESSION['signup_Q3'],
                $_SESSION['signup_Q4']
            );

            $stmt->close();

            header("Location: SS_dashboard.php");
            exit();
        } else {
            $error = "Insert failed: " . $stmt->error;
            $stmt->close();
        }
    }
}
?>
<!DOCTYPE html>
<html>
<head>

<meta charset="UTF-8">
<title>Sign Up - Step 4</title>
<link rel="stylesheet" href="styles.css">

</head>
<body>
<main class="signup-main container">
    <h1>Last step: tell us about your income.</h1>

    <?php if ($error): ?>
        <p class="error-message"><strong><?php echo htmlspecialchars($error); ?></strong></p>
    <?php endif; ?>

    <form class="signup-form" action="SS_Signup_pg4.php" method="post">
    <label>How often do you receive income?</label><br>
    <select class="signup-form" name="income_frequency" required>
            <option value="">--Select--</option>
            <option value="monthly">Monthly</option>
            <option value="biweekly">Bi Weekly</option>
            <option value="weekly">Weekly</option>
            <option value="irregular">Irregular</option>
     </select>
        <br><br>

    <label>What is your annual salary?
        <input type="number" step="0.01" name="annual_salary" required>
    </label>
    <br><br>

        <button class="btn" type="submit">Finish Sign Up</button>
    </form>
</main>
</body>
</html>
