-- SQL Dump for database: webgis_donations
-- Updated for WebGIS Poverty Mapping schema normalization
-- Target location: Pontianak, West Kalimantan

CREATE DATABASE IF NOT EXISTS `webgis_donations`;
USE `webgis_donations`;

-- Drop tables in order of dependency
DROP TABLE IF EXISTS `distribution_history`;
DROP TABLE IF EXISTS `poverty_assessments`;
DROP TABLE IF EXISTS `households`;
DROP TABLE IF EXISTS `houses`; -- Remove old table if it exists
DROP TABLE IF EXISTS `religious_places`;
DROP TABLE IF EXISTS `users`;

-- 1. Create users table
CREATE TABLE `users` (
  `id` int NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `email` varchar(100) NOT NULL,
  `password_hash` varchar(255) NOT NULL,
  `role` enum('admin','surveyor') NOT NULL DEFAULT 'surveyor',
  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`),
  UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- Inserting surveyor and admin users
-- Password for admin: admin123
-- Password for surveyor1: surveyor123 (or similar hashes)
INSERT INTO `users` (`id`, `username`, `email`, `password_hash`, `role`) VALUES
(1, 'admin', 'admin@webgis.com', '$2b$10$wOFuhFBZzEWdnNIt7hxNBeDW.sivkV5Y0jCHQz0k0Ew7eC7T3xJQe', 'admin'),
(2, 'Surveyor1', 'surveyor1@webgis.com', '$2b$10$C6TlOgTG6FQhfq6USToWHen3gRouR3z0tnkaVbWCQlcsd7m/aluM2', 'surveyor');

-- 2. Create religious_places table (often used for linked references)
CREATE TABLE `religious_places` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `lat` decimal(10,8) NOT NULL,
  `lng` decimal(11,8) NOT NULL,
  `radius` int NOT NULL DEFAULT '500',
  `place_type` enum('mosque','church_catholic','church_protestant','pura','vihara','kelenteng') NOT NULL DEFAULT 'mosque',
  `added_by` int DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `added_by` (`added_by`),
  CONSTRAINT `religious_places_ibfk_1` FOREIGN KEY (`added_by`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

INSERT INTO `religious_places` (`id`, `name`, `lat`, `lng`, `radius`, `place_type`, `added_by`) VALUES
(1, 'Mujahideen Mosque', '-0.04149913', '109.33635592', 500, 'mosque', 1),
(2, 'Majid Amanatul Hasanah', '-0.04104852', '109.34329748', 500, 'mosque', 2);

-- 3. Create households table (Normalized from houses)
CREATE TABLE `households` (
  `id` int NOT NULL AUTO_INCREMENT,
  `kk_number` varchar(16) NOT NULL,
  `head_nik` varchar(16) NOT NULL,
  `head_name` varchar(255) NOT NULL,
  `resident_count` int NOT NULL,
  `lat` decimal(10,8) NOT NULL,
  `lng` decimal(11,8) NOT NULL,
  `linked_place_id` int DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `linked_place_id` (`linked_place_id`),
  CONSTRAINT `households_ibfk_1` FOREIGN KEY (`linked_place_id`) REFERENCES `religious_places` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- 4. Create poverty_assessments table
CREATE TABLE `poverty_assessments` (
  `id` int NOT NULL AUTO_INCREMENT,
  `household_id` int NOT NULL,
  `surveyor_id` int DEFAULT NULL,
  `electricity_va` varchar(50) NOT NULL,
  `floor_material` varchar(100) NOT NULL,
  `wall_material` varchar(100) NOT NULL,
  `drinking_water_source` varchar(100) NOT NULL,
  `photo_evidence_url` varchar(255) DEFAULT NULL,
  `verification_status` enum('pending','verified','rejected') NOT NULL DEFAULT 'pending',
  `verified_by` int DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `household_id` (`household_id`),
  KEY `surveyor_id` (`surveyor_id`),
  KEY `verified_by` (`verified_by`),
  CONSTRAINT `poverty_assessments_ibfk_1` FOREIGN KEY (`household_id`) REFERENCES `households` (`id`) ON DELETE CASCADE,
  CONSTRAINT `poverty_assessments_ibfk_2` FOREIGN KEY (`surveyor_id`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `poverty_assessments_ibfk_3` FOREIGN KEY (`verified_by`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- 5. Create distribution_history table
CREATE TABLE `distribution_history` (
  `id` int NOT NULL AUTO_INCREMENT,
  `house_id` int NOT NULL,
  `place_id` int DEFAULT NULL,
  `distributed_by` int DEFAULT NULL,
  `aid_category` varchar(100) NOT NULL,
  `amount_value` decimal(15,2) NOT NULL,
  `notes` text,
  `verification_status` enum('pending','verified','rejected') NOT NULL DEFAULT 'pending',
  `verified_by` int DEFAULT NULL,
  `verified_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `house_id` (`house_id`),
  KEY `place_id` (`place_id`),
  KEY `distributed_by` (`distributed_by`),
  KEY `verified_by` (`verified_by`),
  CONSTRAINT `distribution_history_ibfk_1` FOREIGN KEY (`house_id`) REFERENCES `households` (`id`) ON DELETE CASCADE,
  CONSTRAINT `distribution_history_ibfk_2` FOREIGN KEY (`place_id`) REFERENCES `religious_places` (`id`) ON DELETE SET NULL,
  CONSTRAINT `distribution_history_ibfk_3` FOREIGN KEY (`distributed_by`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `distribution_history_ibfk_4` FOREIGN KEY (`verified_by`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- =========================================================
-- TASK 1: Mock Data SQL Script
-- 5 households in Pontianak, West Kalimantan (lat: -0.0258, lng: 109.3323)
-- NIK/KK start with '61' for West Kalimantan
-- Assumed surveyor_id = 1
-- =========================================================

-- 1. Insert households
INSERT INTO `households` (`id`, `kk_number`, `head_nik`, `head_name`, `resident_count`, `lat`, `lng`, `linked_place_id`) VALUES
(1, '6171010101230001', '6171011203840001', 'Budi Santoso', 4, '-0.02450000', '109.33210000', 1),
(2, '6171020202340002', '6171021508820002', 'Saputra Wijaya', 6, '-0.02680000', '109.33500000', 1),
(3, '6171030303450003', '6171030912850003', 'Syahputra Ramadhan', 3, '-0.02310000', '109.33050000', NULL),
(4, '6171040404560004', '6171042104900004', 'Dian Lestari', 2, '-0.02750000', '109.33120000', 2),
(5, '6171050505670005', '6171052511790005', 'Hendra Kurniawan', 5, '-0.02520000', '109.33680000', 2);

-- 2. Insert poverty assessments
-- Mix of high-vulnerability (low VA, dirt floor, wood wall, rainwater/river)
-- and low-vulnerability (high VA, ceramic, brick, PDAM/bottled)
INSERT INTO `poverty_assessments` (`id`, `household_id`, `surveyor_id`, `electricity_va`, `floor_material`, `wall_material`, `drinking_water_source`, `photo_evidence_url`, `verification_status`, `verified_by`) VALUES
-- Household 1: High Vulnerability
(1, 1, 1, '450', 'dirt', 'wood', 'rainwater', 'https://example.com/photos/budi_house.jpg', 'pending', NULL),
-- Household 2: High/Moderate Vulnerability (Verified)
(2, 2, 1, '450', 'cement', 'wood', 'well', 'https://example.com/photos/saputra_house.jpg', 'verified', 1),
-- Household 3: Moderate/Low Vulnerability
(3, 3, 1, '900', 'wood', 'brick', 'pdam', NULL, 'pending', NULL),
-- Household 4: Low Vulnerability (Verified)
(4, 4, 1, '1300', 'ceramic', 'brick', 'bottled', NULL, 'verified', 1),
-- Household 5: High Vulnerability
(5, 5, 1, '450', 'dirt', 'wood', 'river', 'https://example.com/photos/hendra_house.jpg', 'pending', NULL);
