<?php
declare(strict_types=1);
session_start();
require_once '../config.php';
header('Content-Type: application/json');
if (!isset($_SESSION['user_id'])) { http_response_code(401); echo json_encode(['success'=>false]); exit; }
$conn = getDBConnection();
if (!$conn instanceof mysqli) { echo json_encode(['success'=>false]); exit; }
$parentId = (int) ($_POST['parent_id'] ?? 0);
$studentId = (int) ($_POST['student_id'] ?? 0);
$remove = (int) ($_POST['remove'] ?? 0);
if ($remove === 1) {
    $stmt = $conn->prepare("DELETE FROM parent_student_link WHERE parent_id = ? AND student_id = ?");
} else {
    $stmt = $conn->prepare("INSERT IGNORE INTO parent_student_link (parent_id, student_id) VALUES (?, ?)");
}
$stmt->bind_param('ii', $parentId, $studentId);
$stmt->execute();
$stmt->close();
echo json_encode(['success'=>true]);
