<?php
// Run once: creates DB, tables, and demo accounts. Then delete this file.
$host='localhost'; $user='edencamp_eden'; $pass='D#uVBrdpAppH^d,6'; $db='edencamp_edencampus';
$mysqli = new mysqli($host,$user,$pass);
if ($mysqli->connect_errno) die('Connect error: '.$mysqli->connect_error);

// Create DB
$mysqli->query("CREATE DATABASE IF NOT EXISTS `$db` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci");
$mysqli->select_db($db);

// Tables
$queries = [];
$queries[] = "CREATE TABLE IF NOT EXISTS admins (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) UNIQUE,
    password VARCHAR(255)
) ENGINE=InnoDB;";

$queries[] = "CREATE TABLE IF NOT EXISTS students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    nic_number VARCHAR(20) UNIQUE,
    password VARCHAR(255),
    course_name VARCHAR(100),
    unit_exam VARCHAR(100),
    attempts INT DEFAULT 0
) ENGINE=InnoDB;";

$queries[] = "CREATE TABLE IF NOT EXISTS exams (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(150),
    course_name VARCHAR(100),
    unit_exam VARCHAR(100),
    total_questions INT DEFAULT 40
) ENGINE=InnoDB;";

$queries[] = "CREATE TABLE IF NOT EXISTS questions (
    id INT AUTO_INCREMENT PRIMARY KEY,
    exam_id INT NOT NULL,
    question_text TEXT,
    option_a TEXT,
    option_b TEXT,
    option_c TEXT,
    option_d TEXT,
    correct_answer CHAR(1),
    FOREIGN KEY (exam_id) REFERENCES exams(id) ON DELETE CASCADE
) ENGINE=InnoDB;";

$queries[] = "CREATE TABLE IF NOT EXISTS results (
    id INT AUTO_INCREMENT PRIMARY KEY,
    student_id INT,
    exam_id INT,
    score INT,
    grade VARCHAR(5),
    status VARCHAR(10),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (student_id) REFERENCES students(id),
    FOREIGN KEY (exam_id) REFERENCES exams(id)
) ENGINE=InnoDB;";

foreach($queries as $q){
    if (!$mysqli->query($q)){
        echo 'Table error: '.$mysqli->error.'<br>';
    }
}

// Demo admin account
$admin='admin';
$apass = password_hash('admin123', PASSWORD_DEFAULT);
$stmt = $mysqli->prepare('SELECT id FROM admins WHERE username=?');
$stmt->bind_param('s',$admin);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows==0){
    $i = $mysqli->prepare('INSERT INTO admins (username,password) VALUES (?,?)');
    $i->bind_param('ss',$admin,$apass);
    $i->execute();
}
$stmt->close();

// Demo student
$nic='200025525456';
$spass = password_hash('eden123', PASSWORD_DEFAULT);
$stmt = $mysqli->prepare('SELECT id FROM students WHERE nic_number=?');
$stmt->bind_param('s',$nic);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows==0){
    $name='Test Student';
    $course='IT Diploma';
    $unit='Unit 1';
    $ins = $mysqli->prepare('INSERT INTO students (name,nic_number,password,course_name,unit_exam) VALUES (?,?,?,?,?)');
    $ins->bind_param('sssss',$name,$nic,$spass,$course,$unit);
    $ins->execute();
}
$stmt->close();

// Sample exam + questions
$res = $mysqli->query('SELECT id FROM exams LIMIT 1');
if ($res->num_rows == 0){
    $mysqli->query("INSERT INTO exams (title,course_name,unit_exam,total_questions) VALUES ('Sample Exam','IT Diploma','Unit 1',40)");
    $eid = $mysqli->insert_id;
    $qs = [
        ['What is a computer?','A machine','A fruit','A car','A phone','A'],
        ['CPU stands for?','Central Processing Unit','Computer Processing Unit','Central Print Unit','Control Process Unit','A'],
        ['HTML is?','Programming language','Markup language','Database','Operating System','B'],
        ['PHP is mainly used for?','Styling pages','Server-side scripting','Database design','Networking','B'],
        ['Which is a database?','MySQL','Photoshop','Excel (not DB)','Chrome','A'],
        ['What does NIC mean?','National Identity Card','Network Interface Card','New Internet Code','None','A']
    ];
    $ins = $mysqli->prepare('INSERT INTO questions (exam_id,question_text,option_a,option_b,option_c,option_d,correct_answer) VALUES (?,?,?,?,?,?,?)');
    foreach($qs as $q){
        $a=$q[0]; $b=$q[1]; $c=$q[2]; $d=$q[3]; $corr=$q[4];
        $ins->bind_param('issssss',$eid,$a,$b,$c,$d,$corr);
        $ins->execute();
    }
}

echo '<h2>Setup complete.</h2><p>Admin: admin / admin123</p><p>Student NIC: 200025525456 / eden123</p><p>Delete setup.php now.</p>';
?>