<?php
require_once __DIR__ . '/includes/config.php';

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    header('Location: ' . BASE_URL . '/index.php#consultation');
    exit;
}

$name   = trim($_POST['full_name']    ?? '');
$mobile = trim($_POST['mobile']       ?? '');
$email  = trim($_POST['email']        ?? '');
$country= trim($_POST['country']      ?? '');
$edu    = trim($_POST['education']    ?? '');
$year   = trim($_POST['grad_year']    ?? '');
$gpa    = trim($_POST['gpa']          ?? '');
$eng    = trim($_POST['english_test'] ?? '');
$msg    = trim($_POST['message']      ?? '');

if (!$name || !$mobile) {
    header('Location: ' . BASE_URL . '/index.php#consultation');
    exit;
}

$db = site_db();
if ($db) {
    $db->query("CREATE TABLE IF NOT EXISTS applications (
        id INT AUTO_INCREMENT PRIMARY KEY,
        full_name VARCHAR(100) NOT NULL,
        mobile VARCHAR(30) NOT NULL,
        email VARCHAR(150),
        country VARCHAR(100),
        education VARCHAR(100),
        grad_year VARCHAR(10),
        gpa VARCHAR(20),
        english_test VARCHAR(30),
        message TEXT,
        status ENUM('new','contacted','processing','completed','rejected') DEFAULT 'new',
        is_read TINYINT(1) DEFAULT 0,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    )");
    $st = $db->prepare("INSERT INTO applications (full_name,mobile,email,country,education,grad_year,gpa,english_test,message) VALUES (?,?,?,?,?,?,?,?,?)");
    $st->bind_param('sssssssss', $name,$mobile,$email,$country,$edu,$year,$gpa,$eng,$msg);
    $st->execute();
}

header('Location: ' . BASE_URL . '/index.php?applied=1#consultation');
exit;
