<?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
    {
        if (Schema::hasTable('m_numbering_formats')) {
            Schema::table('m_numbering_formats', function (Blueprint $table) {
                // Tambah kolom baru sebagai nullable dulu
                if (!Schema::hasColumn('m_numbering_formats', 'tipe_dokumen')) {
                    $table->string('tipe_dokumen', 10)->nullable()->after('spb_type')->comment('DO, PO, atau PR');
                }
                
                if (!Schema::hasColumn('m_numbering_formats', 'tipe_barang')) {
                    $table->string('tipe_barang', 20)->nullable()->after('tipe_dokumen')->comment('Basah atau Kering');
                }
            });
            
            // Update existing data dengan default values
            DB::statement("UPDATE m_numbering_formats SET tipe_dokumen = 'DO' WHERE tipe_dokumen IS NULL");
            DB::statement("UPDATE m_numbering_formats SET tipe_barang = 'Basah' WHERE tipe_barang IS NULL");
            
            // Sekarang ubah menjadi NOT NULL
            Schema::table('m_numbering_formats', function (Blueprint $table) {
                $table->string('tipe_dokumen', 10)->nullable(false)->change();
                $table->string('tipe_barang', 20)->nullable(false)->change();
            });
            
            // Drop kolom yang tidak diperlukan
            Schema::table('m_numbering_formats', function (Blueprint $table) {
                $columnsToRemove = [
                    'document_type',
                    'item_type',
                    'format_pattern',
                    'prefix',
                    'suffix',
                    'separator',
                    'number_length',
                    'reset_period'
                ];
                
                foreach ($columnsToRemove as $column) {
                    if (Schema::hasColumn('m_numbering_formats', $column)) {
                        $table->dropColumn($column);
                    }
                }
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        if (Schema::hasTable('m_numbering_formats')) {
            Schema::table('m_numbering_formats', function (Blueprint $table) {
                // Re-add removed columns
                if (!Schema::hasColumn('m_numbering_formats', 'document_type')) {
                    $table->string('document_type', 20)->after('store_id');
                }
                
                if (!Schema::hasColumn('m_numbering_formats', 'item_type')) {
                    $table->string('item_type', 20)->after('spb_type');
                }
                
                if (!Schema::hasColumn('m_numbering_formats', 'format_pattern')) {
                    $table->string('format_pattern')->after('item_type');
                }
                
                if (!Schema::hasColumn('m_numbering_formats', 'prefix')) {
                    $table->string('prefix', 50)->nullable()->after('format_pattern');
                }
                
                if (!Schema::hasColumn('m_numbering_formats', 'suffix')) {
                    $table->string('suffix', 50)->nullable()->after('prefix');
                }
                
                if (!Schema::hasColumn('m_numbering_formats', 'separator')) {
                    $table->string('separator', 10)->nullable()->after('suffix');
                }
                
                if (!Schema::hasColumn('m_numbering_formats', 'number_length')) {
                    $table->integer('number_length')->default(4)->after('separator');
                }
                
                if (!Schema::hasColumn('m_numbering_formats', 'reset_period')) {
                    $table->string('reset_period', 20)->default('Monthly')->after('number_length');
                }
                
                // Drop new columns
                if (Schema::hasColumn('m_numbering_formats', 'tipe_dokumen')) {
                    $table->dropColumn('tipe_dokumen');
                }
                
                if (Schema::hasColumn('m_numbering_formats', 'tipe_barang')) {
                    $table->dropColumn('tipe_barang');
                }
            });
        }
    }
};
