<?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('savings_accounts', function (Blueprint $table): void {
            if (! Schema::hasColumn('savings_accounts', 'start_date')) {
                $table->date('start_date')->nullable()->after('product_type');
            }

            if (! Schema::hasColumn('savings_accounts', 'contribution_periods')) {
                $table->unsignedInteger('contribution_periods')->nullable()->after('contribution_frequency');
            }

            if (! Schema::hasColumn('savings_accounts', 'planned_total_amount')) {
                $table->decimal('planned_total_amount', 14, 2)->nullable()->after('contribution_periods');
            }
        });
    }

    public function down(): void
    {
        foreach (['start_date', 'contribution_periods', 'planned_total_amount'] as $column) {
            if (Schema::hasColumn('savings_accounts', $column)) {
                Schema::table('savings_accounts', function (Blueprint $table) use ($column): void {
                    $table->dropColumn($column);
                });
            }
        }
    }
};
