<?php

use Wibiesana\Padi\Core\DatabaseManager;

return new class {
    public function up(): void
    {
        $db = DatabaseManager::connection();
        
        // 1. sarpras_bangunan
        $db->exec("CREATE TABLE IF NOT EXISTS sarpras_bangunan (
            id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            kode_bangunan VARCHAR(50) UNIQUE NOT NULL,
            nama_bangunan VARCHAR(100) NOT NULL,
            deskripsi TEXT NULL,
            luas_tanah DECIMAL(10, 2) NULL,
            jumlah_lantai INT DEFAULT 1,
            kondisi VARCHAR(50) DEFAULT 'baik',
            tahun_pembangunan INT NULL,
            created_by INT UNSIGNED NULL,
            updated_by INT UNSIGNED NULL,
            created_at DATETIME NULL,
            updated_at DATETIME NULL,
            INDEX idx_sarpras_bangunan_kode (kode_bangunan)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");

        // 2. sarpras_ruangan
        $db->exec("CREATE TABLE IF NOT EXISTS sarpras_ruangan (
            id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            bangunan_id INT UNSIGNED NOT NULL,
            kode_ruangan VARCHAR(50) UNIQUE NOT NULL,
            nama_ruangan VARCHAR(100) NOT NULL,
            tipe_ruangan VARCHAR(50) DEFAULT 'kelas',
            lantai INT DEFAULT 1,
            kapasitas INT DEFAULT 0,
            luas DECIMAL(10, 2) NULL,
            kondisi VARCHAR(50) DEFAULT 'baik',
            keterangan TEXT NULL,
            created_by INT UNSIGNED NULL,
            updated_by INT UNSIGNED NULL,
            created_at DATETIME NULL,
            updated_at DATETIME NULL,
            FOREIGN KEY (bangunan_id) REFERENCES sarpras_bangunan(id) ON DELETE CASCADE,
            INDEX idx_sarpras_ruangan_kode (kode_ruangan)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");

        echo "✓ Sarpras prasarana tables created successfully\n";
    }

    public function down(): void
    {
        $db = DatabaseManager::connection();
        $db->exec("DROP TABLE IF EXISTS sarpras_ruangan");
        $db->exec("DROP TABLE IF EXISTS sarpras_bangunan");
        echo "✓ Sarpras prasarana tables dropped successfully\n";
    }
};
