<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // Check if table exists before modifying
        if (Schema::hasTable('t_qad_sync_logs')) {
            Schema::table('t_qad_sync_logs', function (Blueprint $table) {
                // Change duration_seconds from integer to decimal to handle fractional seconds
                if (Schema::hasColumn('t_qad_sync_logs', 'duration_seconds')) {
                    $table->decimal('duration_seconds', 10, 6)->nullable()->change();
                }
                
                // Also fix qad_response_time to handle decimal values properly
                if (Schema::hasColumn('t_qad_sync_logs', 'qad_response_time')) {
                    $table->decimal('qad_response_time', 10, 6)->nullable()->change();
                }
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Check if table exists before modifying
        if (Schema::hasTable('t_qad_sync_logs')) {
            Schema::table('t_qad_sync_logs', function (Blueprint $table) {
                // Revert back to integer (this might cause data loss if there are decimal values)
                if (Schema::hasColumn('t_qad_sync_logs', 'duration_seconds')) {
                    $table->integer('duration_seconds')->nullable()->change();
                }
                
                if (Schema::hasColumn('t_qad_sync_logs', 'qad_response_time')) {
                    $table->integer('qad_response_time')->nullable()->change();
                }
            });
        }
    }
};