<?php
header('Content-Type: application/json; charset=utf-8');
error_reporting(E_ALL); ini_set('display_errors', 1);

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['success' => false, 'message' => 'Invalid request']);
    exit;
}

$name = isset($_POST['name']) ? trim($_POST['name']) : '';
$password = isset($_POST['password']) ? trim($_POST['password']) : '';
$email = isset($_POST['email']) ? trim($_POST['email']) : '';
$age = isset($_POST['age']) ? trim($_POST['age']) : '';
$phone = isset($_POST['phone']) ? trim($_POST['phone']) : '';
$gender = isset($_POST['gender']) ? trim($_POST['gender']) : '';

if ($name === '' || $password === '' || $email === '' || $age === '' || $phone === '' || $gender === '') {
    echo json_encode(['success' => false, 'message' => 'Please fill in all fields']);
    exit;
}

// Validate password strength: min 8 chars, must contain letters, numbers, and symbols
if (strlen($password) < 8) {
    echo json_encode(['success' => false, 'message' => 'Password must be at least 8 characters long']);
    exit;
}
if (!preg_match('/[a-zA-Z]/', $password)) {
    echo json_encode(['success' => false, 'message' => 'Password must contain at least one letter']);
    exit;
}
if (!preg_match('/[0-9]/', $password)) {
    echo json_encode(['success' => false, 'message' => 'Password must contain at least one number']);
    exit;
}
if (!preg_match('/[!@#$%^&*()_+\-=\[\]{};:\'",.<>?\/\\|`~]/', $password)) {
    echo json_encode(['success' => false, 'message' => 'Password must contain at least one special character (!@#$%^&* etc)']);
    exit;
}

require_once __DIR__ . '/db_config.php';

$conn = $conn;  // Already set by db_config.php
if ($conn->connect_error) {
    echo json_encode(['success' => false, 'message' => 'DB connection error']);
    exit;
}
$conn->set_charset('utf8mb4');

// Check for conflicts
$checkStmt = $conn->prepare("SELECT `name`,`email`,`phone` FROM `login-t` WHERE `name` = ? OR `email` = ? OR `phone` = ?");
if (!$checkStmt) {
    echo json_encode(['success' => false, 'message' => 'DB prepare error']);
    exit;
}
$checkStmt->bind_param('sss', $name, $email, $phone);
$checkStmt->execute();
$res = $checkStmt->get_result();

$conflicts = [];
while ($row = $res->fetch_assoc()) {
    if (strcasecmp($row['name'], $name) === 0) $conflicts[] = 'name';
    if (strcasecmp($row['email'], $email) === 0) $conflicts[] = 'email';
    if (strcasecmp($row['phone'], $phone) === 0) $conflicts[] = 'phone';
}
$checkStmt->close();

if (!empty($conflicts)) {
    echo json_encode(['success' => false, 'message' => 'conflict', 'conflicts' => array_values(array_unique($conflicts))]);
    $conn->close();
    exit;
}

// Insert (plain-text as per current app behavior)
$insertStmt = $conn->prepare("INSERT INTO `login-t` (`name`,`password`,`email`,`age`,`phone`,`gender`,`dt`) VALUES (?, ?, ?, ?, ?, ?, NOW())");
if (!$insertStmt) {
    echo json_encode(['success' => false, 'message' => 'DB prepare error']);
    $conn->close();
    exit;
}
$ageInt = (int)$age;
$insertStmt->bind_param('sssiss', $name, $password, $email, $ageInt, $phone, $gender);
$executed = $insertStmt->execute();
if ($executed) {
    $insertId = $conn->insert_id;
    echo json_encode(['success' => true, 'redirect' => 'landing.php', 'id' => $insertId]);
} else {
    echo json_encode(['success' => false, 'message' => 'insert_failed', 'error' => $insertStmt->error]);
}
$insertStmt->close();
$conn->close();
