<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    public function up(): void
    {
        // Convert ENUM -> VARCHAR(50) to support values like "invoice_return_payment"
        DB::statement("ALTER TABLE `shop_contact_account_transactions` MODIFY `reference_type` VARCHAR(50) NOT NULL");

        // Helpful composite index for fast reference lookups by shop/type/code
        // Best-effort: ignore if it already exists
        try {
            DB::statement("CREATE INDEX `idx_shop_reference` ON `shop_contact_account_transactions` (`shop_id`, `reference_type`, `reference_code`)");
        } catch (\Throwable $e) {
            // ignore
        }
    }

    public function down(): void
    {
        // Best-effort rollback: revert to previous ENUM values.
        // Any rows containing non-enum values will fail this down migration.
        DB::statement("ALTER TABLE `shop_contact_account_transactions` MODIFY `reference_type` ENUM('invoice','goods_receipt','payment','refund') NOT NULL");

        try {
            DB::statement("DROP INDEX `idx_shop_reference` ON `shop_contact_account_transactions`");
        } catch (\Throwable $e) {
            // ignore
        }
    }
};


