SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

DROP TABLE IF EXISTS pasien_order;
DROP TABLE IF EXISTS register_ambulans;
DROP TABLE IF EXISTS pegawai;
DROP TABLE IF EXISTS pasien;
DROP TABLE IF EXISTS users;

CREATE TABLE users (
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  nama VARCHAR(150) NOT NULL,
  username VARCHAR(80) NOT NULL UNIQUE,
  password VARCHAR(255) NOT NULL,
  role VARCHAR(50) NOT NULL DEFAULT 'user',
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

CREATE TABLE pasien (
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  rm VARCHAR(50) NULL,
  atribut VARCHAR(30) NULL,
  nama VARCHAR(150) NOT NULL,
  tgl_lahir DATE NULL,
  gender VARCHAR(30) NULL,
  alamat TEXT NULL,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  KEY idx_pasien_rm (rm),
  KEY idx_pasien_nama (nama)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

CREATE TABLE pegawai (
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  nama VARCHAR(150) NOT NULL,
  jabatan VARCHAR(100) NULL,
  unit VARCHAR(100) NULL,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  KEY idx_pegawai_nama (nama),
  KEY idx_pegawai_jabatan (jabatan)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

CREATE TABLE register_ambulans (
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  rm VARCHAR(50) NULL,
  register VARCHAR(80) NOT NULL UNIQUE,
  order_type ENUM('True Emergency','Non Emergency','Pending Order') NOT NULL DEFAULT 'Pending Order',
  status VARCHAR(50) NOT NULL DEFAULT 'Predispatch',
  fase TINYINT UNSIGNED NOT NULL DEFAULT 1,
  prioritas VARCHAR(50) NULL,
  nama_penelpon VARCHAR(150) NULL,
  no_penelpon VARCHAR(50) NULL,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  KEY idx_register_rm (rm),
  KEY idx_register_fase (fase),
  KEY idx_register_order_type (order_type),
  CONSTRAINT fk_register_pasien_rm FOREIGN KEY (rm) REFERENCES pasien(rm) ON UPDATE CASCADE ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

CREATE TABLE pasien_order (
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  register_id INT UNSIGNED NOT NULL,
  nama_penelpon VARCHAR(150) NULL,
  no_penelpon VARCHAR(50) NULL,
  kesadaran VARCHAR(100) NULL,
  kategori VARCHAR(100) NULL,
  pernafasan JSON NULL,
  posisi JSON NULL,
  keterbatasan VARCHAR(100) NULL,
  nyeri JSON NULL,
  suhu VARCHAR(50) NULL,
  bahaya JSON NULL,
  berat_badan VARCHAR(50) NULL,
  keluhan_utama TEXT NULL,
  tujuan VARCHAR(150) NULL,
  tujuan_detail TEXT NULL,
  persetujuan VARCHAR(100) NULL,
  biaya VARCHAR(100) NULL,
  validasi VARCHAR(100) NULL,
  petugas_id INT UNSIGNED NULL,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  KEY idx_order_register (register_id),
  KEY idx_order_petugas (petugas_id),
  CONSTRAINT fk_order_register FOREIGN KEY (register_id) REFERENCES register_ambulans(id) ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT fk_order_petugas FOREIGN KEY (petugas_id) REFERENCES pegawai(id) ON UPDATE CASCADE ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

INSERT INTO users (nama, username, password, role) VALUES
('Administrator 119', 'admin119', '$2y$12$snjqy2Ep5eBKNDaRQPdTNOWM5Bjht7UECBRbaY6mFaQu.G1phJmwK', 'admin'),
('Leader 119', 'leader119', '$2y$12$QGTVOQRFP8xScmpeNutadOWDIS6EEMfL9hXuyKC7PWmbiV9SAnSEK', 'leader'),
('Perawat 119', 'perawat119', '$2y$12$rTHZKpqq9qOrydr981uqvOmBu/OeIxUUrrhSyF/pXf2mGd9tzRObq', 'perawat'),
('Driver 119', 'driver119', '$2y$12$FDJ8hS2bUqqxpuo69YGlOO1icl.6CCHr7NLlEOe1BtOW7q.TsTApy', 'driver');

INSERT INTO pegawai (nama, jabatan, unit) VALUES
('Danang Guntoro S.Kep. Ns', 'Sub Ur Ruang', 'Ambulans 119'),
('Brian Septa SP AMd.Kep', 'PJ Tim', 'Ambulans 119'),
('Aguswanto AMd.Kep', 'Ka Jaga', 'Ambulans 119'),
('Bhaktiar Rahyung Dwi A S.Kep. Ners', 'Ka Jaga', 'Ambulans 119'),
('Lega Ulmani S.Kep. Ners', 'Ka Jaga', 'Ambulans 119'),
('Nadhira Wahyu Lestari S.Kep. Ners', 'Ka Jaga', 'Ambulans 119'),
('M. Sudrajat P AMd.Kep', 'Ka Jaga', 'Ambulans 119'),
('Wiwik Miftahul Jannah S.Kep. Ners', 'Perawat', 'Ambulans 119'),
('Dody Dwi Laksono AMd.Kep', 'Perawat', 'Ambulans 119'),
('Faisal Anam AMd.Kep', 'Perawat', 'Ambulans 119'),
('Uswatun Hasanah S.Kep. Ners', 'Perawat', 'Ambulans 119'),
('Yahya Citra Winata', 'Pengemudi', 'Ambulans 119'),
('Widodo', 'Pengemudi', 'Ambulans 119'),
('Ichwan Prasetyo', 'Pengemudi', 'Ambulans 119'),
('Ahwan Patty', 'Pengemudi', 'Ambulans 119'),
('Muhamad Dedi Trianto', 'Pengemudi', 'Ambulans 119');

INSERT INTO pasien (rm, atribut, nama, tgl_lahir, gender, alamat) VALUES
('000001', 'Tn.', 'Budi Santoso', '1988-02-14', 'Laki-laki', 'Jl. Simulasi No. 1, Malang'),
('000002', 'Ny.', 'Siti Aminah', '1979-08-20', 'Perempuan', 'Jl. Contoh No. 2, Malang');

INSERT INTO register_ambulans (rm, register, order_type, status, fase, prioritas, nama_penelpon, no_penelpon, created_at) VALUES
('000001', 'AMB-260505-001', 'True Emergency', 'Predispatch', 1, 'Tinggi', 'Andi', '081234567890', NOW()),
('000002', 'AMB-260505-002', 'Non Emergency', 'Diproses', 3, 'Sedang', 'Rina', '081298765432', NOW());

INSERT INTO pasien_order (
  register_id, nama_penelpon, no_penelpon, kesadaran, kategori, pernafasan, posisi,
  keterbatasan, nyeri, suhu, bahaya, berat_badan, keluhan_utama, tujuan,
  tujuan_detail, persetujuan, biaya, validasi, petugas_id
) VALUES
(1, 'Andi', '081234567890', 'Sadar', 'Dewasa', JSON_OBJECT('sesak','Ya','batuk','Tidak'), JSON_OBJECT('lokasi', JSON_ARRAY('ruang_tamu'), 'lainnya', NULL),
 'Tidak', JSON_OBJECT('status','Ya','lokasi', JSON_ARRAY('Dada'), 'lain', NULL), '37.8', JSON_OBJECT('status','Tidak','detail',NULL), '70',
 'Nyeri dada dan sesak napas sejak 30 menit sebelum panggilan.', 'IGD RSSA', 'Rujuk ke IGD', 'Setuju', 'Umum', 'Valid', 8),
(2, 'Rina', '081298765432', 'Sadar', 'Dewasa', JSON_OBJECT('sesak','Tidak','batuk','Tidak'), JSON_OBJECT('lokasi', JSON_ARRAY('kamar_tidur'), 'lainnya', NULL),
 'Tidak', JSON_OBJECT('status','Tidak','lokasi', JSON_ARRAY(), 'lain', NULL), '36.7', JSON_OBJECT('status','Tidak','detail',NULL), '55',
 'Kontrol pasien non-emergency dengan kebutuhan transport ambulans.', 'Poliklinik RSSA', 'Transport pasien', 'Setuju', 'BPJS', 'Valid', 12);

SET FOREIGN_KEY_CHECKS = 1;
