-- =============================================================================
-- SIPKEM - Sistem Informasi Pemetaan Kemiskinan
-- Database Setup Script
-- =============================================================================
-- Usage: Run this file in your MySQL/MariaDB client:
--   mysql -u root -p < sipkem_database.sql
-- Or import it via phpMyAdmin / any MySQL GUI tool.
-- =============================================================================

-- Create and select the database
CREATE DATABASE IF NOT EXISTS `sipkem`
    CHARACTER SET utf8mb4
    COLLATE utf8mb4_unicode_ci;

USE `sipkem`;

-- =============================================================================
-- TABLE: users
-- Stores both 'pengguna' (regular users) and 'pengurus' (admin) accounts.
-- =============================================================================
CREATE TABLE IF NOT EXISTS `users` (
    `id`            VARCHAR(36)     NOT NULL,
    `nama`          VARCHAR(100)    NOT NULL,
    `email`         VARCHAR(150)    NOT NULL,
    `password_hash` VARCHAR(255)    NOT NULL,
    `role`          ENUM('pengguna', 'pengurus') NOT NULL DEFAULT 'pengguna',
    `dibuat_pada`   TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uq_users_email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


-- =============================================================================
-- TABLE: rumah_ibadah
-- Houses of worship (mosques, churches, etc.) used as aid distribution hubs.
-- =============================================================================
CREATE TABLE IF NOT EXISTS `rumah_ibadah` (
    `id`            VARCHAR(36)     NOT NULL,
    `nama`          VARCHAR(150)    NOT NULL,
    `jenis`         VARCHAR(50)     NOT NULL COMMENT 'e.g. Masjid, Gereja, Pura, Vihara',
    `kontak`        VARCHAR(100)    NOT NULL DEFAULT '-',
    `alamat`        TEXT,
    `geom`          POINT           NOT NULL COMMENT 'Spatial point (lng, lat)',
    `jangkauan_m`   INT             NOT NULL DEFAULT 500 COMMENT 'Coverage radius in meters',
    `dibuat_pada`   TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


-- =============================================================================
-- TABLE: rumah_miskin
-- Low-income households to be mapped and assigned to a rumah_ibadah.
-- =============================================================================
CREATE TABLE IF NOT EXISTS `rumah_miskin` (
    `id`            VARCHAR(36)     NOT NULL,
    `nama_kk`       VARCHAR(150)    NOT NULL COMMENT 'Head of household name',
    `kontak`        VARCHAR(100)    NOT NULL DEFAULT '-',
    `alamat`        TEXT,
    `geom`          POINT           NOT NULL COMMENT 'Spatial point (lng, lat)',
    `ri_id`         VARCHAR(36)     NULL COMMENT 'Assigned rumah_ibadah',
    `jarak_ke_ri_m` FLOAT           NULL COMMENT 'Distance to assigned rumah_ibadah in meters',
    `dibuat_pada`   TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_rm_ri_id` (`ri_id`),
    CONSTRAINT `fk_rm_ri`
        FOREIGN KEY (`ri_id`) REFERENCES `rumah_ibadah` (`id`)
        ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


-- =============================================================================
-- TABLE: jenis_bantuan
-- Types / categories of aid that can be given to low-income households.
-- =============================================================================
CREATE TABLE IF NOT EXISTS `jenis_bantuan` (
    `id`            VARCHAR(36)     NOT NULL,
    `nama`          VARCHAR(100)    NOT NULL,
    `kategori`      VARCHAR(50)     NOT NULL DEFAULT 'Lainnya',
    `deskripsi`     TEXT,
    `dibuat_pada`   TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


-- =============================================================================
-- TABLE: riwayat_bantuan
-- Aid history — records each instance of aid given to a household.
-- =============================================================================
CREATE TABLE IF NOT EXISTS `riwayat_bantuan` (
    `id`            VARCHAR(36)     NOT NULL,
    `rm_id`         VARCHAR(36)     NOT NULL,
    `bantuan_id`    VARCHAR(36)     NOT NULL,
    `tanggal`       DATE            NOT NULL,
    `catatan`       TEXT,
    `user_id`       VARCHAR(36)     NULL COMMENT 'User who confirmed the aid',
    `dibuat_pada`   TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_rb_rm_id` (`rm_id`),
    KEY `idx_rb_bantuan_id` (`bantuan_id`),
    CONSTRAINT `fk_rb_rm`
        FOREIGN KEY (`rm_id`) REFERENCES `rumah_miskin` (`id`)
        ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `fk_rb_bantuan`
        FOREIGN KEY (`bantuan_id`) REFERENCES `jenis_bantuan` (`id`)
        ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `fk_rb_user`
        FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
        ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


-- =============================================================================
-- TABLE: riwayat_binaan
-- Assignment history — tracks when a household is moved between rumah_ibadah.
-- =============================================================================
CREATE TABLE IF NOT EXISTS `riwayat_binaan` (
    `id`            INT             NOT NULL AUTO_INCREMENT,
    `rm_id`         VARCHAR(36)     NOT NULL,
    `ri_id_lama`    VARCHAR(36)     NULL COMMENT 'Previous rumah_ibadah',
    `ri_id_baru`    VARCHAR(36)     NULL COMMENT 'New rumah_ibadah',
    `alasan`        VARCHAR(255)    NOT NULL DEFAULT 'otomatis',
    `dibuat_pada`   TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_rib_rm_id` (`rm_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


-- =============================================================================
-- DEFAULT SEED DATA
-- =============================================================================

-- Default 'pengurus' (admin) account
-- Email: admin@sipkem.id  |  Password: admin1234
INSERT IGNORE INTO `users` (`id`, `nama`, `email`, `password_hash`, `role`)
VALUES (
    'admin-0000-0000-0000-000000000001',
    'Administrator',
    'admin@sipkem.id',
    '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', -- password: admin1234
    'pengurus'
);

-- Common aid types
INSERT IGNORE INTO `jenis_bantuan` (`id`, `nama`, `kategori`, `deskripsi`) VALUES
    ('bt-0000-0000-0000-000000000001', 'Sembako',        'Pangan',      'Paket bahan pokok makanan'),
    ('bt-0000-0000-0000-000000000002', 'Uang Tunai',     'Finansial',   'Bantuan langsung tunai'),
    ('bt-0000-0000-0000-000000000003', 'Pakaian Layak',  'Sandang',     'Pakaian bekas layak pakai'),
    ('bt-0000-0000-0000-000000000004', 'Biaya Sekolah',  'Pendidikan',  'Bantuan biaya pendidikan anak'),
    ('bt-0000-0000-0000-000000000005', 'Obat-obatan',    'Kesehatan',   'Obat dan kebutuhan medis dasar');

-- =============================================================================
-- DONE
-- =============================================================================
-- After running this script:
--   1. Open api.php and set DB_HOST, DB_USER, DB_PASS to match your environment.
--   2. Default admin login: admin@sipkem.id / admin1234 (change this immediately).
--   3. Run `npm install && npm run dev` in the project folder to start the frontend.
-- =============================================================================
