<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('absensis', function (Blueprint $table) {
            // 1. Tambahkan kolom guru_id jika belum ada
            if (!Schema::hasColumn('absensis', 'guru_id')) {
                $table->unsignedBigInteger('guru_id')->nullable()->after('siswa_id');
                $table->foreign('guru_id')->references('id')->on('gurus')->onDelete('cascade');
            }

            // 2. Ubah nama jenis_ibadah menjadi kategori agar lebih umum
            if (Schema::hasColumn('absensis', 'jenis_ibadah')) {
                $table->renameColumn('jenis_ibadah', 'kategori');
            }
        });
    }

    public function down(): void
    {
        Schema::table('absensis', function (Blueprint $table) {
            $table->dropForeign(['guru_id']);
            $table->dropColumn('guru_id');
            $table->renameColumn('kategori', 'jenis_ibadah');
        });
    }
};
