����JFIF��`�`�����<CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), quality = 100
���C����C����d�d"��������������
����JFIF��`�`�����<CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), quality = 100
���C����C����d�d"��������������
����JFIF��`�`�����<CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), quality = 100
���C����C����d�d"��������������
����JFIF��`�`�����<CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), quality = 100
���C����C����d�d"��������������
<?php
// api/register.php
require_once 'db_connect.php';
header('Content-Type: application/json');
require_once 'mailer.php';

$input = json_decode(file_get_contents('php://input'), true);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $type = $input['type'] ?? 'interest';
    $name = trim($input['name'] ?? '');
    $phone_raw = $input['phone'] ?? '';
    $email = trim($input['email'] ?? '');
    $city = trim($input['city'] ?? '');

    // 1. Phone Sanitization: Remove any non-digits
    $phone = preg_replace('/[^0-9]/', '', $phone_raw);

    // 2. Mandatory Checks
    if (empty($name) || empty($phone) || empty($city)) {
        http_response_code(400);
        echo json_encode(['success' => false, 'error' => 'All basic fields (Name, Phone, City) are mandatory.']);
        exit;
    }

    if (strlen($phone) !== 10) {
        http_response_code(400);
        echo json_encode(['success' => false, 'error' => 'Phone number must be exactly 10 digits.']);
        exit;
    }

    $success = false;
    $details = [];

    try {
        if ($type === 'interest') {
            $stmt = $pdo->prepare("INSERT INTO interests (name, phone, email, city) VALUES (?, ?, ?, ?)");
            $success = $stmt->execute([$name, $phone, $email ?: null, $city]);
            if ($success) {
                $newId = $pdo->lastInsertId();
                $pdo->query("UPDATE interests SET uid = $newId WHERE id = $newId");
            }
        } else {
            $bike = trim($input['bike'] ?? '');
            $blood = trim($input['blood'] ?? '');
            $tshirt = trim($input['tshirt'] ?? '');
            $em_name = trim($input['em_name'] ?? '');
            $em_phone_raw = $input['em_phone'] ?? '';
            $experience = trim($input['experience'] ?? '');
            $medical = trim($input['medical'] ?? '');

            // Sanitizing emergency phone too
            $em_phone = preg_replace('/[^0-9]/', '', $em_phone_raw);

            // Mandatory Checks for Full Registration
            if (empty($bike) || empty($blood) || empty($tshirt) || empty($em_name) || empty($em_phone) || empty($experience)) {
                http_response_code(400);
                echo json_encode(['success' => false, 'error' => 'All registration fields except Medical Notes are mandatory.']);
                exit;
            }

            if (strlen($em_phone) !== 10) {
                http_response_code(400);
                echo json_encode(['success' => false, 'error' => 'Emergency contact number must be exactly 10 digits.']);
                exit;
            }

            $details = [
                'bike' => $bike,
                'blood' => $blood,
                'tshirt' => $tshirt,
                'experience' => $experience
            ];

            // Use the updated schema columns: em_name, em_phone, experience
            $stmt = $pdo->prepare("INSERT INTO registrations (name, phone, email, city, bike, blood_group, tshirt_size, em_name, em_phone, experience, medical_notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            $success = $stmt->execute([$name, $phone, $email ?: null, $city, $bike, $blood, $tshirt, $em_name, $em_phone, $experience, $medical]);
            if ($success) {
                $newId = $pdo->lastInsertId();
                $pdo->query("UPDATE registrations SET uid = $newId WHERE id = $newId");
            }
        }

        if ($success && !empty($email)) {
            // Attempt to send email, but don't let a mail failure break the entire response
            try {
                $mail_status = send_registration_email($email, $name, $type, $details);
                if ($mail_status !== true) {
                    error_log("Registration mail status for $email: " . print_r($mail_status, true));
                }
            } catch (Exception $e) {
                error_log("Registration mail fatal error for $email: " . $e->getMessage());
            }
        }
        
        echo json_encode(['success' => true, 'message' => 'Submission successful']);
    } catch (PDOException $e) {
        if ($e->getCode() == 23000) { // Unique constraint violation
            http_response_code(409);
            echo json_encode(['success' => false, 'error' => 'A rider with this Email or Phone is already registered.']);
        } else {
            error_log("Registration error: " . $e->getMessage());
            http_response_code(500);
            echo json_encode(['success' => false, 'error' => 'Server error, please try again later.']);
        }
    } catch (Exception $e) {
        error_log("Registration error: " . $e->getMessage());
        http_response_code(500);
        echo json_encode(['success' => false, 'error' => $e->getMessage()]);
    }
}
?>



