<?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(): void
    {
        // Set transaction_type='in' and reference_type='addition' for all existing inventories
        DB::table('inventories')
            ->whereNull('transaction_type')
            ->orWhere('transaction_type', '')
            ->update([
                'transaction_type' => 'in',
                'reference_type' => 'addition',
                'reference_id' => DB::raw('id'), // Set reference_id to inventory id itself
            ]);
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Reset transaction_type and reference fields
        DB::table('inventories')
            ->update([
                'transaction_type' => 'in',
                'reference_type' => null,
                'reference_id' => null,
            ]);
    }
};
