<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('machine_sessions', function (Blueprint $table) {
            $table->unsignedBigInteger('total_pause_duration')->default(0)->after('total_duration');
            $table->unsignedInteger('case_count')->default(0)->after('total_pause_duration');
            $table->unsignedInteger('filling_rate')->default(0)->after('case_count');
        });

        DB::statement("ALTER TABLE machine_sessions MODIFY COLUMN status ENUM('running', 'paused', 'stopped') DEFAULT 'running'");

        Schema::create('machine_pauses', function (Blueprint $table) {
            $table->id();
            $table->foreignId('machine_session_id')->constrained('machine_sessions')->cascadeOnDelete();
            $table->timestamp('pause_time')->nullable();
            $table->timestamp('resume_time')->nullable();
            $table->text('pause_reason');
            $table->unsignedBigInteger('duration')->default(0);
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('machine_pauses');

        DB::statement("ALTER TABLE machine_sessions MODIFY COLUMN status ENUM('running', 'stopped') DEFAULT 'running'");

        Schema::table('machine_sessions', function (Blueprint $table) {
            $table->dropColumn(['total_pause_duration', 'case_count', 'filling_rate']);
        });
    }
};
