<?php

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

return new class extends Migration {
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // Table: tizinjenis
        if (!Schema::connection('mai1')->hasTable('tizinjenis')) {
            Schema::connection('mai1')->create('tizinjenis', function (Blueprint $table) {
                $table->id();
                $table->string('title'); // e.g., Sakit, Izin, Lainnya
                $table->string('code')->nullable();
                $table->timestamps();
            });

            // Seed default data
            DB::connection('mai1')->table('tizinjenis')->insert([
                ['title' => 'Sakit', 'code' => 'S', 'created_at' => now(), 'updated_at' => now()],
                ['title' => 'Izin', 'code' => 'I', 'created_at' => now(), 'updated_at' => now()],
                ['title' => 'Lainnya', 'code' => 'O', 'created_at' => now(), 'updated_at' => now()],
            ]);
        }

        // Table: tizin
        if (!Schema::connection('mai1')->hasTable('tizin')) {
            Schema::connection('mai1')->create('tizin', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('idsis'); // Relasi ke siswa/identitas
                $table->unsignedBigInteger('jen'); // Relasi ke tizinjenis
                $table->date('tgl_mulai');
                $table->date('tgl_akhir');
                $table->text('ket'); // Keterangan
                $table->string('dok')->nullable(); // Dokumen path
                $table->string('sta')->default('menunggu'); // Status: menunggu, disetujui, ditolak, dibatalkan
                $table->timestamps();

                // $table->foreign('jen')->references('id')->on('tizinjenis');
                // Note: Foreign keys might need specific handling if engines vary, keeping it simple for now.
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::connection('mai1')->dropIfExists('tizin');
        Schema::connection('mai1')->dropIfExists('tizinjenis');
    }
};
