<?php
require_once '../../config/config.php';
header('Content-Type: application/json');

try {
    $halls = $pdo->query("SELECT hall_id, hall_name, capacity FROM halls ORDER BY hall_name")->fetchAll(PDO::FETCH_ASSOC);
    $created = 0; $skipped = 0;
    $ins = $pdo->prepare('INSERT INTO hall_settings (hall_id, rows_count, columns_count, seating_method, bench_capacity, max_students, row_labeling, internal_invigilators_required, external_invigilators_required) VALUES (?,?,?,?,?,?,?,?,?)');
    foreach ($halls as $h) {
        $exists = $pdo->prepare('SELECT hall_setting_id FROM hall_settings WHERE hall_id=?');
        $exists->execute([$h['hall_id']]);
        if ($exists->fetchColumn()) { $skipped++; continue; }
        $capacity = (int)($h['capacity'] ?? 0);
        $rows = max(4, min(12, (int)floor(($capacity ?: 60)/10)));
        $cols = max(4, min(12, (int)ceil(($capacity ?: 60)/$rows/2)));
        $benchCap = 2;
        $maxStud = $capacity ?: ($rows*$cols*$benchCap);
        $ins->execute([$h['hall_id'],$rows,$cols,'Parallel',$benchCap,$maxStud,'Numeric',1,0]);
        $created++;
    }
    echo json_encode(['status'=>'success','message'=>'Hall settings seeded','created'=>$created,'skipped'=>$skipped]);
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode(['status'=>'error','message'=>$e->getMessage()]);
}
?>


