-- ============================================================
-- 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;