-- =====================
-- DATABASE
-- =====================
DROP DATABASE IF EXISTS database_monitoring;
CREATE DATABASE database_monitoring;
USE database_monitoring;

-- =====================
-- USERS
-- =====================
CREATE TABLE users (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100) UNIQUE,
    password VARCHAR(255),
    role ENUM('super_admin','admin','user') DEFAULT 'user',
    created_at TIMESTAMP NULL,
    updated_at TIMESTAMP NULL
);

-- =====================
-- PEGAWAI
-- =====================
DROP TABLE pegawai;

CREATE TABLE pegawai (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    nip VARCHAR(50) UNIQUE,
    nama VARCHAR(100),
    jabatan VARCHAR(100),
    user_id BIGINT NULL,
    created_at TIMESTAMP NULL,
    updated_at TIMESTAMP NULL
);

-- =====================
-- PENUGASAN
-- =====================
CREATE TABLE penugasan (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    nomor_surat VARCHAR(100),
    kegiatan VARCHAR(255),
    tanggal_mulai DATE,
    tanggal_selesai DATE,
    status ENUM('on_progress','selesai','telah_ditinjau') DEFAULT 'on_progress',
    created_by BIGINT,
    approved_by BIGINT NULL,
    approved_at TIMESTAMP NULL,
    created_at TIMESTAMP NULL,
    updated_at TIMESTAMP NULL,
    FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (approved_by) REFERENCES users(id) ON DELETE SET NULL
);

-- =====================
-- RELASI MANY TO MANY
-- =====================
CREATE TABLE penugasan_pegawai (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    penugasan_id BIGINT,
    pegawai_id BIGINT,

    FOREIGN KEY (penugasan_id) REFERENCES penugasan(id) ON DELETE CASCADE,
    FOREIGN KEY (pegawai_id) REFERENCES pegawai(id) ON DELETE CASCADE,

    UNIQUE (penugasan_id, pegawai_id)
);

INSERT INTO users (name, email, password, role)
VALUES (
'ERIKA FITRANOVA, SH.,M.A.P',
'superadmin@mail.com',
'123456',
'super_admin'
);


INSERT INTO users (name, email, password, role) VALUES
('SUTOWO, SE, M.Si', 'admin1@mail.com', '123456', 'admin'),
('WELPRIDA IRANI, SH, M.Si', 'admin2@mail.com', '123456', 'admin');


INSERT INTO pegawai (nip, nama, jabatan) VALUES
('P001', 'HARRI MULIAWAN, ST', 'Pengawas Ketenagakerjaan Ahli Muda'),
('P002', 'DANA OKTAVIAN, S.Sos', 'Pengawas Ketenagakerjaan Ahli Muda'),
('P003', 'LISBET MARYATI S, SH', 'Pengawas Ketenagakerjaan Ahli Pertama'),
('P004', 'MAISA BAHARI, SH', 'Pengawas Ketenagakerjaan Ahli Pertama'),
('P005', 'URAY NANDA KHARISMA, SE', 'Pengawas Ketenagakerjaan Ahli Pertama');

DROP DATABASE IF EXISTS database_monitoring;
CREATE DATABASE database_monitoring;
USE database_monitoring;
RENAME TABLE pegawais TO pegawai;
SELECT * FROM pegawai;
SELECT * FROM users;
