-- ============================================================
-- Schema Database WebGIS SPBU Pontianak
-- Jalankan sekali untuk membuat database & tabel secara manual
-- (Opsional — server.js sudah auto-create tabel saat start)
-- ============================================================

CREATE DATABASE IF NOT EXISTS spbu_pontianak
  CHARACTER SET utf8mb4
  COLLATE utf8mb4_unicode_ci;

USE spbu_pontianak;

-- ── Tabel SPBU ──────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS spbu (
  id               INT AUTO_INCREMENT PRIMARY KEY,
  kode             VARCHAR(20)  NOT NULL UNIQUE          COMMENT 'Kode unik SPBU, mis: 64.781.01',
  nama             VARCHAR(100) NOT NULL,
  alamat           TEXT         NOT NULL,
  lat              DOUBLE       NOT NULL                  COMMENT 'Latitude (desimal)',
  lng              DOUBLE       NOT NULL                  COMMENT 'Longitude (desimal)',
  jam_buka         TIME         NOT NULL DEFAULT '06:00'  COMMENT 'Format HH:MM',
  jam_tutup        TIME         NOT NULL DEFAULT '22:00'  COMMENT 'Format HH:MM',
  jenis_bbm        JSON         NOT NULL                  COMMENT 'Array jenis BBM, contoh: ["Pertalite","Pertamax"]',
  manual_override  ENUM('auto','open','closed') NOT NULL DEFAULT 'auto',
  aktif            TINYINT(1)   NOT NULL DEFAULT 1        COMMENT '1=aktif, 0=dihapus (soft delete)',
  created_at       TIMESTAMP    DEFAULT CURRENT_TIMESTAMP,
  updated_at       TIMESTAMP    DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ── Tabel Jalan ─────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS jalan (
  id           INT AUTO_INCREMENT PRIMARY KEY,
  nama_jalan   VARCHAR(150) NOT NULL                    COMMENT 'Nama jalan, mis: Jl. Ahmad Yani',
  jenis_jalan  ENUM('nasional','provinsi','kota_kabupaten') NOT NULL DEFAULT 'kota_kabupaten'
               COMMENT 'Klasifikasi jalan',
  koordinat    LONGTEXT     NOT NULL                    COMMENT 'GeoJSON LineString koordinat polyline',
  aktif        TINYINT(1)   NOT NULL DEFAULT 1          COMMENT '1=aktif, 0=dihapus (soft delete)',
  created_at   TIMESTAMP    DEFAULT CURRENT_TIMESTAMP,
  updated_at   TIMESTAMP    DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ── Tabel Bangunan ──────────────────────────────────────────
CREATE TABLE IF NOT EXISTS bangunan (
  id              INT AUTO_INCREMENT PRIMARY KEY,
  nama_bangunan   VARCHAR(150) NOT NULL                 COMMENT 'Nama bangunan atau kavling',
  jenis_bangunan  ENUM('SHM','HGB','HGU','HP','HPL')   NOT NULL DEFAULT 'SHM'
                  COMMENT 'SHM=Sertifikat Hak Milik, HGB=Hak Guna Bangunan, HGU=Hak Guna Usaha, HP=Hak Pakai, HPL=Hak Pengelolaan',
  nama_jalan      VARCHAR(150)                          COMMENT 'Nama jalan lokasi bangunan',
  koordinat       LONGTEXT     NOT NULL                 COMMENT 'GeoJSON Polygon koordinat area bangunan',
  aktif           TINYINT(1)   NOT NULL DEFAULT 1       COMMENT '1=aktif, 0=dihapus (soft delete)',
  created_at      TIMESTAMP    DEFAULT CURRENT_TIMESTAMP,
  updated_at      TIMESTAMP    DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ── Tabel Poverty Mapping ───────────────────────────────────────────────
-- Catatan: project ini memakai MySQL/MariaDB. Kolom geom dibuat kompatibel
-- dengan MySQL spatial dan query radius memakai ST_Distance_Sphere.

CREATE TABLE IF NOT EXISTS tempat_ibadah (
  id          INT AUTO_INCREMENT PRIMARY KEY,
  nama        VARCHAR(255) NOT NULL,
  jenis       ENUM('masjid','gereja','pura','vihara','kelenteng','lainnya') NOT NULL,
  alamat      TEXT,
  kelurahan   VARCHAR(100),
  lat         DOUBLE NOT NULL,
  lng         DOUBLE NOT NULL,
  geom        POINT GENERATED ALWAYS AS (ST_SRID(POINT(lng, lat), 4326)) STORED,
  created_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  SPATIAL INDEX idx_tempat_ibadah_geom (geom)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS keluarga_miskin (
  id              INT AUTO_INCREMENT PRIMARY KEY,
  nama_kk         VARCHAR(255) NOT NULL COMMENT 'Nama Kepala Keluarga',
  jumlah_anggota  INT NOT NULL DEFAULT 1,
  kategori        ENUM('sangat_miskin','miskin','rentan_miskin') NOT NULL,
  alamat          TEXT,
  rt              VARCHAR(5),
  rw              VARCHAR(5),
  kelurahan       VARCHAR(100),
  keterangan      TEXT,
  lat             DOUBLE NOT NULL,
  lng             DOUBLE NOT NULL,
  geom            POINT GENERATED ALWAYS AS (ST_SRID(POINT(lng, lat), 4326)) STORED,
  created_at      TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at      TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  SPATIAL INDEX idx_keluarga_geom (geom)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO tempat_ibadah (nama, jenis, alamat, kelurahan, lat, lng) VALUES
('Masjid Jami Sultan Syarif Abdurrahman', 'masjid', 'Jl. Tanjungpura', 'Dalam Bugis', -0.0226, 109.3316),
('Gereja Bethel Indonesia', 'gereja', 'Jl. Gajahmada', 'Sungai Bangkong', -0.0334, 109.3253),
('Vihara Bodhisattva', 'vihara', 'Jl. Diponegoro', 'Mariana', -0.0289, 109.3401);

INSERT INTO keluarga_miskin (nama_kk, jumlah_anggota, kategori, kelurahan, lat, lng) VALUES
('Ahmad Fauzi', 5, 'sangat_miskin', 'Dalam Bugis', -0.0231, 109.3321),
('Siti Rahmah', 3, 'miskin', 'Dalam Bugis', -0.0219, 109.3308),
('Budi Santoso', 6, 'rentan_miskin', 'Sungai Bangkong', -0.0341, 109.3261),
('Maria Goretti', 4, 'miskin', 'Mariana', -0.0295, 109.3412),
('Lim Ah Kow', 2, 'sangat_miskin', 'Mariana', -0.0501, 109.3600);
