<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * `category` is a plain VARCHAR, not a 2-value enum — Phase 1 only ever
 * writes room_charge/other, but Phase 2 adds restaurant/event and Phase 4's
 * KPI dashboard reconciles revenue by category, so new values are just
 * strings, never an ALTER TABLE against a live tenant database.
 */
class CreateFolioLineItemsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'folio_id' => [
                'type'     => 'INT',
                'constraint' => 11,
                'unsigned' => true,
            ],
            'description' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
            ],
            'amount' => [
                'type'       => 'DECIMAL',
                'constraint' => '10,2',
            ],
            'category' => [
                'type'       => 'VARCHAR',
                'constraint' => 30,
                'default'    => 'other',
                'comment'    => 'room_charge, other in Phase 1; restaurant/event added Phase 2',
            ],
            'item_date' => [
                'type' => 'DATE',
            ],
            'created_by' => [
                'type'     => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'null'     => true,
            ],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey('folio_id');
        $this->forge->addKey('category');
        $this->forge->addForeignKey('folio_id', 'folios', 'id', '', 'CASCADE');
        $this->forge->addForeignKey('created_by', 'users', 'id', '', 'SET NULL');
        $this->forge->createTable('folio_line_items');
    }

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