<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddProductIdToInvoiceItemsTable extends Migration
{
    public function up()
    {
        $this->forge->addColumn('invoice_items', [
            'product_id' => [
                'type' => 'BIGINT',
                'unsigned' => true,
                'null' => true,
                'after' => 'service_id',
            ],
        ]);

        $this->db->query(
            'ALTER TABLE invoice_items ADD CONSTRAINT invoice_items_product_id_foreign
             FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE SET NULL ON UPDATE CASCADE'
        );
    }

    public function down()
    {
        $this->forge->dropForeignKey('invoice_items', 'invoice_items_product_id_foreign');
        $this->forge->dropColumn('invoice_items', 'product_id');
    }
}
