CREATE DATABASE IF NOT EXISTS webgis;
USE webgis;

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 `data_jalan` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nama_jalan` varchar(255) NOT NULL,
  `status_jalan` varchar(100) NOT NULL,
  `panjang_meter` double NOT NULL,
  `keterangan` text,
  `geojson` longtext NOT NULL,
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `data_parsil` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nama_pemilik` varchar(255) NOT NULL,
  `no_sertifikat` varchar(100),
  `jenis_hak` varchar(100) NOT NULL,
  `luas_m2` double NOT NULL,
  `keterangan` text,
  `geojson` longtext NOT NULL,
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

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