-- ============================================================
-- DATABASE PROYEK 02 — Jalanan, Parsil, Jalan Rusak
-- Import ini ke phpMyAdmin (dari server, bukan dari database)
-- ============================================================

SET FOREIGN_KEY_CHECKS = 0;

-- Database data_jalan: tabel jalan & parsil
CREATE DATABASE IF NOT EXISTS data_jalan
  CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE data_jalan;

DROP TABLE IF EXISTS parsil;
DROP TABLE IF EXISTS jalan;

CREATE TABLE jalan (
    id         INT AUTO_INCREMENT PRIMARY KEY,
    nama       VARCHAR(150) NOT NULL,
    status     ENUM('baik','sedang','rusak_ringan','rusak_berat') DEFAULT 'baik',
    panjang    DECIMAL(10,2) DEFAULT 0,
    geometry   LONGTEXT COMMENT 'GeoJSON LineString',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE parsil (
    id         INT AUTO_INCREMENT PRIMARY KEY,
    pemilik    VARCHAR(150) NOT NULL,
    status     ENUM('milik_sendiri','sewa','hibah','sengketa') DEFAULT 'milik_sendiri',
    luas       DECIMAL(12,2) DEFAULT 0,
    geometry   LONGTEXT COMMENT 'GeoJSON Polygon',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Database webgis: tabel jalan_rusak
CREATE DATABASE IF NOT EXISTS webgis
  CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE webgis;

DROP TABLE IF EXISTS jalan_rusak;

CREATE TABLE jalan_rusak (
    id         INT AUTO_INCREMENT PRIMARY KEY,
    keterangan TEXT,
    tingkat    ENUM('ringan','sedang','berat') DEFAULT 'sedang',
    geometry   LONGTEXT COMMENT 'GeoJSON geometry',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

SET FOREIGN_KEY_CHECKS = 1;

-- Insert Dummy Data for data_jalan
USE data_jalan;
INSERT INTO jalan (nama, status, panjang, geometry) VALUES
('Jalan Ahmad Yani', 'baik', 1200, '{"type":"LineString","coordinates":[[109.342500,-0.026300],[109.350000,-0.030000]]}'),
('Jalan Tanjungpura', 'sedang', 800, '{"type":"LineString","coordinates":[[109.331500,-0.018500],[109.340000,-0.020000]]}');

INSERT INTO parsil (pemilik, status, luas, geometry) VALUES
('Budi', 'milik_sendiri', 500, '{"type":"Polygon","coordinates":[[[109.342500,-0.026300],[109.343500,-0.026300],[109.343500,-0.027300],[109.342500,-0.027300],[109.342500,-0.026300]]]}');

-- Insert Dummy Data for webgis
USE webgis;
INSERT INTO jalan_rusak (keterangan, tingkat, geometry) VALUES
('Lubang besar', 'berat', '{"type":"Point","coordinates":[109.340000,-0.020000]}');

-- ============================================================
-- SELESAI — 2 database, 3 tabel siap dipakai
-- ============================================================
