<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     * Tabel Akademik: tahun_ajaran, kelas, siswa, siswa_kelas
     */
    public function up(): void
    {
        // ─── Tahun Ajaran ────────────────────────────────────
        Schema::create('tahun_ajaran', function (Blueprint $table) {
            $table->id();
            $table->string('tahun')->unique(); // contoh: "2026/2027"
            $table->enum('status', ['aktif', 'nonaktif'])->default('nonaktif');
            $table->timestamps();
        });

        // ─── Kelas ──────────────────────────────────────────
        // Jenjang: TK, SD, SMP, SMA
        // Tingkat: A/B (TK), 1-6 (SD), 7-9 (SMP), 10-12 (SMA)
        // Paralel: A, B, C, 1, 2, 3
        Schema::create('kelas', function (Blueprint $table) {
            $table->id();
            $table->enum('jenjang', ['TK', 'SD', 'SMP', 'SMA']);
            $table->string('tingkat');          // A, B, 1-12
            $table->string('paralel')->nullable(); // A, B, C, 1, 2, 3
            $table->string('nama_kelas');       // generated: "X IPA A" atau "TK A"
            $table->foreignId('tahun_ajaran_id')->constrained('tahun_ajaran')->onDelete('cascade');
            $table->timestamps();

            $table->unique(['jenjang', 'tingkat', 'paralel', 'tahun_ajaran_id'], 'kelas_unique');
        });

        // ─── Siswa ──────────────────────────────────────────
        Schema::create('siswa', function (Blueprint $table) {
            $table->id();
            $table->string('nis')->unique();
            $table->string('nama_lengkap');
            $table->enum('kategori', ['internal', 'external']); // mempengaruhi tarif DPP
            $table->enum('jenis_kelamin', ['L', 'P']);
            $table->string('no_wa')->nullable();
            $table->enum('status', ['aktif', 'naik_kelas', 'lulus', 'tidak_aktif'])->default('aktif');
            $table->timestamps();
        });

        // ─── Pivot: Siswa-Kelas (penempatan per tahun ajaran) ─────
        Schema::create('siswa_kelas', function (Blueprint $table) {
            $table->id();
            $table->foreignId('siswa_id')->constrained('siswa')->onDelete('cascade');
            $table->foreignId('kelas_id')->constrained('kelas')->onDelete('cascade');
            $table->foreignId('tahun_ajaran_id')->constrained('tahun_ajaran')->onDelete('cascade');
            $table->timestamps();

            $table->unique(['siswa_id', 'tahun_ajaran_id'], 'siswa_kelas_unique');
        });

        // ─── Riwayat Status Siswa (tracking perubahan status) ────
        Schema::create('riwayat_status_siswa', function (Blueprint $table) {
            $table->id();
            $table->foreignId('siswa_id')->constrained('siswa')->onDelete('cascade');
            $table->foreignId('tahun_ajaran_id')->constrained('tahun_ajaran')->onDelete('cascade');
            $table->enum('status_lama', ['aktif', 'naik_kelas', 'lulus', 'tidak_aktif']);
            $table->enum('status_baru', ['aktif', 'naik_kelas', 'lulus', 'tidak_aktif']);
            $table->text('keterangan')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('riwayat_status_siswa');
        Schema::dropIfExists('siswa_kelas');
        Schema::dropIfExists('siswa');
        Schema::dropIfExists('kelas');
        Schema::dropIfExists('tahun_ajaran');
    }
};
