-- ============================================================
-- SINERGI - Sistem Informasi Geografis Penanganan Kemiskinan
-- Database Schema v1.0
-- Kota Pontianak
-- ============================================================

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

CREATE DATABASE IF NOT EXISTS `sinergi_db`
  CHARACTER SET utf8mb4
  COLLATE utf8mb4_unicode_ci;

USE `sinergi_db`;

-- ============================================================
-- TABLE: roles
-- ============================================================
CREATE TABLE `roles` (
  `id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `nama` VARCHAR(50) NOT NULL,
  `slug` VARCHAR(30) NOT NULL,
  `deskripsi` VARCHAR(255),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_roles_slug` (`slug`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `roles` (`id`,`nama`,`slug`,`deskripsi`) VALUES
(1,'Super Admin','superadmin','Administrator sistem, mengelola seluruh data master'),
(2,'Verifikator Kelurahan','verifikator','Petugas verifikasi laporan di tingkat kelurahan'),
(3,'Pengurus Rumah Ibadah','rumah_ibadah','Pengurus rumah ibadah yang menangani warga binaan'),
(4,'Dinas Sosial','dinsos','Petugas Dinas Sosial untuk monitoring kemiskinan'),
(5,'Wali Kota','walikota','Wali Kota untuk dashboard eksekutif');

-- ============================================================
-- TABLE: kecamatan
-- ============================================================
CREATE TABLE `kecamatan` (
  `id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `nama` VARCHAR(100) NOT NULL,
  `kode` VARCHAR(20),
  `lat` DECIMAL(10,7),
  `lng` DECIMAL(10,7),
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 6 Kecamatan Kota Pontianak
INSERT INTO `kecamatan` (`id`,`nama`,`kode`,`lat`,`lng`) VALUES
(1,'Pontianak Kota','62.71.01',-0.0233,109.3294),
(2,'Pontianak Barat','62.71.02',-0.0500,109.3100),
(3,'Pontianak Selatan','62.71.03',-0.0700,109.3400),
(4,'Pontianak Tenggara','62.71.04',-0.0900,109.3600),
(5,'Pontianak Timur','62.71.05',-0.0400,109.3700),
(6,'Pontianak Utara','62.71.06',0.0100,109.3300);

-- ============================================================
-- TABLE: kelurahan
-- ============================================================
CREATE TABLE `kelurahan` (
  `id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `id_kecamatan` SMALLINT UNSIGNED NOT NULL,
  `nama` VARCHAR(100) NOT NULL,
  `kode` VARCHAR(20),
  `lat` DECIMAL(10,7),
  `lng` DECIMAL(10,7),
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `fk_kel_kec` (`id_kecamatan`),
  CONSTRAINT `fk_kel_kec` FOREIGN KEY (`id_kecamatan`) REFERENCES `kecamatan` (`id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Kelurahan Kota Pontianak (sample per kecamatan)
INSERT INTO `kelurahan` (`id`,`id_kecamatan`,`nama`,`kode`,`lat`,`lng`) VALUES
-- Pontianak Kota
(1,1,'Darat Sekip','62.71.01.1001',-0.0175,109.3230),
(2,1,'Sungai Bangkong','62.71.01.1002',-0.0290,109.3310),
(3,1,'Tengah','62.71.01.1003',-0.0210,109.3290),
(4,1,'Mariana','62.71.01.1004',-0.0155,109.3350),
-- Pontianak Barat
(5,2,'Sungai Jawi Dalam','62.71.02.1001',-0.0467,109.3050),
(6,2,'Sungai Jawi Luar','62.71.02.1002',-0.0530,109.3000),
(7,2,'Pal Lima','62.71.02.1003',-0.0600,109.2900),
(8,2,'Sungai Beliung','62.71.02.1004',-0.0450,109.3150),
-- Pontianak Selatan
(9,3,'Akcaya','62.71.03.1001',-0.0650,109.3380),
(10,3,'Bangka Belitung Darat','62.71.03.1002',-0.0750,109.3420),
(11,3,'Bangka Belitung Laut','62.71.03.1003',-0.0820,109.3350),
(12,3,'Parit Tokaya','62.71.03.1004',-0.0700,109.3300),
-- Pontianak Tenggara
(13,4,'Bansir Darat','62.71.04.1001',-0.0870,109.3550),
(14,4,'Bansir Laut','62.71.04.1002',-0.0950,109.3600),
(15,4,'Tanjung Hulu','62.71.04.1003',-0.0920,109.3700),
(16,4,'Tanjung Hilir','62.71.04.1004',-0.0850,109.3650),
-- Pontianak Timur
(17,5,'Saigon','62.71.05.1001',-0.0380,109.3700),
(18,5,'Tambelan Sampit','62.71.05.1002',-0.0450,109.3750),
(19,5,'Tanjung Raya','62.71.05.1003',-0.0320,109.3800),
(20,5,'Parit Mayor','62.71.05.1004',-0.0500,109.3680),
-- Pontianak Utara
(21,6,'Siantan Hulu','62.71.06.1001',0.0200,109.3250),
(22,6,'Siantan Tengah','62.71.06.1002',0.0100,109.3200),
(23,6,'Siantan Hilir','62.71.06.1003',0.0050,109.3300),
(24,6,'Batu Layang','62.71.06.1004',0.0300,109.3350);

-- ============================================================
-- TABLE: users
-- ============================================================
CREATE TABLE `users` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `id_role` TINYINT UNSIGNED NOT NULL,
  `id_kelurahan` SMALLINT UNSIGNED DEFAULT NULL,
  `nama` VARCHAR(100) NOT NULL,
  `username` VARCHAR(50) NOT NULL,
  `email` VARCHAR(100) NOT NULL,
  `password` VARCHAR(255) NOT NULL,
  `no_hp` VARCHAR(20),
  `foto` VARCHAR(255),
  `is_active` TINYINT(1) NOT NULL DEFAULT 1,
  `last_login` DATETIME,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_users_username` (`username`),
  UNIQUE KEY `uq_users_email` (`email`),
  KEY `fk_users_role` (`id_role`),
  KEY `fk_users_kel` (`id_kelurahan`),
  CONSTRAINT `fk_users_role` FOREIGN KEY (`id_role`) REFERENCES `roles` (`id`) ON DELETE RESTRICT,
  CONSTRAINT `fk_users_kel` FOREIGN KEY (`id_kelurahan`) REFERENCES `kelurahan` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Password: Admin@123 (bcrypt)
INSERT INTO `users` (`id`,`id_role`,`id_kelurahan`,`nama`,`username`,`email`,`password`,`no_hp`,`is_active`) VALUES
(1,1,NULL,'Super Administrator','superadmin','superadmin@sinergi.id','$2y$12$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi','081200000001',1),
(2,2,1,'Budi Santoso','verif_darat','budi.santoso@pontianak.go.id','$2y$12$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi','081200000002',1),
(3,2,5,'Sari Dewi','verif_jawi','sari.dewi@pontianak.go.id','$2y$12$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi','081200000003',1),
(4,4,NULL,'Ahmad Fauzi','dinsos_ahmad','ahmad.fauzi@dinsos.id','$2y$12$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi','081200000004',1),
(5,5,NULL,'H. Edi Kamtono','walikota','walikota@pontianak.go.id','$2y$12$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi','081200000005',1);

-- ============================================================
-- TABLE: jenis_rumah_ibadah
-- ============================================================
CREATE TABLE `jenis_rumah_ibadah` (
  `id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `nama` VARCHAR(50) NOT NULL,
  `ikon` VARCHAR(100),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `jenis_rumah_ibadah` VALUES
(1,'Masjid','masjid.png'),
(2,'Gereja','gereja.png'),
(3,'Vihara','vihara.png'),
(4,'Pura','pura.png'),
(5,'Klenteng','klenteng.png');

-- ============================================================
-- TABLE: rumah_ibadah
-- ============================================================
CREATE TABLE `rumah_ibadah` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `id_user` INT UNSIGNED DEFAULT NULL COMMENT 'User pengurus',
  `id_kelurahan` SMALLINT UNSIGNED NOT NULL,
  `id_jenis` TINYINT UNSIGNED NOT NULL,
  `nama` VARCHAR(150) NOT NULL,
  `alamat` TEXT,
  `lat` DECIMAL(10,7) NOT NULL,
  `lng` DECIMAL(10,7) NOT NULL,
  `radius_meter` INT UNSIGNED NOT NULL DEFAULT 500 COMMENT 'Radius pelayanan dalam meter',
  `no_hp_pengurus` VARCHAR(20),
  `nama_ketua` VARCHAR(100),
  `foto` VARCHAR(255),
  `is_active` TINYINT(1) NOT NULL DEFAULT 1,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `fk_ri_user` (`id_user`),
  KEY `fk_ri_kel` (`id_kelurahan`),
  KEY `fk_ri_jenis` (`id_jenis`),
  CONSTRAINT `fk_ri_user` FOREIGN KEY (`id_user`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `fk_ri_kel` FOREIGN KEY (`id_kelurahan`) REFERENCES `kelurahan` (`id`) ON DELETE RESTRICT,
  CONSTRAINT `fk_ri_jenis` FOREIGN KEY (`id_jenis`) REFERENCES `jenis_rumah_ibadah` (`id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ============================================================
-- TABLE: kategori_bantuan
-- ============================================================
CREATE TABLE `kategori_bantuan` (
  `id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `nama` VARCHAR(100) NOT NULL,
  `deskripsi` TEXT,
  `satuan` VARCHAR(50) COMMENT 'kg, paket, rupiah, dll',
  `is_active` TINYINT(1) NOT NULL DEFAULT 1,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `kategori_bantuan` (`id`,`nama`,`deskripsi`,`satuan`) VALUES
(1,'Sembako','Bantuan bahan pokok makanan','paket'),
(2,'Sandang','Bantuan pakaian dan kebutuhan sandang','paket'),
(3,'Uang Tunai','Bantuan uang tunai langsung','rupiah'),
(4,'Obat-obatan','Bantuan obat dan layanan kesehatan','paket'),
(5,'Peralatan Sekolah','Bantuan perlengkapan pendidikan anak','paket'),
(6,'Renovasi Rumah','Bantuan material renovasi rumah tidak layak huni','paket'),
(7,'Modal Usaha','Bantuan modal untuk usaha kecil','rupiah'),
(8,'Gas & Listrik','Bantuan biaya gas dan listrik','paket');

-- ============================================================
-- TABLE: laporan_masyarakat
-- ============================================================
CREATE TABLE `laporan_masyarakat` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `kode_laporan` VARCHAR(20) NOT NULL UNIQUE COMMENT 'LPR-YYYYMMDD-XXXX',
  `nama_pelapor` VARCHAR(100) NOT NULL,
  `no_hp_pelapor` VARCHAR(20),
  `nama_warga` VARCHAR(100) NOT NULL,
  `alamat_warga` TEXT NOT NULL,
  `id_kelurahan` SMALLINT UNSIGNED NOT NULL,
  `lat` DECIMAL(10,7) NOT NULL,
  `lng` DECIMAL(10,7) NOT NULL,
  `deskripsi` TEXT,
  `foto` VARCHAR(255),
  `status` ENUM('menunggu','diproses','terverifikasi','ditolak') NOT NULL DEFAULT 'menunggu',
  `catatan_verifikator` TEXT,
  `id_verifikator` INT UNSIGNED DEFAULT NULL,
  `tanggal_verifikasi` DATETIME,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `fk_lap_kel` (`id_kelurahan`),
  KEY `fk_lap_verif` (`id_verifikator`),
  CONSTRAINT `fk_lap_kel` FOREIGN KEY (`id_kelurahan`) REFERENCES `kelurahan` (`id`) ON DELETE RESTRICT,
  CONSTRAINT `fk_lap_verif` FOREIGN KEY (`id_verifikator`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ============================================================
-- TABLE: warga_miskin
-- ============================================================
CREATE TABLE `warga_miskin` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `id_laporan` INT UNSIGNED DEFAULT NULL,
  `id_kelurahan` SMALLINT UNSIGNED NOT NULL,
  `id_rumah_ibadah` INT UNSIGNED DEFAULT NULL COMMENT 'Rumah ibadah penanggung jawab',
  `id_verifikator` INT UNSIGNED DEFAULT NULL,
  -- Data Pribadi
  `nik` VARCHAR(16) UNIQUE,
  `nama` VARCHAR(100) NOT NULL,
  `tempat_lahir` VARCHAR(100),
  `tanggal_lahir` DATE,
  `jenis_kelamin` ENUM('L','P') DEFAULT 'L',
  `agama` ENUM('Islam','Kristen','Katolik','Hindu','Buddha','Konghucu','Lainnya') DEFAULT 'Islam',
  `alamat` TEXT NOT NULL,
  `lat` DECIMAL(10,7) NOT NULL,
  `lng` DECIMAL(10,7) NOT NULL,
  `no_hp` VARCHAR(20),
  `foto` VARCHAR(255),
  -- Data Sosial Ekonomi
  `pendidikan_terakhir` ENUM('Tidak Sekolah','SD','SMP','SMA','D3','S1','S2','S3') DEFAULT 'SD',
  `pekerjaan` VARCHAR(100),
  `penghasilan_bulanan` INT UNSIGNED DEFAULT 0 COMMENT 'Rupiah per bulan',
  `jumlah_tanggungan` TINYINT UNSIGNED DEFAULT 0,
  -- Kondisi Khusus
  `kondisi_rumah` ENUM('Sangat Tidak Layak','Tidak Layak','Cukup Layak','Layak') DEFAULT 'Tidak Layak',
  `riwayat_penyakit` TEXT COMMENT 'Daftar penyakit yang diderita',
  `is_lansia` TINYINT(1) DEFAULT 0,
  `is_disabilitas` TINYINT(1) DEFAULT 0,
  `disabilitas_jenis` VARCHAR(100),
  -- Status & Prioritas
  `status_kemiskinan` ENUM('Miskin Ekstrem','Miskin','Rentan Miskin') DEFAULT 'Miskin',
  `skor_prioritas` TINYINT UNSIGNED DEFAULT 0 COMMENT '0-100',
  `level_prioritas` ENUM('Tinggi','Sedang','Rendah') DEFAULT 'Sedang',
  `is_blind_spot` TINYINT(1) DEFAULT 0,
  `status` ENUM('aktif','nonaktif','meninggal','pindah') DEFAULT 'aktif',
  -- Timestamps
  `tanggal_verifikasi` DATE,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `fk_wm_laporan` (`id_laporan`),
  KEY `fk_wm_kel` (`id_kelurahan`),
  KEY `fk_wm_ri` (`id_rumah_ibadah`),
  KEY `fk_wm_verif` (`id_verifikator`),
  KEY `idx_wm_blind_spot` (`is_blind_spot`),
  KEY `idx_wm_prioritas` (`level_prioritas`),
  KEY `idx_wm_status_kem` (`status_kemiskinan`),
  CONSTRAINT `fk_wm_laporan` FOREIGN KEY (`id_laporan`) REFERENCES `laporan_masyarakat` (`id`) ON DELETE SET NULL,
  CONSTRAINT `fk_wm_kel` FOREIGN KEY (`id_kelurahan`) REFERENCES `kelurahan` (`id`) ON DELETE RESTRICT,
  CONSTRAINT `fk_wm_ri` FOREIGN KEY (`id_rumah_ibadah`) REFERENCES `rumah_ibadah` (`id`) ON DELETE SET NULL,
  CONSTRAINT `fk_wm_verif` FOREIGN KEY (`id_verifikator`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ============================================================
-- TABLE: histori_bantuan
-- ============================================================
CREATE TABLE `histori_bantuan` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `id_warga` INT UNSIGNED NOT NULL,
  `id_rumah_ibadah` INT UNSIGNED NOT NULL,
  `id_kategori_bantuan` TINYINT UNSIGNED NOT NULL,
  `id_user_pengurus` INT UNSIGNED NOT NULL,
  `kode_bantuan` VARCHAR(20) NOT NULL UNIQUE COMMENT 'BNT-YYYYMMDD-XXXX',
  `tanggal_penyaluran` DATE NOT NULL,
  `jumlah` DECIMAL(12,2),
  `satuan` VARCHAR(50),
  `deskripsi` TEXT,
  `foto_bukti` VARCHAR(255),
  `status` ENUM('rencana','disalurkan','dibatalkan') DEFAULT 'disalurkan',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `fk_hb_warga` (`id_warga`),
  KEY `fk_hb_ri` (`id_rumah_ibadah`),
  KEY `fk_hb_kat` (`id_kategori_bantuan`),
  KEY `fk_hb_user` (`id_user_pengurus`),
  CONSTRAINT `fk_hb_warga` FOREIGN KEY (`id_warga`) REFERENCES `warga_miskin` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_hb_ri` FOREIGN KEY (`id_rumah_ibadah`) REFERENCES `rumah_ibadah` (`id`) ON DELETE RESTRICT,
  CONSTRAINT `fk_hb_kat` FOREIGN KEY (`id_kategori_bantuan`) REFERENCES `kategori_bantuan` (`id`) ON DELETE RESTRICT,
  CONSTRAINT `fk_hb_user` FOREIGN KEY (`id_user_pengurus`) REFERENCES `users` (`id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ============================================================
-- TABLE: aktivitas_sistem
-- ============================================================
CREATE TABLE `aktivitas_sistem` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `id_user` INT UNSIGNED DEFAULT NULL,
  `nama_user` VARCHAR(100) COMMENT 'Snapshot nama saat aksi',
  `role` VARCHAR(30),
  `aksi` VARCHAR(100) NOT NULL,
  `modul` VARCHAR(50),
  `deskripsi` TEXT,
  `ip_address` VARCHAR(45),
  `user_agent` VARCHAR(255),
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `fk_as_user` (`id_user`),
  KEY `idx_as_modul` (`modul`),
  KEY `idx_as_created` (`created_at`),
  CONSTRAINT `fk_as_user` FOREIGN KEY (`id_user`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ============================================================
-- TABLE: sessions (untuk autentikasi manual)
-- ============================================================
CREATE TABLE `sessions` (
  `id` VARCHAR(128) NOT NULL,
  `id_user` INT UNSIGNED NOT NULL,
  `ip_address` VARCHAR(45),
  `user_agent` VARCHAR(255),
  `payload` TEXT,
  `last_activity` INT UNSIGNED NOT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_sess_user` (`id_user`),
  CONSTRAINT `fk_sess_user` FOREIGN KEY (`id_user`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

SET FOREIGN_KEY_CHECKS = 1;
