<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('invoice_items', function (Blueprint $table) {
            // The promotion that auto-applied to this line (null = none). The
            // deal's discount is recorded separately from the cashier's manual
            // discount so a receipt can show exactly why a line is cheaper.
            $table->foreignId('deal_id')->nullable()->after('product_bundle_id')->constrained('deals')->nullOnDelete();
            // Name snapshot: a deal can be renamed (or deleted) after the sale,
            // and the printed line must still explain itself.
            $table->string('deal_name', 150)->nullable()->after('deal_id');
            $table->decimal('deal_discount_amount', 14, 2)->default(0)->after('deal_name');
        });
    }

    public function down(): void
    {
        Schema::table('invoice_items', function (Blueprint $table) {
            $table->dropColumn('deal_discount_amount');
            $table->dropColumn('deal_name');
            $table->dropConstrainedForeignId('deal_id');
        });
    }
};
