<?php

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

return new class extends Migration
{
    /**
     * The original ENUM could not store values used on invoice payment forms
     * (e.g. account_transfer, cheque, online_payment), causing MySQL 1265 truncation.
     */
    public function up(): void
    {
        if (Schema::getConnection()->getDriverName() !== 'mysql') {
            return;
        }

        DB::statement("ALTER TABLE `payments` MODIFY `payment_method` VARCHAR(64) NOT NULL DEFAULT 'bank_transfer'");
    }

    public function down(): void
    {
        if (Schema::getConnection()->getDriverName() !== 'mysql') {
            return;
        }

        DB::statement("UPDATE `payments` SET `payment_method` = 'other' WHERE `payment_method` NOT IN ('cash','bank_transfer','credit_card','paypal','stripe','other')");

        DB::statement("ALTER TABLE `payments` MODIFY `payment_method` ENUM('cash','bank_transfer','credit_card','paypal','stripe','other') NOT NULL DEFAULT 'bank_transfer'");
    }
};
