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

$db = getDB();

try {
    $db->exec("CREATE TABLE IF NOT EXISTS berkas_pinjaman (
        id INT AUTO_INCREMENT PRIMARY KEY,
        anggota_id INT NOT NULL,
        pinjaman_id INT NOT NULL,
        nama_berkas VARCHAR(255) NOT NULL,
        file_path VARCHAR(255) NOT NULL,
        file_type VARCHAR(100) NOT NULL,
        file_size INT NOT NULL,
        keterangan TEXT DEFAULT NULL,
        created_by INT DEFAULT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        FOREIGN KEY (anggota_id) REFERENCES anggota(id) ON DELETE CASCADE,
        FOREIGN KEY (pinjaman_id) REFERENCES pinjaman(id) ON DELETE CASCADE,
        FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL
    ) ENGINE=InnoDB");

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