-- ============================================
-- DATABASE: pemetaan_kemiskinan
-- DIBUAT UNTUK: Sistem Pemetaan Kemiskinan
-- ============================================

-- CREATE DATABASE
CREATE DATABASE IF NOT EXISTS `pemetaan_kemiskinan` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE `pemetaan_kemiskinan`;

-- ============================================
-- TABLE 1: masyarakat_miskin
-- ============================================
CREATE TABLE IF NOT EXISTS `masyarakat_miskin` (
  `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `nama` varchar(100) NOT NULL,
  `tanggal_lahir` date NOT NULL,
  `jenis_kelamin` enum('Laki-laki','Perempuan') NOT NULL,
  `nik` varchar(20),
  `alamat` text NOT NULL,
  `latitude` decimal(10,8) NOT NULL,
  `longitude` decimal(11,8) NOT NULL,
  `status_bpjs` enum('Ya','Tidak','Proses') DEFAULT 'Tidak',
  `no_hp` varchar(15),
  `pekerjaan` varchar(100),
  `pendapatan_bulanan` int(11),
  `jumlah_tanggungan` int(11),
  `status_rumah` enum('Milik Sendiri','Sewa','Menumpang') DEFAULT 'Menumpang',
  `kelurahan` varchar(100),
  `kecamatan` varchar(100),
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX `idx_nama` (`nama`),
  INDEX `idx_status_bpjs` (`status_bpjs`),
  INDEX `idx_kecamatan` (`kecamatan`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- ============================================
-- TABLE 2: rumah_ibadah
-- ============================================
CREATE TABLE IF NOT EXISTS `rumah_ibadah` (
  `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `nama` varchar(100) NOT NULL,
  `jenis` enum('Masjid','Gereja','Vihara','Kelenteng','Sinagog','Lain') NOT NULL,
  `latitude` decimal(10,8) NOT NULL,
  `longitude` decimal(11,8) NOT NULL,
  `alamat` text NOT NULL,
  `nama_pemimpin` varchar(100),
  `no_hp_pemimpin` varchar(15),
  `kapasitas` int(11),
  `tahun_didirikan` year,
  `keterangan` text,
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX `idx_nama` (`nama`),
  INDEX `idx_jenis` (`jenis`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- ============================================
-- TABLE 3: bantuan_riwayat
-- ============================================
CREATE TABLE IF NOT EXISTS `bantuan_riwayat` (
  `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `id_masyarakat` int(11) NOT NULL,
  `jenis_bantuan` varchar(100) NOT NULL,
  `tanggal_bantuan` date NOT NULL,
  `nilai` int(11) NOT NULL,
  `satuan` varchar(50),
  `keterangan` text,
  `nama_pemberi` varchar(100),
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (`id_masyarakat`) REFERENCES `masyarakat_miskin`(`id`) ON DELETE CASCADE,
  INDEX `idx_id_masyarakat` (`id_masyarakat`),
  INDEX `idx_tanggal` (`tanggal_bantuan`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- ============================================
-- TABLE 4: bpjs_record
-- ============================================
CREATE TABLE IF NOT EXISTS `bpjs_record` (
  `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `id_masyarakat` int(11) NOT NULL,
  `no_peserta` varchar(20) NOT NULL UNIQUE,
  `jenis_bpjs` enum('Kesehatan','Ketenagakerjaan','Jaminan Hari Tua') DEFAULT 'Kesehatan',
  `tingkat` enum('Penerima Bantuan Iuran (PBI)','Pekerja Penerima Upah','Pekerja Bukan Penerima Upah','Peserta Mandiri') DEFAULT 'Penerima Bantuan Iuran (PBI)',
  `tanggal_daftar` date NOT NULL,
  `status_aktif` enum('Aktif','Nonaktif','Suspended') DEFAULT 'Aktif',
  `faskes_tingkat_1` varchar(100),
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (`id_masyarakat`) REFERENCES `masyarakat_miskin`(`id`) ON DELETE CASCADE,
  INDEX `idx_id_masyarakat` (`id_masyarakat`),
  INDEX `idx_no_peserta` (`no_peserta`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- ============================================
-- TABLE 5: users (untuk login admin)
-- ============================================
CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `username` varchar(50) NOT NULL UNIQUE,
  `password` varchar(255) NOT NULL,
  `nama_lengkap` varchar(100) NOT NULL,
  `role` enum('Admin','Surveyor','Viewer') DEFAULT 'Surveyor',
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- Insert default admin user (password: admin123)
INSERT INTO `users` (`username`, `password`, `nama_lengkap`, `role`) VALUES 
('admin', '5f4dcc3b5aa765d61d8327deb882cf99', 'Administrator', 'Admin');

-- ============================================
-- TABLE 6: fasilitas_jalan
-- ============================================
CREATE TABLE IF NOT EXISTS `fasilitas_jalan` (
  `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `nama` varchar(150) NOT NULL,
  `geojson` text NOT NULL,
  `tipe` varchar(50),
  `keterangan` text,
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- ============================================
-- TABLE 7: fasilitas_polygon
-- ============================================
CREATE TABLE IF NOT EXISTS `fasilitas_polygon` (
  `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `nama` varchar(150) NOT NULL,
  `geojson` text NOT NULL,
  `tipe` varchar(50),
  `keterangan` text,
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- ============================================
-- TABLE 8: fasilitas_spbu
-- ============================================
CREATE TABLE IF NOT EXISTS `fasilitas_spbu` (
  `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `nama` varchar(150) NOT NULL,
  `latitude` decimal(10,8) NOT NULL,
  `longitude` decimal(11,8) NOT NULL,
  `alamat` text,
  `is_24_jam` enum('Ya','Tidak') DEFAULT 'Tidak',
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- ============================================
-- SAMPLE DATA (Opsional - untuk testing)
-- ============================================

-- Sample Masyarakat Miskin
INSERT INTO `masyarakat_miskin` (`nama`, `tanggal_lahir`, `jenis_kelamin`, `nik`, `alamat`, `latitude`, `longitude`, `status_bpjs`, `no_hp`, `pekerjaan`, `pendapatan_bulanan`, `jumlah_tanggungan`, `status_rumah`, `kelurahan`, `kecamatan`) VALUES
('Siti Nur Azizah', '1985-03-15', 'Perempuan', '6101234567890123', 'Jl. Mahakam No. 25', -0.0612, 109.3445, 'Ya', '081234567890', 'Buruh Harian', 1500000, 4, 'Menumpang', 'Sungai Jawi', 'Pontianak Kota');

-- Sample Rumah Ibadah
INSERT INTO `rumah_ibadah` (`nama`, `jenis`, `latitude`, `longitude`, `alamat`, `nama_pemimpin`, `no_hp_pemimpin`, `kapasitas`, `tahun_didirikan`) VALUES
('Masjid Al-Hidayah', 'Masjid', -0.0615, 109.3450, 'Jl. Diponegoro No. 45', 'Ustaz Ahmad', '081234567890', 500, 2005),
('Gereja Hati Kudus', 'Gereja', -0.0610, 109.3440, 'Jl. Jend Sudirman No. 12', 'Pdt. Bonaventura', '081234567891', 300, 1995);

-- ============================================
-- SELESAI
-- ============================================
