<?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
    {
        // WAJIB: Cek tabel ada atau tidak sebelum modifikasi
        if (Schema::hasTable('m_raw_materials')) {
            // WAJIB: Cek kolom sebelum menghapus
            if (Schema::hasColumn('m_raw_materials', 'tipe_dokumen')) {
                Schema::table('m_raw_materials', function (Blueprint $table) {
                    $table->dropColumn('tipe_dokumen');
                });
                
                DB::statement("COMMENT ON TABLE m_raw_materials IS 'Master Raw Materials - tipe_dokumen dihapus karena sekarang ada di mapping store-item (m_store_raw_materials)'");
            }
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // WAJIB: Implementasi rollback yang aman
        if (Schema::hasTable('m_raw_materials')) {
            if (!Schema::hasColumn('m_raw_materials', 'tipe_dokumen')) {
                Schema::table('m_raw_materials', function (Blueprint $table) {
                    $table->string('tipe_dokumen', 2)->default('DO')->after('spb_flag');
                });
            }
        }
    }
};
