<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateTbStokProdukLogTable extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id_stok_produk_log' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'id_produk' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => false,
            ],
            'tanggal_stok_produk_log' => [
                'type' => 'DATE',
                'null' => false,
            ],
            'tipe_stok_produk_log' => [
                'type'       => 'ENUM',
                'constraint' => ['masuk', 'keluar'],
                'null'       => false,
            ],
            'jumlah_stok_produk_log' => [
                'type'       => 'INT',
                'constraint' => 11,
                'null'       => false,
            ],
            'keterangan_stok_produk_log' => [
                'type'       => 'VARCHAR',
                'constraint' => 200,
                'null'       => true,
                'default'    => null,
            ],
            'created_at' => [
                'type'    => 'DATETIME',
                'null'    => true,
                'default' => null,
            ],
        ]);

        $this->forge->addKey('id_stok_produk_log', true);
        $this->forge->addKey('id_produk');
        $this->forge->addKey('tanggal_stok_produk_log');
        $this->forge->addForeignKey('id_produk', 'tb_produk', 'id_produk', 'RESTRICT', 'RESTRICT');
        $this->forge->createTable('tb_stok_produk_log');
    }

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