<?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('easy_in_purchases', function (Blueprint $table) {
            if (! Schema::hasColumn('easy_in_purchases', 'division_id')) {
                $table->foreignId('division_id')
                    ->nullable()
                    ->after('total_amount')
                    ->constrained('project_divisions')
                    ->onDelete('set null');
            }
        });

        Schema::table('easy_out_dispatches', function (Blueprint $table) {
            if (! Schema::hasColumn('easy_out_dispatches', 'bill_number')) {
                $table->string('bill_number', 100)->nullable()->after('dispatch_date');
                $table->index('bill_number');
            }

            if (! Schema::hasColumn('easy_out_dispatches', 'mr_no')) {
                $table->string('mr_no', 100)->nullable()->after('bill_number');
            }
        });
    }

    public function down(): void
    {
        Schema::table('easy_out_dispatches', function (Blueprint $table) {
            if (Schema::hasColumn('easy_out_dispatches', 'mr_no')) {
                $table->dropColumn('mr_no');
            }

            if (Schema::hasColumn('easy_out_dispatches', 'bill_number')) {
                $table->dropIndex(['bill_number']);
                $table->dropColumn('bill_number');
            }
        });

        Schema::table('easy_in_purchases', function (Blueprint $table) {
            if (Schema::hasColumn('easy_in_purchases', 'division_id')) {
                $table->dropForeign(['division_id']);
                $table->dropColumn('division_id');
            }
        });
    }
};
