-- ================================================================
-- FILE    : database.sql
-- DATABASE: webgis_db
-- Jalankan file ini di phpMyAdmin > tab SQL > klik Go
-- ================================================================

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

USE webgis_db;

-- ----------------------------------------------------------------
-- TABEL tb_jalan (Polyline) — ditambah kolom foto
-- ----------------------------------------------------------------
CREATE TABLE IF NOT EXISTS tb_jalan (
    id            INT(11)      NOT NULL AUTO_INCREMENT,
    nama_jalan    VARCHAR(200) NOT NULL                    COMMENT 'Nama ruas jalan',
    status_jalan  ENUM('Nasional','Provinsi','Kabupaten')
                               NOT NULL DEFAULT 'Kabupaten',
    panjang_meter DECIMAL(15,2) NOT NULL DEFAULT 0,
    geojson       LONGTEXT     NOT NULL,
    keterangan    TEXT,
    foto          VARCHAR(255)  NULL                       COMMENT 'Path file foto jalan',
    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;

-- ----------------------------------------------------------------
-- TABEL tb_parsil (Polygon) — ditambah kolom foto
-- ----------------------------------------------------------------
CREATE TABLE IF NOT EXISTS tb_parsil (
    id                 INT(11)      NOT NULL AUTO_INCREMENT,
    nama_pemilik       VARCHAR(200) NOT NULL,
    no_sertifikat      VARCHAR(100) NOT NULL,
    status_kepemilikan ENUM('SHM','HGB','HGU','HP')
                                    NOT NULL DEFAULT 'SHM',
    luas_m2            DECIMAL(20,4) NOT NULL DEFAULT 0,
    geojson            LONGTEXT      NOT NULL,
    alamat             TEXT,
    foto               VARCHAR(255)  NULL                  COMMENT 'Path file foto parsil',
    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;

-- ----------------------------------------------------------------
-- TABEL tb_jalan_rusak (Point) — fitur baru
-- ----------------------------------------------------------------
CREATE TABLE IF NOT EXISTS tb_jalan_rusak (
    id            INT(11)       NOT NULL AUTO_INCREMENT,
    lokasi        VARCHAR(200)  NOT NULL                   COMMENT 'Nama/deskripsi lokasi kerusakan',
    tingkat       ENUM('Ringan','Sedang','Berat')
                                NOT NULL DEFAULT 'Sedang'  COMMENT 'Tingkat kerusakan',
    jenis         VARCHAR(100)  NOT NULL DEFAULT ''        COMMENT 'Jenis kerusakan: berlubang, retak, dll',
    geojson       LONGTEXT      NOT NULL                   COMMENT 'Geometry Point format GeoJSON',
    keterangan    TEXT,
    foto          VARCHAR(255)  NULL                       COMMENT 'Path file foto kerusakan',
    tanggal_lapor DATE          NOT NULL DEFAULT (CURRENT_DATE),
    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;

-- ----------------------------------------------------------------
-- CONTOH DATA tb_jalan
-- ----------------------------------------------------------------
INSERT INTO tb_jalan (nama_jalan, status_jalan, panjang_meter, geojson, keterangan) VALUES
('Jl. Ahmad Yani','Nasional',1240.50,'{"type":"Feature","geometry":{"type":"LineString","coordinates":[[109.3272,-0.0207],[109.3290,-0.0215],[109.3310,-0.0225],[109.3335,-0.0238]]},"properties":{}}','Jalan Nasional utama kota Pontianak'),
('Jl. Gajah Mada','Provinsi',870.00,'{"type":"Feature","geometry":{"type":"LineString","coordinates":[[109.3250,-0.0260],[109.3265,-0.0270],[109.3280,-0.0285]]},"properties":{}}','Jalan Provinsi pusat kota'),
('Jl. Pahlawan','Kabupaten',540.75,'{"type":"Feature","geometry":{"type":"LineString","coordinates":[[109.3300,-0.0300],[109.3315,-0.0310],[109.3325,-0.0322]]},"properties":{}}','Jalan Kabupaten lingkungan');

-- ----------------------------------------------------------------
-- CONTOH DATA tb_parsil
-- ----------------------------------------------------------------
INSERT INTO tb_parsil (nama_pemilik, no_sertifikat, status_kepemilikan, luas_m2, geojson, alamat) VALUES
('Budi Santoso','SHM-001/PTK/2023','SHM',4500.00,'{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[109.3280,-0.0240],[109.3295,-0.0240],[109.3295,-0.0255],[109.3280,-0.0255],[109.3280,-0.0240]]]},"properties":{}}','Jl. Ahmad Yani No. 10, Pontianak'),
('PT. Maju Jaya','HGB-005/PTK/2022','HGB',12000.00,'{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[109.3310,-0.0220],[109.3330,-0.0220],[109.3330,-0.0240],[109.3310,-0.0240],[109.3310,-0.0220]]]},"properties":{}}','Kawasan Industri Pontianak');

-- ----------------------------------------------------------------
-- CONTOH DATA tb_jalan_rusak
-- ----------------------------------------------------------------
INSERT INTO tb_jalan_rusak (lokasi, tingkat, jenis, geojson, keterangan, tanggal_lapor) VALUES
('Depan SPBU Jl. Ahmad Yani','Berat','Berlubang','{"type":"Feature","geometry":{"type":"Point","coordinates":[109.3285,-0.0212]},"properties":{}}','Lubang besar diameter ±50cm, berbahaya bagi pengendara','2024-10-15'),
('Pertigaan Jl. Gajah Mada','Sedang','Retak','{"type":"Feature","geometry":{"type":"Point","coordinates":[109.3260,-0.0268]},"properties":{}}','Retak memanjang sepanjang 3 meter','2024-11-02');
