<?php
require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/config/auth.php';

$db = getDB();

try {
    $db->exec("CREATE TABLE IF NOT EXISTS notifications (
        id INT AUTO_INCREMENT PRIMARY KEY,
        user_id INT DEFAULT NULL,
        title VARCHAR(255) NOT NULL,
        message TEXT NOT NULL,
        type ENUM('info', 'success', 'warning', 'danger') DEFAULT 'info',
        is_read TINYINT(1) DEFAULT 0,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
    ) ENGINE=InnoDB");

    // Clear existing dummy if any
    $db->exec("DELETE FROM notifications WHERE user_id = 1 AND title = 'Selamat Datang'");

    // Insert dummy for user 1
    $stmt = $db->prepare("INSERT INTO notifications (user_id, title, message, type) VALUES (?, ?, ?, ?)");
    $stmt->execute([1, 'Selamat Datang', 'Terima kasih telah menggunakan sistem manajemen koperasi.', 'info']);
    $stmt->execute([1, 'Pencairan Pinjaman', 'Pinjaman AGT-0001 telah berhasil dicairkan.', 'success']);
    $stmt->execute([1, 'Tagihan Jatuh Tempo', 'Ada 3 pinjaman yang akan jatuh tempo dalam 3 hari.', 'warning']);

    echo "Migration successful.";
} catch (Exception $e) {
    echo "Migration failed: " . $e->getMessage();
}
