<?php

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

class AddGeneratingStatusAndOrderQty extends Migration
{
    /**
     * Run the migrations.
     */
    public function up()
    {
        if (Schema::hasColumn('pr_supplier_products', 'order_qty')) { return; }
        // Add 'generating' to pr_requests status enum
        DB::statement("ALTER TABLE pr_requests MODIFY COLUMN status ENUM('generating', 'draft', 'pending_supplier', 'pending_approval', 'approved', 'completed') DEFAULT 'draft'");

        // Add order_qty column to pr_supplier_products
        Schema::table('pr_supplier_products', function (Blueprint $table) {
            $table->decimal('order_qty', 22, 4)->default(0)->after('available_qty');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down()
    {
        // Remove 'generating' from enum
        DB::statement("ALTER TABLE pr_requests MODIFY COLUMN status ENUM('draft', 'pending_supplier', 'pending_approval', 'approved', 'completed') DEFAULT 'draft'");

        // Remove order_qty column
        Schema::table('pr_supplier_products', function (Blueprint $table) {
            $table->dropColumn('order_qty');
        });
    }
}
