<?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
    {
        Schema::table('purchase_orders', function (Blueprint $table) {
            // Update the status enum to match the required workflow
            // draft → issued → completed/cancelled
            $table->enum('status', ['draft', 'issued', 'completed', 'cancelled'])->default('draft')->change();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('purchase_orders', function (Blueprint $table) {
            // Revert status enum
            $table->enum('status', ['ordered', 'partially_received', 'completed', 'cancelled'])->default('ordered')->change();
        });

        // Revert the status values
        DB::table('purchase_orders')->where('status', 'draft')->update(['status' => 'ordered']);
        DB::table('purchase_orders')->where('status', 'issued')->update(['status' => 'partially_received']);
        DB::table('purchase_orders')->where('status', 'issued')->update(['status' => 'approved']);
    }
};
