<?php

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


return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up()
    {
        Schema::table('catatan_pertumbuhan', function (Blueprint $table) {
            $table->date('tanggal_pengukuran')->nullable()->after('anak_id');
        });

        // Copy data dari kolom lama ke kolom baru (gunakan raw query)
        DB::statement('UPDATE catatan_pertumbuhan SET tanggal_pengukuran = tanggal');

        // Hapus kolom lama
        Schema::table('catatan_pertumbuhan', function (Blueprint $table) {
            $table->dropColumn('tanggal');
        });
    }

    public function down()
    {
        Schema::table('catatan_pertumbuhan', function (Blueprint $table) {
            $table->date('tanggal')->nullable()->after('anak_id');
        });

        DB::statement('UPDATE catatan_pertumbuhan SET tanggal = tanggal_pengukuran');

        Schema::table('catatan_pertumbuhan', function (Blueprint $table) {
            $table->dropColumn('tanggal_pengukuran');
        });
    }
};