<?php
$conn = new mysqli('127.0.0.1', 'root', '', 'siakad');
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }

$fields = [
    "id_mahasiswa_pddikti VARCHAR(50) NULL AFTER id",
    "id_registrasi_mahasiswa VARCHAR(50) NULL AFTER id_mahasiswa_pddikti",
    "angkatan VARCHAR(4) NULL AFTER prodi_id",
    "nisn VARCHAR(20) NULL",
    "npwp VARCHAR(50) NULL",
    "jalan VARCHAR(255) NULL",
    "dusun VARCHAR(100) NULL",
    "rt VARCHAR(10) NULL",
    "rw VARCHAR(10) NULL",
    "kelurahan VARCHAR(100) NULL",
    "kode_pos VARCHAR(10) NULL",
    "kecamatan VARCHAR(100) NULL",
    "jenis_tinggal VARCHAR(50) NULL",
    "telepon VARCHAR(50) NULL",
    "email_pribadi VARCHAR(100) NULL",
    "penerima_kps VARCHAR(10) NULL",
    "nomor_kps VARCHAR(50) NULL",
    "no_rekening VARCHAR(50) NULL",
    "nama_pemilik_rekening VARCHAR(100) NULL",
    "nama_bank VARCHAR(50) NULL",
    "nik_ayah VARCHAR(20) NULL",
    "nama_ayah VARCHAR(150) NULL",
    "tanggal_lahir_ayah DATE NULL",
    "pendidikan_ayah VARCHAR(50) NULL",
    "pekerjaan_ayah VARCHAR(50) NULL",
    "penghasilan_ayah VARCHAR(50) NULL",
    "nik_ibu VARCHAR(20) NULL",
    "nama_ibu_kandung VARCHAR(150) NULL",
    "tanggal_lahir_ibu DATE NULL",
    "pendidikan_ibu VARCHAR(50) NULL",
    "pekerjaan_ibu VARCHAR(50) NULL",
    "penghasilan_ibu VARCHAR(50) NULL",
    "nama_wali VARCHAR(150) NULL",
    "tanggal_lahir_wali DATE NULL",
    "pendidikan_wali VARCHAR(50) NULL",
    "pekerjaan_wali VARCHAR(50) NULL",
    "penghasilan_wali VARCHAR(50) NULL"
];

foreach ($fields as $fieldDef) {
    $parts = explode(' ', trim($fieldDef));
    $colName = $parts[0];
    
    $check = $conn->query("SHOW COLUMNS FROM mahasiswa LIKE '$colName'");
    if ($check->num_rows == 0) {
        $sql = "ALTER TABLE mahasiswa ADD COLUMN $fieldDef";
        if ($conn->query($sql) === TRUE) {
            echo "Added column $colName\n";
        } else {
            echo "Error adding $colName: " . $conn->error . "\n";
        }
    } else {
        echo "Column $colName already exists\n";
    }
}
echo "Migration complete.";
