<?php

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

return new class extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

            DB::statement("
                UPDATE records
                SET data_pagamento = CASE
                    WHEN date IS NOT NULL THEN date
                    ELSE CURRENT_DATE()
                END
                WHERE data_pagamento IS NULL AND type='OUT'
            ");

            // Log the number of updated rows
            $updatedRows = DB::select("
                SELECT COUNT(*) as count FROM records
                WHERE data_pagamento IS NOT NULL AND type='OUT'
            ");
            Log::info('Updated data_pagamento for OUT records: ' . $updatedRows[0]->count);


    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // No need to revert the data updates, but you could if needed
        // In this case, we won't roll back the data fixes
    }
};
