<?php
header('Content-Type: application/json; charset=utf-8');
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost","root","","edencamp_eden_exam"); $mysqli->set_charset("utf8mb4");

$student_id = (int)($_POST['student_id'] ?? 0);
$type       = ($_POST['type'] ?? '');
$label      = trim($_POST['label'] ?? '');
$is_percent = (int)($_POST['is_percent'] ?? 0);
$amount_lkr = (float)($_POST['amount_lkr'] ?? 0);
$amount_usd = (float)($_POST['amount_usd'] ?? 0);
if($student_id<=0 || !in_array($type,['discount','fee']) || $label===''){ echo json_encode(['ok'=>false,'error'=>'Bad input']); exit; }

$mysqli->query("CREATE TABLE IF NOT EXISTS student_adjustments (
  id INT AUTO_INCREMENT PRIMARY KEY,
  student_id INT NOT NULL,
  label VARCHAR(100) NOT NULL,
  type ENUM('discount','fee') NOT NULL,
  is_percent TINYINT(1) DEFAULT 0,
  amount_lkr DECIMAL(10,2) DEFAULT 0,
  amount_usd DECIMAL(10,2) DEFAULT 0,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");

$st = $mysqli->prepare("INSERT INTO student_adjustments
 (student_id,label,type,is_percent,amount_lkr,amount_usd) VALUES (?,?,?,?,?,?)");
$st->bind_param("issidd", $student_id, $label, $type, $is_percent, $amount_lkr, $amount_usd);
$st->execute(); $st->close();

echo json_encode(['ok'=>true]);
