<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Inventory module (was a disabled "Soon" nav item — now built for
 * real). Current stock only — no transaction/movement ledger yet (the
 * mockup's "Stock Value Trend" chart implied historical value tracking,
 * which would need one; omitted rather than faked, see
 * InventoryController's doc comment). "Pending Orders" (the mockup's
 * purchase-order stat) also omitted — no purchasing/ordering workflow
 * exists to back it.
 */
class CreateInventoryItemsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'name' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
            ],
            'category' => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
                'comment'    => 'linen|amenities|fnb|cleaning|other — validated against Config\Lookups.',
            ],
            'quantity_in_stock' => [
                'type'       => 'INT',
                'constraint' => 11,
                'default'    => 0,
            ],
            'unit' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'comment'    => 'pcs|kg|rolls|sets|litres — free text, not a closed list (unlike category), since units genuinely vary per item.',
            ],
            'unit_cost' => [
                'type'       => 'DECIMAL',
                'constraint' => '10,2',
                'default'    => 0,
            ],
            'reorder_level' => [
                'type'       => 'INT',
                'constraint' => 11,
                'default'    => 0,
                'comment'    => 'At or below this quantity, the item shows as Low Stock.',
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->createTable('inventory_items');
    }

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