<?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
{
    public function up(): void
    {
        Schema::table('purchases', function (Blueprint $table) {
            $table->foreignId('supplier_id')->nullable()->after('supplier_name')
                  ->constrained('suppliers')->onDelete('restrict');
            // Add new statuses for supplier flow
            // We'll use a string column instead of enum for flexibility
        });

        // Change status column from enum to string to support new statuses
        // SQLite doesn't support ALTER COLUMN, so we handle it gracefully
        if (config('database.default') !== 'sqlite') {
            DB::statement("ALTER TABLE purchases MODIFY COLUMN status VARCHAR(20) NOT NULL DEFAULT 'draft'");
        }
    }

    public function down(): void
    {
        Schema::table('purchases', function (Blueprint $table) {
            $table->dropForeign(['supplier_id']);
            $table->dropColumn('supplier_id');
        });
    }
};
