<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateDetailPeminjaman extends Migration
{
    public function up()
    {
        $this->forge->addField([

            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],

            'peminjaman_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],

            'buku_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],

            'tanggal_kembali' => [
                'type' => 'DATE',
                'null' => true,
            ],

            'status' => [
                'type'       => 'ENUM',
                'constraint' => ['Dipinjam', 'Kembali'],
                'default'    => 'Dipinjam',
            ],

            'terlambat' => [
                'type'       => 'INT',
                'constraint' => 11,
                'default'    => 0,
            ],

            'denda' => [
                'type'       => 'DECIMAL',
                'constraint' => '12,2',
                'default'    => 0,
            ],

            // ✅ FIX DI SINI
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],

            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],

        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey('peminjaman_id');
        $this->forge->addKey('buku_id');

        $this->forge->addForeignKey(
            'peminjaman_id',
            'peminjaman',
            'id',
            'CASCADE',
            'CASCADE'
        );

        $this->forge->addForeignKey(
            'buku_id',
            'buku',
            'id',
            'CASCADE',
            'CASCADE'
        );

        $this->forge->createTable('detail_peminjaman');
    }

    public function down()
    {
        $this->forge->dropTable('detail_peminjaman', true);
    }
}