-- migrations.sql
-- Run this SQL script in phpMyAdmin or your MySQL client for database "bansos_gis"

-- 1. Membuat tabel 'users' untuk multi-role
CREATE TABLE IF NOT EXISTS `users` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(100) NOT NULL UNIQUE,
  `email` VARCHAR(150) NOT NULL UNIQUE,
  `password` VARCHAR(255) NOT NULL,
  `role` ENUM('admin', 'petugas', 'koordinator', 'kontributor') NOT NULL DEFAULT 'kontributor',
  `rumah_ibadah_id` INT(11) NULL DEFAULT NULL COMMENT 'Terhubung ke rumah ibadah tertentu untuk Petugas & Koordinator',
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(),
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP(),
  PRIMARY KEY (`id`),
  CONSTRAINT `fk_users_rumah_ibadah` FOREIGN KEY (`rumah_ibadah_id`) REFERENCES `rumah_ibadah` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- 2. Membuat tabel 'kebutuhan' rumah ibadah
CREATE TABLE IF NOT EXISTS `kebutuhan` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `rumah_ibadah_id` INT(11) NOT NULL,
  `jenis_bantuan` VARCHAR(150) NOT NULL COMMENT 'Contoh: Beras, Uang Operasional, Jasa Renovasi, dll',
  `kategori` ENUM('uang', 'barang', 'jasa') NOT NULL DEFAULT 'barang',
  `satuan` VARCHAR(32) NULL DEFAULT 'unit' COMMENT 'satuan untuk jumlah_dibutuhkan (orang/jam/shift/unit/nominal)',
  `jumlah_dibutuhkan` DECIMAL(15,2) NOT NULL DEFAULT 0.00,
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(),
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP(),
  PRIMARY KEY (`id`),
  CONSTRAINT `fk_kebutuhan_rumah_ibadah` FOREIGN KEY (`rumah_ibadah_id`) REFERENCES `rumah_ibadah` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- 3. Membuat tabel 'kontribusi' oleh kontributor
CREATE TABLE IF NOT EXISTS `kontribusi` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `user_id` INT(11) NOT NULL,
  `kebutuhan_id` INT(11) NOT NULL,
  `jenis_kontribusi` ENUM('uang', 'barang', 'jasa') NOT NULL,
  `jumlah` DECIMAL(15,2) NOT NULL DEFAULT 0.00,
  `detail_barang_jasa` TEXT NULL COMMENT 'Deskripsi detail barang/jasa yang disumbangkan',
  `status` ENUM('pending', 'confirmed') NOT NULL DEFAULT 'pending',
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(),
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP(),
  PRIMARY KEY (`id`),
  CONSTRAINT `fk_kontribusi_users` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_kontribusi_kebutuhan` FOREIGN KEY (`kebutuhan_id`) REFERENCES `kebutuhan` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- 4. Seed User Awal (Password: password123)
-- Hash bcrypt untuk password123: $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi
INSERT INTO `users` (`username`, `email`, `password`, `role`, `rumah_ibadah_id`) 
VALUES 
('admin', 'admin@webgis.com', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'admin', NULL)
ON DUPLICATE KEY UPDATE `username`=`username`;

INSERT INTO `users` (`username`, `email`, `password`, `role`, `rumah_ibadah_id`) 
VALUES 
('petugas', 'petugas@webgis.com', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'petugas', 1)
ON DUPLICATE KEY UPDATE `username`=`username`;

INSERT INTO `users` (`username`, `email`, `password`, `role`, `rumah_ibadah_id`) 
VALUES 
('koordinator', 'koordinator@webgis.com', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'koordinator', 1)
ON DUPLICATE KEY UPDATE `username`=`username`;

INSERT INTO `users` (`username`, `email`, `password`, `role`, `rumah_ibadah_id`) 
VALUES 
('kontributor', 'kontributor@webgis.com', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'kontributor', NULL)
ON DUPLICATE KEY UPDATE `username`=`username`;

-- 5. Seed Kebutuhan Awal untuk Demonstrasi
INSERT INTO `kebutuhan` (`id`, `rumah_ibadah_id`, `jenis_bantuan`, `kategori`, `jumlah_dibutuhkan`)
VALUES
(1, 1, 'Beras Sembako (KK)', 'barang', 10.00),
(2, 1, 'Dana Operasional Santunan', 'uang', 5000000.00),
(3, 2, 'Relawan Pembagi Paket Beras', 'jasa', 5.00)
ON DUPLICATE KEY UPDATE `id`=`id`;

-- Backfill/ensure `satuan` exists for existing installations
ALTER TABLE `kebutuhan` ADD COLUMN IF NOT EXISTS `satuan` VARCHAR(32) NULL DEFAULT 'unit' COMMENT 'satuan untuk jumlah_dibutuhkan (orang/jam/shift/unit/nominal)';
