CREATE DATABASE IF NOT EXISTS webgis_poverty;
USE webgis_poverty;

CREATE TABLE `rumah_ibadah` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nama` varchar(255) NOT NULL,
  `jenis` varchar(100) NOT NULL,
  `alamat` text,
  `no_wa` varchar(20),
  `latitude` double NOT NULL,
  `longitude` double NOT NULL,
  `radius_meter` int(11) NOT NULL DEFAULT 0,
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `penduduk_miskin` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nama` varchar(255) NOT NULL,
  `no_wa` varchar(20),
  `nik` varchar(20),
  `alamat` text,
  `jumlah_anggota` int(11) NOT NULL DEFAULT 1,
  `latitude` double NOT NULL,
  `longitude` double NOT NULL,
  `status_proximity` varchar(50),
  `rumah_ibadah_id` int(11),
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`rumah_ibadah_id`) REFERENCES `rumah_ibadah` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `log_bantuan` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `penduduk_miskin_id` int(11) NOT NULL,
  `rumah_ibadah_id` int(11) NOT NULL,
  `tipe_bantuan` varchar(100) NOT NULL,
  `sub_kategori` varchar(100),
  `keterangan` text,
  `tanggal_bantuan` date NOT NULL,
  `nilai_bantuan` double,
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`penduduk_miskin_id`) REFERENCES `penduduk_miskin` (`id`) ON DELETE CASCADE,
  FOREIGN KEY (`rumah_ibadah_id`) REFERENCES `rumah_ibadah` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL UNIQUE,
  `password` varchar(255) NOT NULL,
  `role` enum('admin', 'petugas_lapangan', 'guest') NOT NULL,
  `rumah_ibadah_id` int(11) DEFAULT NULL,
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`rumah_ibadah_id`) REFERENCES `rumah_ibadah` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

