<?php
// Seed sample Students and Staff data (idempotent)
header('Content-Type: application/json');
require_once '../../config/config.php';

function findId($pdo, $sql, $params){ $stmt=$pdo->prepare($sql); $stmt->execute($params); $id=$stmt->fetchColumn(); return $id? (int)$id : 0; }

function ensureStaff($pdo, $staff_code, $first, $last, $email, $collegeId=null, $phone='', $role='Internal', $status='Active'){
    $id = findId($pdo, 'SELECT staff_id FROM staff WHERE staff_code=?', [$staff_code]);
    if ($id) return $id;
    $stmt=$pdo->prepare('INSERT INTO staff (college_id, staff_code, first_name, last_name, email, phone, role, status) VALUES (?,?,?,?,?,?,?,?)');
    $stmt->execute([$collegeId?:null,$staff_code,$first,$last,$email,$phone,$role,$status]);
    return (int)$pdo->lastInsertId();
}

function ensureStudent($pdo, $roll, $collegeId, $courseId, $first, $last, $doj, $sem=1, $status='Active'){
    $id = findId($pdo, 'SELECT student_id FROM students WHERE university_rollno=?', [$roll]);
    if ($id) return $id;
    $stmt=$pdo->prepare('INSERT INTO students (university_rollno,college_id,course_id,first_name,last_name,date_of_joining,current_semester,status) VALUES (?,?,?,?,?,?,?,?)');
    $stmt->execute([$roll,$collegeId,$courseId,$first,$last,$doj,$sem,$status]);
    return (int)$pdo->lastInsertId();
}

try{
    // dependencies: academic_structure should already be seeded
    $collegeId = findId($pdo, 'SELECT college_id FROM colleges WHERE college_code=?', ['INE01']);
    if ($collegeId<=0) { http_response_code(400); echo json_encode(['status'=>'error','message'=>'Seed academic_structure first.']); exit; }
    $courseCSE = findId($pdo, 'SELECT course_id FROM courses WHERE course_code=?', ['BE-CSE']);
    $courseME  = findId($pdo, 'SELECT course_id FROM courses WHERE course_code=?', ['BE-ME']);
    if ($courseCSE<=0 || $courseME<=0) { http_response_code(400); echo json_encode(['status'=>'error','message'=>'Required courses not found. Run academic_structure seed.']); exit; }

    $pdo->beginTransaction();

    // Staff
    $staffList = [
        ['STF001','Anita','Sharma','anita.sharma@example.com',$collegeId,'9876543210','Internal','Active'],
        ['STF002','Ravi','Kumar','ravi.kumar@example.com',$collegeId,'9876543211','Invigilator','Active'],
        ['STF003','Priya','Menon','priya.menon@example.com',$collegeId,'9876543212','Valuator','Inactive'],
    ];
    $staffCreated = 0; foreach($staffList as $s){ ensureStaff($pdo,$s[0],$s[1],$s[2],$s[3],$s[4],$s[5],$s[6],$s[7]); $staffCreated++; }

    // Students
    $students = [
        ['U20CSE001',$collegeId,$courseCSE,'Arun','K','2020-08-01',1,'Active'],
        ['U20CSE002',$collegeId,$courseCSE,'Meera','S','2020-08-01',1,'Active'],
        ['U20ME001',$collegeId,$courseME,'Vikram','R','2020-08-01',1,'Active'],
        ['U20ME002',$collegeId,$courseME,'Karthik','S','2020-08-01',1,'Active'],
    ];
    $stdCreated=0; foreach($students as $st){ ensureStudent($pdo,$st[0],$st[1],$st[2],$st[3],$st[4],$st[5],$st[6],$st[7]); $stdCreated++; }

    $pdo->commit();
    echo json_encode(['status'=>'success','message'=>'People seeded','staff'=>$staffCreated,'students'=>$stdCreated]);
}catch(Throwable $e){ if($pdo->inTransaction()) $pdo->rollBack(); http_response_code(500); echo json_encode(['status'=>'error','message'=>$e->getMessage()]); }
?>


