<?php

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

return new class extends Migration
{
    // Define semua kolom baru di level class agar bisa diakses oleh up() dan down()
    protected array $newColumns = [
        'judul' => ['type' => 'string', 'after' => 'id'],
        'pengarang' => ['type' => 'string', 'after' => 'judul'],
        'edisi' => ['type' => 'string', 'after' => 'pengarang'],
        'kode_eksemplar' => ['type' => 'string', 'after' => 'edisi'],
        'isbn_issn' => ['type' => 'string', 'after' => 'kode_eksemplar'],
        'penerbit' => ['type' => 'string', 'after' => 'isbn_issn'],
        'tahun_terbit' => ['type' => 'year', 'after' => 'penerbit'],
        'tempat_terbit' => ['type' => 'string', 'after' => 'tahun_terbit'],
        'klasifikasi' => ['type' => 'string', 'after' => 'tempat_terbit'],
        'nomor_panggil' => ['type' => 'string', 'after' => 'klasifikasi'],
        'sampul' => ['type' => 'string', 'after' => 'nomor_panggil'],
        'tampilkan_opac' => ['type' => 'boolean', 'default' => 1, 'after' => 'sampul'],
        'promosi_beranda' => ['type' => 'boolean', 'default' => 0, 'after' => 'tampilkan_opac'],
        'label' => ['type' => 'string', 'after' => 'promosi_beranda'],
        // Kolom tambahan
        'biblio_id' => ['type' => 'unsignedBigInteger', 'nullable' => true, 'after' => 'label'],
        'coll_type_id' => ['type' => 'unsignedBigInteger', 'nullable' => true, 'after' => 'biblio_id'],
        'inventory_code' => ['type' => 'string', 'nullable' => true, 'after' => 'coll_type_id'],
        'received_date' => ['type' => 'date', 'nullable' => true, 'after' => 'inventory_code'],
        'supplier_id' => ['type' => 'unsignedBigInteger', 'nullable' => true, 'after' => 'received_date'],
        'order_no' => ['type' => 'string', 'nullable' => true, 'after' => 'supplier_id'],
        'order_date' => ['type' => 'date', 'nullable' => true, 'after' => 'order_no'],
        'item_status_id' => ['type' => 'unsignedSmallInteger', 'default' => 0, 'after' => 'order_date'],
        'invoice' => ['type' => 'string', 'nullable' => true, 'after' => 'item_status_id'],
        'invoice_date' => ['type' => 'date', 'nullable' => true, 'after' => 'invoice'],
        'uid' => ['type' => 'unsignedBigInteger', 'nullable' => true, 'after' => 'invoice_date'],
    ];

    public function up(): void
    {
        Schema::table('koleksis', function (Blueprint $table) {
            // 🔁 Rename kolom lama ke nama baru yang lebih deskriptif
            if (Schema::hasColumn('koleksis', 'item_code')) {
                $table->renameColumn('item_code', 'kode_koleksi');
            }

            if (Schema::hasColumn('koleksis', 'location_id')) {
                $table->renameColumn('location_id', 'lokasi_rak');
            }

            if (Schema::hasColumn('koleksis', 'source')) {
                $table->renameColumn('source', 'sumber');
            }

            // NOTE: Kolom 'price' dan 'price_currency' TIDAK ada di skema 
            // koleksis yang Anda tunjukkan sebelumnya. Hapus jika tidak ada.
            // Saya asumsikan ini adalah kolom di tabel lain atau ada di migrasi awal.
            if (Schema::hasColumn('koleksis', 'price')) {
                $table->renameColumn('price', 'harga');
            }

            if (Schema::hasColumn('koleksis', 'price_currency')) {
                $table->renameColumn('price_currency', 'mata_uang');
            }

            // ➕ Tambahkan kolom baru sesuai form admin
            foreach ($this->newColumns as $name => $attr) {
                if (!Schema::hasColumn('koleksis', $name)) {
                    $column = null;
                    switch ($attr['type']) {
                        case 'string':
                            $column = $table->string($name);
                            if (!empty($attr['nullable'])) {
                                $column->nullable();
                            } else {
                                $column->nullable();
                            }
                            break;
                        case 'year':
                            $column = $table->year($name);
                            if (!empty($attr['nullable'])) {
                                $column->nullable();
                            }
                            break;
                        case 'boolean':
                            $column = $table->boolean($name)->default($attr['default'] ?? 0);
                            break;
                        case 'unsignedBigInteger':
                            $column = $table->unsignedBigInteger($name);
                            if (!empty($attr['nullable'])) {
                                $column->nullable();
                            }
                            break;
                        case 'unsignedSmallInteger':
                            $column = $table->unsignedSmallInteger($name)->default($attr['default'] ?? 0);
                            break;
                        case 'date':
                            $column = $table->date($name);
                            if (!empty($attr['nullable'])) {
                                $column->nullable();
                            }
                            break;
                        default:
                            $column = null;
                    }

                    // Posisikan kolom hanya jika kolom 'after' ada di tabel
                    if ($column && isset($attr['after'])) {
                        if (Schema::hasColumn('koleksis', $attr['after'])) {
                            $column->after($attr['after']);
                        }
                        // jika kolom after tidak ada, biarkan DB menambahkan kolom di posisi default
                    }
                }
            }
        });
    }

    public function down(): void
    {
        Schema::table('koleksis', function (Blueprint $table) {
            // Drop semua kolom tambahan yang baru dibuat
            $dropCols = array_keys($this->newColumns);
            // Hanya drop kolom yang benar-benar ada untuk menghindari error
            $existingColsToDrop = array_filter($dropCols, fn($col) => Schema::hasColumn('koleksis', $col));
            
            if (!empty($existingColsToDrop)) {
                $table->dropColumn($existingColsToDrop);
            }

            // 🔁 Rename kolom lama kembali (reverse order dari up)
            if (Schema::hasColumn('koleksis', 'mata_uang')) {
                $table->renameColumn('mata_uang', 'price_currency');
            }
            if (Schema::hasColumn('koleksis', 'harga')) {
                $table->renameColumn('harga', 'price');
            }
            if (Schema::hasColumn('koleksis', 'sumber')) {
                $table->renameColumn('sumber', 'source');
            }
            if (Schema::hasColumn('koleksis', 'lokasi_rak')) {
                $table->renameColumn('lokasi_rak', 'location_id');
            }
            if (Schema::hasColumn('koleksis', 'kode_koleksi')) {
                $table->renameColumn('kode_koleksi', 'item_code');
            }
        });
    }
};

