<?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('orders', function (Blueprint $table) {
            $table->boolean('is_cancelled')->default(false)->after('spk_printed_at');
            $table->timestamp('cancelled_at')->nullable()->after('is_cancelled');
            $table->foreignId('cancelled_by')->nullable()->constrained('users')->onDelete('set null')->after('cancelled_at');
            $table->text('cancel_reason')->nullable()->after('cancelled_by');
            $table->index('is_cancelled');
        });

        Schema::table('order_items', function (Blueprint $table) {
            $table->boolean('is_cancelled')->default(false)->after('item_finished_at');
            $table->timestamp('cancelled_at')->nullable()->after('is_cancelled');
            $table->foreignId('cancelled_by')->nullable()->constrained('users')->onDelete('set null')->after('cancelled_at');
            $table->text('cancel_reason')->nullable()->after('cancelled_by');
            $table->index('is_cancelled');
        });
    }

    public function down(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            $table->dropForeign(['cancelled_by']);
            $table->dropIndex(['is_cancelled']);
            $table->dropColumn(['is_cancelled', 'cancelled_at', 'cancelled_by', 'cancel_reason']);
        });

        Schema::table('order_items', function (Blueprint $table) {
            $table->dropForeign(['cancelled_by']);
            $table->dropIndex(['is_cancelled']);
            $table->dropColumn(['is_cancelled', 'cancelled_at', 'cancelled_by', 'cancel_reason']);
        });
    }
};
