-- ================================================================
-- DATABASE: db_webgis  (versi hosting — tanpa CREATE DATABASE/USE)
-- WebGIS SIG B - Tugas + UAS
-- Athallah Ghathfan Aqila | D1041231060 | Untan
-- Import file ini ke database yang sudah dibuat di InfinityFree
-- ================================================================

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ================================================================
-- BAGIAN TUGAS
-- ================================================================

CREATE TABLE IF NOT EXISTS spbu (
    id           INT AUTO_INCREMENT PRIMARY KEY,
    nama         VARCHAR(255) NOT NULL,
    nomor        VARCHAR(50),
    status_24jam ENUM('Ya','Tidak') DEFAULT 'Tidak',
    lat          DECIMAL(10,7) NOT NULL,
    lng          DECIMAL(10,7) NOT NULL,
    created_at   TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS jalan (
    id           INT AUTO_INCREMENT PRIMARY KEY,
    nama_jalan   VARCHAR(255) NOT NULL,
    status_jalan ENUM('Nasional','Provinsi','Kabupaten') DEFAULT 'Kabupaten',
    panjang_m    DECIMAL(10,2),
    geojson      TEXT NOT NULL,
    created_at   TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS persil (
    id           INT AUTO_INCREMENT PRIMARY KEY,
    nomor_persil VARCHAR(100) NOT NULL,
    status_milik ENUM('SHM','HGB','HGU','HP') DEFAULT 'SHM',
    luas_m2      DECIMAL(12,2),
    geojson      TEXT NOT NULL,
    created_at   TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS masjid (
    id          INT AUTO_INCREMENT PRIMARY KEY,
    nama_masjid VARCHAR(255) NOT NULL,
    pic         VARCHAR(100),
    alamat      TEXT,
    radius_m    INT DEFAULT 300,
    lat         DECIMAL(10,7) NOT NULL,
    lng         DECIMAL(10,7) NOT NULL,
    created_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS kemiskinan (
    id         INT AUTO_INCREMENT PRIMARY KEY,
    nama_kk    VARCHAR(255) NOT NULL,
    jumlah_kk  INT DEFAULT 1,
    alamat     TEXT,
    lat        DECIMAL(10,7) NOT NULL,
    lng        DECIMAL(10,7) NOT NULL,
    pkh        TINYINT(1) NOT NULL DEFAULT 0,
    bpnt       TINYINT(1) NOT NULL DEFAULT 0,
    blt        TINYINT(1) NOT NULL DEFAULT 0,
    foto       VARCHAR(255),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS wilayah (
    id         INT AUTO_INCREMENT PRIMARY KEY,
    nama       VARCHAR(255) NOT NULL,
    tipe       ENUM('RT','RW','Kelurahan','Kecamatan') DEFAULT 'RT',
    geojson    TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

-- ================================================================
-- BAGIAN UAS
-- ================================================================

CREATE TABLE IF NOT EXISTS users (
    id         INT AUTO_INCREMENT PRIMARY KEY,
    username   VARCHAR(50) UNIQUE NOT NULL,
    password   VARCHAR(255) NOT NULL,
    nama       VARCHAR(100),
    role       ENUM('admin','operator') DEFAULT 'operator',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS rumah_ibadah (
    id         INT AUTO_INCREMENT PRIMARY KEY,
    nama       VARCHAR(255) NOT NULL,
    jenis      ENUM('Masjid','Musholla','Gereja Protestan','Gereja Katolik','Pura','Vihara','Klenteng','Lainnya') DEFAULT 'Masjid',
    kontak     VARCHAR(100),
    alamat     TEXT,
    lat        DECIMAL(10,7) NOT NULL,
    lng        DECIMAL(10,7) NOT NULL,
    radius     INT DEFAULT 300,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS penduduk_miskin (
    id              INT AUTO_INCREMENT PRIMARY KEY,
    kk_nama         VARCHAR(255) NOT NULL,
    nik             VARCHAR(20),
    alamat          TEXT,
    kecamatan       VARCHAR(100),
    kelurahan       VARCHAR(100),
    lat             DECIMAL(10,7) NOT NULL,
    lng             DECIMAL(10,7) NOT NULL,
    jumlah_anggota  INT DEFAULT 1,
    rumah_ibadah_id INT,
    jarak_meter     DECIMAL(10,2),
    status_bantuan  ENUM('belum_dibantu','sudah_dibantu','menunggu_verifikasi') DEFAULT 'belum_dibantu',
    jenis_bantuan   VARCHAR(255),
    tanggal_bantuan DATE,
    bukti_file      VARCHAR(255),
    created_at      TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at      TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (rumah_ibadah_id) REFERENCES rumah_ibadah(id) ON DELETE SET NULL
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS anggota_keluarga (
    id          INT AUTO_INCREMENT PRIMARY KEY,
    penduduk_id INT NOT NULL,
    nama        VARCHAR(255) NOT NULL,
    hubungan    VARCHAR(50),
    umur        INT,
    pekerjaan   VARCHAR(100),
    keterangan  TEXT,
    FOREIGN KEY (penduduk_id) REFERENCES penduduk_miskin(id) ON DELETE CASCADE
) ENGINE=InnoDB;

-- ================================================================
-- DATA SAMPLE - TUGAS
-- ================================================================

INSERT IGNORE INTO spbu (nama, nomor, status_24jam, lat, lng) VALUES
('SPBU Gajahmada',       '64.771.01', 'Ya',    -0.0208, 109.3284),
('SPBU Ahmad Yani',      '64.771.02', 'Ya',    -0.0342, 109.3401),
('SPBU Tanjungpura',     '64.771.03', 'Tidak', -0.0156, 109.3512),
('SPBU Sultan Hamid II', '64.771.04', 'Ya',    -0.0470, 109.3350);

INSERT IGNORE INTO masjid (nama_masjid, pic, alamat, radius_m, lat, lng) VALUES
('Masjid Raya Mujahidin', 'H. Ahmad Fauzi', 'Jl. Gajahmada, Pontianak Selatan', 450, -0.0261, 109.3256),
('Masjid Al-Muhajirin',   'Ustadz Mahmud',  'Jl. Sutoyo, Pontianak Kota',       300, -0.0189, 109.3478),
('Masjid Nurul Huda',     'Ustadz Fahmi',   'Jl. Ahmad Yani, Pontianak Timur',  350, -0.0387, 109.3398),
('Masjid Jami Sultan',    'H. Ridwan',      'Jl. Tanjungpura, Pontianak Barat', 400, -0.0142, 109.3332);

INSERT IGNORE INTO kemiskinan (nama_kk, jumlah_kk, alamat, lat, lng, pkh, bpnt, blt) VALUES
('Ahmad Suryana',   4, 'Jl. Gajahmada Gg. 3',    -0.0278, 109.3267, 1, 1, 0),
('Siti Rohana',     3, 'Jl. Sutoyo No. 8',        -0.0201, 109.3461, 0, 1, 1),
('Budi Santoso',    5, 'Jl. Ahmad Yani No. 32',   -0.0355, 109.3412, 1, 0, 0),
('Dewi Rahayu',     2, 'Jl. Tanjungpura No. 7',   -0.0412, 109.3389, 0, 0, 1),
('Hendra Kusuma',   6, 'Jl. Imam Bonjol No. 21',  -0.0167, 109.3502, 1, 1, 1),
('Ratna Wulandari', 3, 'Jl. Gusti Situt No. 5',   -0.0301, 109.3220, 1, 0, 1),
('Suparman',        7, 'Jl. Pak Kasih No. 12',    -0.0243, 109.3310, 0, 1, 0);

-- ================================================================
-- DATA SAMPLE - UAS
-- password = 'admin123'
-- ================================================================

INSERT IGNORE INTO users (username, password, nama, role) VALUES
('admin',    '$2y$10$Ep64LS7KEQc2jKMu.AsH.O2wKF9ku4C8Cz0z4RHahgmYbz0vMr0/.', 'Administrator',    'admin'),
('operator', '$2y$10$Ep64LS7KEQc2jKMu.AsH.O2wKF9ku4C8Cz0z4RHahgmYbz0vMr0/.', 'Operator Lapangan', 'operator');

INSERT IGNORE INTO rumah_ibadah (id, nama, jenis, kontak, alamat, lat, lng, radius) VALUES
(1, 'Masjid Raya Mujahidin',             'Masjid',         '0561-123456', 'Jl. Ahmad Yani, Pontianak',      -0.0262, 109.3429, 500),
(2, 'Gereja Katedral Santo Yosef',       'Gereja Katolik', '0561-234567', 'Jl. Pattimura, Pontianak',       -0.0312, 109.3301, 400),
(3, 'Vihara Bodhisattva Karaniya Metta', 'Vihara',         '0561-345678', 'Jl. Tanjungpura, Pontianak',     -0.0355, 109.3344, 350),
(4, 'Pura Agung Nusantara',              'Pura',           '0561-456789', 'Jl. Gajah Mada, Pontianak',      -0.0409, 109.3221, 400),
(5, 'Musholla Al-Ikhlas',                'Musholla',       NULL,          'Jl. Kom Yos Sudarso, Pontianak', -0.0195, 109.3553, 250);

INSERT IGNORE INTO penduduk_miskin (id, kk_nama, nik, alamat, lat, lng, rumah_ibadah_id, jarak_meter, status_bantuan, jenis_bantuan, tanggal_bantuan) VALUES
(1, 'Ahmad Subandi', '6171011234560001', 'Gg. Melati No.3',        -0.0290, 109.3410, 1, 312.5, 'sudah_dibantu',       'PKH',        '2024-03-01'),
(2, 'Siti Rahayu',   '6171021234560002', 'Jl. Budi Utomo Gg.5',    -0.0278, 109.3450, 1, 245.0, 'belum_dibantu',       NULL,          NULL),
(3, 'Budi Santoso',  '6171031234560003', 'Jl. Gusti Hamzah',        -0.0320, 109.3290, 2, 198.3, 'menunggu_verifikasi', NULL,          NULL),
(4, 'Maria Yuliana', '6171041234560004', 'Jl. Purnama Gg.2',        -0.0350, 109.3320, 3, 267.1, 'sudah_dibantu',       'BPNT + PKH', '2024-01-15'),
(5, 'Wati Sari',     '6171051234560005', 'Jl. Kom Yos Sudarso',     -0.0188, 109.3561, 5,  89.4, 'belum_dibantu',       NULL,          NULL);

INSERT IGNORE INTO anggota_keluarga (penduduk_id, nama, hubungan, umur, pekerjaan) VALUES
(1, 'Ahmad Subandi', 'Kepala Keluarga', 45, 'Buruh Harian'),
(1, 'Dewi Subandi',  'Istri',           40, 'Ibu Rumah Tangga'),
(1, 'Rizki Subandi', 'Anak',            18, 'Pelajar'),
(2, 'Siti Rahayu',   'Kepala Keluarga', 38, 'Pedagang Kecil'),
(2, 'Putri Rahayu',  'Anak',            12, 'Pelajar'),
(3, 'Budi Santoso',  'Kepala Keluarga', 52, 'Tidak Bekerja'),
(3, 'Sari Santoso',  'Istri',           48, 'Buruh Cuci'),
(4, 'Maria Yuliana', 'Kepala Keluarga', 41, 'Pedagang'),
(5, 'Wati Sari',     'Kepala Keluarga', 33, 'Buruh');

SET FOREIGN_KEY_CHECKS = 1;
