<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * medbill_claim_items — item-level claim lines (spec §22 insurance_claim.items[]).
 * Each mirrors a medbill_charge_item (charge_item_id) with its claimed amount and,
 * once adjudicated, the approved amount / denial reason per item. Business scope
 * is inherited through the parent claim.
 */
return new class extends Migration {
    public function up(): void
    {
        if (Schema::hasTable('medbill_claim_items')) {
            return;
        }

        Schema::create('medbill_claim_items', function (Blueprint $t) {
            $t->bigIncrements('id');
            $t->unsignedBigInteger('claim_id');
            $t->unsignedBigInteger('charge_item_id')->nullable(); // medbill_charge_items.id
            $t->string('description', 255);
            $t->decimal('claimed_amount', 15, 4)->default(0);
            $t->decimal('approved_amount', 15, 4)->nullable();
            $t->string('denied_reason', 255)->nullable();
            $t->timestamps();

            $t->index('claim_id', 'medbill_claim_items_claim_idx');
            $t->index('charge_item_id', 'medbill_claim_items_charge_idx');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('medbill_claim_items');
    }
};
