CREATE DATABASE rfid_enterprise;
USE rfid_enterprise;

CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
nama VARCHAR(100),
username VARCHAR(50) UNIQUE,
password VARCHAR(255),
role ENUM('admin','operator','kepsek') DEFAULT 'admin',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE siswa (
id INT AUTO_INCREMENT PRIMARY KEY,
nisn VARCHAR(20) UNIQUE,
nama VARCHAR(100),
kelas VARCHAR(20),
kode_rfid VARCHAR(50) UNIQUE,
foto VARCHAR(255) NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE kegiatan (
id INT AUTO_INCREMENT PRIMARY KEY,
nama_kegiatan VARCHAR(100),
tanggal DATE,
status ENUM('aktif','nonaktif') DEFAULT 'nonaktif',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE presensi (
id INT AUTO_INCREMENT PRIMARY KEY,
siswa_id INT,
kegiatan_id INT,
waktu DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (siswa_id) REFERENCES siswa(id) ON DELETE CASCADE,
FOREIGN KEY (kegiatan_id) REFERENCES kegiatan(id) ON DELETE CASCADE
);

INSERT INTO users (nama,username,password,role) VALUES
('Administrator','admin','$2y$10$wH8sRZtE1gJfXHcLkP2E8e3FJYqG7vYx9vX5kLZpZxYlR1mW3V2cW','admin');
