-- ============================================================
-- MIGRASI: Penyesuaian Proses Bisnis WebGIS v2.0
-- Jalankan di phpMyAdmin setelah database_lengkap.sql
-- ============================================================

USE webgis_p3;

-- ── 1. Tambah kolom NIK, No WA, dan alasan_tolak ke laporan_masyarakat ──
-- Periksa apakah kolom sudah ada sebelum menambahkan

-- Tambah kolom nik
SET @col_exists = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = 'webgis_p3' AND TABLE_NAME = 'laporan_masyarakat' AND COLUMN_NAME = 'nik');
SET @sql = IF(@col_exists = 0, 
    'ALTER TABLE laporan_masyarakat ADD COLUMN nik VARCHAR(20) DEFAULT NULL AFTER nama_pelapor', 
    'SELECT 1');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Tambah kolom no_wa
SET @col_exists = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = 'webgis_p3' AND TABLE_NAME = 'laporan_masyarakat' AND COLUMN_NAME = 'no_wa');
SET @sql = IF(@col_exists = 0, 
    'ALTER TABLE laporan_masyarakat ADD COLUMN no_wa VARCHAR(20) DEFAULT NULL AFTER nik', 
    'SELECT 1');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Tambah kolom alasan_tolak
SET @col_exists = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = 'webgis_p3' AND TABLE_NAME = 'laporan_masyarakat' AND COLUMN_NAME = 'alasan_tolak');
SET @sql = IF(@col_exists = 0, 
    'ALTER TABLE laporan_masyarakat ADD COLUMN alasan_tolak TEXT DEFAULT NULL AFTER status', 
    'SELECT 1');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- ── 2. Perluas ENUM status di laporan_masyarakat ──
-- Tambah status: Diverifikasi, Ditolak
ALTER TABLE laporan_masyarakat 
    MODIFY COLUMN status ENUM('Pending','Diproses','Diverifikasi','Ditolak','Selesai') 
    NOT NULL DEFAULT 'Pending';

-- ── 3. Pastikan tabel riwayat_bantuan ada ──
CREATE TABLE IF NOT EXISTS riwayat_bantuan (
    id             INT AUTO_INCREMENT PRIMARY KEY,
    id_kemiskinan  INT NOT NULL,
    pemberi        VARCHAR(200) NOT NULL,
    jenis_bantuan  VARCHAR(200) NOT NULL,
    tanggal        DATE NOT NULL,
    keterangan     TEXT DEFAULT NULL,
    created_at     TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (id_kemiskinan) REFERENCES data_kemiskinan(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ── 4. Pastikan tabel laporan_masyarakat ada ──
CREATE TABLE IF NOT EXISTS laporan_masyarakat (
    id             INT AUTO_INCREMENT PRIMARY KEY,
    nama_pelapor   VARCHAR(200) NOT NULL,
    nik            VARCHAR(20) DEFAULT NULL,
    no_wa          VARCHAR(20) DEFAULT NULL,
    deskripsi      TEXT NOT NULL,
    latitude       DOUBLE NOT NULL,
    longitude      DOUBLE NOT NULL,
    status         ENUM('Pending','Diproses','Diverifikasi','Ditolak','Selesai') NOT NULL DEFAULT 'Pending',
    alasan_tolak   TEXT DEFAULT NULL,
    created_at     TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ── 5. Tambahkan kolom yang mungkin belum ada di data_kemiskinan ──
-- status_bantuan
SET @col_exists = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = 'webgis_p3' AND TABLE_NAME = 'data_kemiskinan' AND COLUMN_NAME = 'status_bantuan');
SET @sql = IF(@col_exists = 0, 
    "ALTER TABLE data_kemiskinan ADD COLUMN status_bantuan VARCHAR(20) NOT NULL DEFAULT 'Belum' AFTER longitude", 
    'SELECT 1');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- kategori
SET @col_exists = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = 'webgis_p3' AND TABLE_NAME = 'data_kemiskinan' AND COLUMN_NAME = 'kategori');
SET @sql = IF(@col_exists = 0, 
    "ALTER TABLE data_kemiskinan ADD COLUMN kategori VARCHAR(50) NOT NULL DEFAULT 'Miskin' AFTER status_bantuan", 
    'SELECT 1');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- ── 6. Password semua akun: "password" ──
-- Jalankan update_pass.php atau generate_hash.php jika database sudah ada.
