<?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::create('peminjaman', function (Blueprint $table) {
            $table->id(); // bigint primary key

            // relasi ke tabel buku
            $table->unsignedBigInteger('buku_id');

            // relasi ke tabel anggota_perpustakaan
            $table->unsignedBigInteger('anggota_id');

            // data peminjaman
            $table->date('tanggal_pinjam');
            $table->date('tanggal_kembali')->nullable();
            $table->integer('jumlah')->default(1);

            $table->enum('status', [
                'dipinjam',
                'dikembalikan',
                'terlambat'
            ])->default('dipinjam');

            $table->timestamps();

            // foreign key buku
            $table->foreign('buku_id')
                  ->references('id')
                  ->on('buku')
                  ->onDelete('cascade');

            // foreign key anggota perpustakaan
            $table->foreign('anggota_id')
                  ->references('id')
                  ->on('anggota_perpustakaan')
                  ->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('peminjaman');
    }
};
