-- Buat database
CREATE DATABASE IF NOT EXISTS webgis_p3 CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE DATABASE IF NOT EXISTS webgis_spbu CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE DATABASE IF NOT EXISTS WEB_GIS CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- Kasih akses user webgis
GRANT ALL PRIVILEGES ON webgis_p3.* TO 'webgis'@'%';
GRANT ALL PRIVILEGES ON webgis_spbu.* TO 'webgis'@'%';
GRANT ALL PRIVILEGES ON WEB_GIS.* TO 'webgis'@'%';
FLUSH PRIVILEGES;

-- Import data webgis_p3
USE webgis_p3;
-- ============================================================
-- DATABASE LENGKAP: webgis_p3
-- WebGIS Poverty Mapping + Sistem Login (3 Role)
-- Import file ini ke Laragon phpMyAdmin
-- ============================================================

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

USE webgis_p3;

-- ── Tabel Data Jalan (Polyline) ────────────────────────────
CREATE TABLE IF NOT EXISTS data_jalan (
    id          INT AUTO_INCREMENT PRIMARY KEY,
    nama_jalan  VARCHAR(200) NOT NULL,
    status      ENUM('Nasional','Provinsi','Kabupaten') NOT NULL DEFAULT 'Kabupaten',
    panjang_m   DOUBLE NOT NULL DEFAULT 0,
    geojson     LONGTEXT NOT NULL,
    created_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ── Tabel Data Parsil Tanah (Polygon) ─────────────────────
CREATE TABLE IF NOT EXISTS data_parsil (
    id          INT AUTO_INCREMENT PRIMARY KEY,
    no_parsil   VARCHAR(100) NOT NULL,
    pemilik     VARCHAR(200) NOT NULL,
    status      ENUM('SHM','HGB','HGU','HP') NOT NULL DEFAULT 'SHM',
    luas_m2     DOUBLE NOT NULL DEFAULT 0,
    geojson     LONGTEXT NOT NULL,
    created_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ── Tabel Data SPBU (Point/Marker) ────────────────────────
CREATE TABLE IF NOT EXISTS data_spbu (
    id          INT AUTO_INCREMENT PRIMARY KEY,
    nama_spbu   VARCHAR(200) NOT NULL,
    no_wa       VARCHAR(20)  NOT NULL,
    buka_24_jam ENUM('Ya','Tidak') NOT NULL DEFAULT 'Tidak',
    latitude    DOUBLE NOT NULL,
    longitude   DOUBLE NOT NULL,
    created_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ── Data Contoh SPBU ──────────────────────────────────────
INSERT INTO data_spbu (nama_spbu, no_wa, buka_24_jam, latitude, longitude) VALUES
('SPBU Jl. Ahmad Yani',       '0812-1111-0001', 'Ya',    -0.0400, 109.3200),
('SPBU Jl. Gajah Mada',       '0812-1111-0002', 'Ya',    -0.0520, 109.3450),
('SPBU Jl. Diponegoro',       '0812-1111-0003', 'Tidak', -0.0610, 109.3600),
('SPBU Jl. Sutan Syahrir',    '0812-1111-0004', 'Tidak', -0.0480, 109.3700),
('SPBU Jl. Kom. Yos Sudarso', '0812-1111-0005', 'Ya',    -0.0350, 109.3550);

-- ── Tabel Data Kemiskinan (Point/Marker) ──────────────────
CREATE TABLE IF NOT EXISTS data_kemiskinan (
    id          INT AUTO_INCREMENT PRIMARY KEY,
    nama_kk     VARCHAR(200) NOT NULL,
    jumlah_kk   INT          NOT NULL DEFAULT 1,
    alamat      TEXT         NOT NULL,
    latitude    DOUBLE       NOT NULL,
    longitude   DOUBLE       NOT NULL,
    created_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ── Tabel Data Rumah Ibadah (Point/Marker + Radius Buffer)
CREATE TABLE IF NOT EXISTS data_masjid (
    id          INT AUTO_INCREMENT PRIMARY KEY,
    nama_masjid VARCHAR(200) NOT NULL,
    jenis       ENUM('Masjid','Mushola','Gereja','Pura','Vihara','Klenteng') NOT NULL DEFAULT 'Masjid',
    pic_masjid  VARCHAR(200) NOT NULL,
    alamat      TEXT         NOT NULL,
    radius_m    INT          NOT NULL DEFAULT 500,
    latitude    DOUBLE       NOT NULL,
    longitude   DOUBLE       NOT NULL,
    created_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ── Tabel Users (Sistem Login 3 Role) ─────────────────────
CREATE TABLE IF NOT EXISTS users (
    id             INT AUTO_INCREMENT PRIMARY KEY,
    username       VARCHAR(100) NOT NULL UNIQUE,
    nama_lengkap   VARCHAR(200) NOT NULL,
    role           ENUM('admin','petugas','pengurus','walikota') NOT NULL,
    password_hash  VARCHAR(255) NOT NULL,
    aktif          TINYINT(1) NOT NULL DEFAULT 1,
    created_at     TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at     TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_role  (role),
    INDEX idx_aktif (aktif)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ── Akun Demo ─────────────────────────────────────────────
-- Password semua akun: "password" (bcrypt via PHP PASSWORD_DEFAULT)
INSERT INTO users (username, nama_lengkap, role, password_hash) VALUES
(
    'admin',
    'Administrator Sistem',
    'admin',
    '$2y$10$mhMxyKlOSSTQxfmWXEncfe1AF56CJ8fb.v0h0CYZ.ELQQd8iP5LrG'
),
(
    'pengurus',
    'Pengurus Rumah Ibadah',
    'pengurus',
    '$2y$10$mhMxyKlOSSTQxfmWXEncfe1AF56CJ8fb.v0h0CYZ.ELQQd8iP5LrG'
),
(
    'walikota',
    'Walikota Pontianak',
    'walikota',
    '$2y$10$mhMxyKlOSSTQxfmWXEncfe1AF56CJ8fb.v0h0CYZ.ELQQd8iP5LrG'
);


-- Import data webgis_spbu
USE webgis_spbu;
-- ============================================================
-- DATABASE: webgis_spbu
-- Pertemuan 3 – Layer Groups and Layers Control
-- Data SPBU dengan 2 Layer Group: Buka 24 Jam & Tidak 24 Jam
-- ============================================================

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

USE webgis_spbu;

-- buka_24_jam: 'Ya'   → Layer Group "SPBU Buka 24 Jam"
--              'Tidak' → Layer Group "SPBU Tidak Buka 24 Jam"
CREATE TABLE IF NOT EXISTS data_spbu (
    id          INT AUTO_INCREMENT PRIMARY KEY,
    nama_spbu   VARCHAR(200) NOT NULL,
    alamat      VARCHAR(300) NOT NULL,
    no_wa       VARCHAR(20)  NOT NULL,
    buka_24_jam ENUM('Ya','Tidak') NOT NULL DEFAULT 'Tidak',
    latitude    DOUBLE NOT NULL,
    longitude   DOUBLE NOT NULL,
    created_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Migrasi: tambah kolom alamat jika tabel lama dari PovertyMap belum punya
-- ALTER TABLE data_spbu ADD COLUMN alamat VARCHAR(300) NOT NULL DEFAULT '' AFTER nama_spbu;
-- UPDATE data_spbu SET alamat = CONCAT(nama_spbu, ', Pontianak') WHERE alamat = '';

INSERT IGNORE INTO data_spbu (nama_spbu, alamat, no_wa, buka_24_jam, latitude, longitude) VALUES
('SPBU Jl. Ahmad Yani',       'Jl. Ahmad Yani No. 12, Pontianak',       '0812-1111-0001', 'Ya',    -0.0400, 109.3200),
('SPBU Jl. Gajah Mada',       'Jl. Gajah Mada No. 45, Pontianak',       '0812-1111-0002', 'Ya',    -0.0520, 109.3450),
('SPBU Jl. Diponegoro',       'Jl. Diponegoro No. 88, Pontianak',       '0812-1111-0003', 'Tidak', -0.0610, 109.3600),
('SPBU Jl. Sutan Syahrir',    'Jl. Sutan Syahrir No. 5, Pontianak',     '0812-1111-0004', 'Tidak', -0.0480, 109.3700),
('SPBU Jl. Kom. Yos Sudarso', 'Jl. Kom. Yos Sudarso No. 22, Pontianak', '0812-1111-0005', 'Ya',    -0.0350, 109.3550),
('SPBU Jl. Tanjung Raya',     'Jl. Tanjung Raya No. 101, Pontianak',   '0812-1111-0006', 'Tidak', -0.0580, 109.3300),
('SPBU Jl. Veteran',          'Jl. Veteran No. 33, Pontianak',          '0812-1111-0007', 'Ya',    -0.0450, 109.3380);

-- Import data WEB_GIS
USE WEB_GIS;
-- =========================================================
-- WebGIS - Struktur Database
-- Tugas: Manajemen Data Jalan & Parsil Tanah (Kavling)
-- Database: WEB_GIS
-- Tipe Geometri: GeoJSON (LineString & Polygon)
-- =========================================================

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

USE WEB_GIS;

-- ---------------------------------------------------------
-- TABEL 1: Manajemen Data Jalan
-- Tipe Geometri : GeoJSON LineString
-- Panjang Jalan : Dihitung otomatis oleh LeafletJS (meter)
-- ---------------------------------------------------------
CREATE TABLE IF NOT EXISTS `tb_jalan` (
  `id`            INT             NOT NULL AUTO_INCREMENT        COMMENT 'Primary key jalan',
  `nama_jalan`    VARCHAR(100)    NOT NULL                       COMMENT 'Nama ruas jalan',
  `status_jalan`  ENUM(
                    'Nasional',
                    'Provinsi',
                    'Kabupaten'
                  )               NOT NULL                       COMMENT 'Status administrasi jalan',
  `panjang_jalan` DECIMAL(12,2)   NOT NULL DEFAULT '0.00'        COMMENT 'Panjang jalan dalam meter (dihitung otomatis LeafletJS via L.LatLng.distanceTo)',
  `geojson`       JSON            NOT NULL                       COMMENT 'Koordinat geometri dalam format GeoJSON LineString',
  `created_at`    TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at`    TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB
  DEFAULT CHARSET=utf8mb4
  COLLATE=utf8mb4_unicode_ci
  COMMENT='Tabel manajemen data jalan berdasarkan status administrasi. Geometri disimpan sebagai GeoJSON LineString.';


-- ---------------------------------------------------------
-- TABEL 2: Manajemen Parsil Tanah / Kavling
-- Tipe Geometri : GeoJSON Polygon
-- Luas Tanah    : Dihitung otomatis oleh LeafletJS (m²)
-- ---------------------------------------------------------
CREATE TABLE IF NOT EXISTS `tb_parsil` (
  `id`                  INT             NOT NULL AUTO_INCREMENT   COMMENT 'Primary key parsil',
  `nama_parsil`         VARCHAR(100)    NOT NULL                  COMMENT 'Nama atau nomor identifikasi parsil tanah',
  `status_kepemilikan`  ENUM(
                          'SHM',
                          'HGB',
                          'HGU',
                          'HP'
                        )               NOT NULL                  COMMENT 'Status sertifikat kepemilikan: SHM=Sertifikat Hak Milik, HGB=Hak Guna Bangunan, HGU=Hak Guna Usaha, HP=Hak Pakai',
  `luas_tanah`          DECIMAL(16,4)   NOT NULL DEFAULT '0.0000' COMMENT 'Luas tanah dalam meter persegi (dihitung otomatis LeafletJS via formula geodesic spherical)',
  `geojson`             JSON            NOT NULL                  COMMENT 'Koordinat geometri dalam format GeoJSON Polygon',
  `created_at`          TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at`          TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB
  DEFAULT CHARSET=utf8mb4
  COLLATE=utf8mb4_unicode_ci
  COMMENT='Tabel manajemen parsil/kavling tanah berdasarkan status kepemilikan. Geometri disimpan sebagai GeoJSON Polygon.';


-- ---------------------------------------------------------
-- Contoh Indeks untuk performa query filter
-- ---------------------------------------------------------
ALTER TABLE `tb_jalan`  ADD INDEX `idx_status_jalan`  (`status_jalan`);
ALTER TABLE `tb_parsil` ADD INDEX `idx_status_parsil` (`status_kepemilikan`);
