<?php
require_once __DIR__ . '/../config/auth.php';
header('Content-Type: application/json');

if (!isLoggedIn()) { echo json_encode([]); exit; }

$pdo = getDB();
$name = trim($_POST['name'] ?? '');
$phone = trim($_POST['phone'] ?? '');
$email = trim($_POST['email'] ?? '');

if (empty($name) || empty($phone)) {
    echo json_encode(['success' => false, 'message' => 'Name and phone are required.']);
    exit;
}

try {
    $stmt = $pdo->prepare("INSERT INTO customers (name, phone, email) VALUES (?,?,?)");
    $stmt->execute([$name, $phone, $email]);
    $id = $pdo->lastInsertId();
    echo json_encode(['success' => true, 'id' => $id, 'name' => $name, 'phone' => $phone]);
} catch (PDOException $e) {
    echo json_encode(['success' => false, 'message' => 'Error: ' . $e->getMessage()]);
}
