<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateTbPenggunaanBahanTable extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id_penggunaan' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'id_bahan' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => false,
            ],
            'tanggal_penggunaan' => [
                'type' => 'DATE',
                'null' => false,
            ],
            'jumlah_penggunaan' => [
                'type'       => 'DECIMAL',
                'constraint' => '12,3',
                'null'       => false,
            ],
            'keperluan_penggunaan' => [
                'type'       => 'VARCHAR',
                'constraint' => 200,
                'null'       => true,
            ],
            'catatan_penggunaan' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'created_at' => [
                'type'    => 'DATETIME',
                'null'    => true,
                'default' => null,
            ],
            'updated_at' => [
                'type'    => 'DATETIME',
                'null'    => true,
                'default' => null,
            ],
            'deleted_at' => [
                'type'    => 'DATETIME',
                'null'    => true,
                'default' => null,
            ],
        ]);

        $this->forge->addKey('id_penggunaan', true);
        $this->forge->addForeignKey('id_bahan', 'tb_bahan', 'id_bahan', 'CASCADE', 'RESTRICT');
        $this->forge->createTable('tb_penggunaan_bahan');
    }

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