<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Line-item-level split billing — closes the assumption flagged in
 * FolioPayerAllocationModel's original doc comment ("allocations are
 * folio-level totals per payer, not tied to individual line items").
 * Nullable: a null folio_line_item_id keeps meaning exactly what every
 * existing allocation already means — "this payer covers this amount of
 * the folio's total" — so nothing about current behavior changes. A set
 * value narrows that same amount to one specific charge instead.
 */
class AddLineItemToFolioPayerAllocations extends Migration
{
    public function up()
    {
        $this->forge->addColumn('folio_payer_allocations', [
            'folio_line_item_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
                'after'      => 'folio_id',
            ],
        ]);

        $this->forge->addForeignKey('folio_line_item_id', 'folio_line_items', 'id', '', 'CASCADE');
        $this->forge->processIndexes('folio_payer_allocations');
    }

    public function down()
    {
        $this->forge->dropForeignKey('folio_payer_allocations', 'folio_payer_allocations_folio_line_item_id_foreign');
        $this->forge->dropColumn('folio_payer_allocations', 'folio_line_item_id');
    }
}
