-- ============================================
-- DATABASE SETUP - SIG Tugas Kuliah
-- Manajemen Jalan & Parsil Tanah - Pontianak
-- ============================================

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

USE sig_tugas;

-- Tabel Data Jalan (Polyline)
CREATE TABLE IF NOT EXISTS jalan (
    id INT AUTO_INCREMENT PRIMARY KEY,
    nama_jalan VARCHAR(100) NOT NULL,
    status_jalan ENUM('Nasional','Provinsi','Kabupaten') NOT NULL,
    panjang DOUBLE NOT NULL DEFAULT 0 COMMENT 'Panjang dalam meter, dihitung otomatis Leaflet',
    geometry LONGTEXT NOT NULL COMMENT 'GeoJSON LineString',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Tabel Data Parsil Tanah (Polygon)
CREATE TABLE IF NOT EXISTS tanah (
    id INT AUTO_INCREMENT PRIMARY KEY,
    kode_kavling VARCHAR(50) NOT NULL,
    nama_pemilik VARCHAR(100) NOT NULL,
    status_tanah ENUM('SHM','HGB','HGU','HP') NOT NULL,
    luas DOUBLE NOT NULL DEFAULT 0 COMMENT 'Luas dalam meter persegi, dihitung otomatis Leaflet',
    geometry LONGTEXT NOT NULL COMMENT 'GeoJSON Polygon',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- ============================================
-- Sample Data - Jalan di Pontianak
-- ============================================
INSERT INTO jalan (nama_jalan, status_jalan, panjang, geometry) VALUES
(
    'Jl. Ahmad Yani',
    'Nasional',
    3250.5,
    '{"type":"LineString","coordinates":[[109.3425,-0.0222],[109.3480,-0.0198],[109.3540,-0.0175],[109.3600,-0.0155]]}'
),
(
    'Jl. Gajah Mada',
    'Provinsi',
    1820.3,
    '{"type":"LineString","coordinates":[[109.3310,-0.0290],[109.3355,-0.0265],[109.3400,-0.0242]]}'
),
(
    'Jl. Tanjungpura',
    'Kabupaten',
    1100.7,
    '{"type":"LineString","coordinates":[[109.3250,-0.0350],[109.3285,-0.0330],[109.3320,-0.0310]]}'
);

-- ============================================
-- Sample Data - Parsil Tanah di Pontianak
-- ============================================
INSERT INTO tanah (kode_kavling, nama_pemilik, status_tanah, luas, geometry) VALUES
(
    'KAV-001',
    'Budi Santoso',
    'SHM',
    1250.75,
    '{"type":"Polygon","coordinates":[[[109.3350,-0.0400],[109.3370,-0.0400],[109.3370,-0.0420],[109.3350,-0.0420],[109.3350,-0.0400]]]}'
),
(
    'KAV-002',
    'PT. Maju Bersama',
    'HGB',
    3400.00,
    '{"type":"Polygon","coordinates":[[[109.3280,-0.0450],[109.3310,-0.0450],[109.3310,-0.0480],[109.3280,-0.0480],[109.3280,-0.0450]]]}'
),
(
    'KAV-003',
    'CV. Agro Kalimantan',
    'HGU',
    5800.50,
    '{"type":"Polygon","coordinates":[[[109.3150,-0.0500],[109.3200,-0.0500],[109.3200,-0.0540],[109.3150,-0.0540],[109.3150,-0.0500]]]}'
);