'Invalid request method'], 405); exit; } if (!validateApiKey()) { logWebhook('bookings', file_get_contents('php://input'), 401); sendJsonResponse(['error' => 'Unauthorized'], 401); exit; } $request_body = file_get_contents('php://input'); $data = json_decode($request_body, true); if (json_last_error() !== JSON_ERROR_NONE) { logWebhook('bookings', $request_body, 400); sendJsonResponse(['error' => 'Invalid JSON'], 400); exit; } $errors = []; if (empty($data['record_type'])) { $errors[] = 'record_type is required'; } if (empty($data['customer_name'])) { $errors[] = 'customer_name is required'; } if (empty($data['customer_phone'])) { $errors[] = 'customer_phone is required'; } if (!empty($errors)) { logWebhook('bookings', $request_body, 422); sendJsonResponse(['errors' => $errors], 422); exit; } try { $stmt = db()->prepare("INSERT INTO bookings (record_type, customer_name, customer_phone, service_address, service_category, service_type, system_type, urgency_level, issue_description, appointment_date, appointment_time, status, estimated_revenue, actual_revenue, booked_by, customer_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([ $data['record_type'], $data['customer_name'], $data['customer_phone'], $data['service_address'] ?? null, $data['service_category'] ?? null, $data['service_type'] ?? null, $data['system_type'] ?? null, $data['urgency_level'] ?? null, $data['issue_description'] ?? null, $data['appointment_date'] ?? null, $data['appointment_time'] ?? null, $data['status'] ?? 'new', $data['estimated_revenue'] ?? null, $data['actual_revenue'] ?? null, $data['booked_by'] ?? 'online', $data['customer_id'] ?? null ]); $new_id = db()->lastInsertId(); logWebhook('bookings', $request_body, 201); sendJsonResponse(['success' => true, 'id' => $new_id, 'message' => 'Booking created'], 201); } catch (PDOException $e) { error_log($e->getMessage()); logWebhook('bookings', $request_body, 500); sendJsonResponse(['error' => 'Database error'], 500); }