<?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
    {
        Schema::table('payments', function (Blueprint $table) {
            // Add new fields for payment validation
            $table->date('date_entree')->nullable();
            $table->date('date_activation')->nullable();
            $table->boolean('is_validated')->default(false);
            $table->enum('status', ['pending', 'validated', 'rejected'])->default('pending');
        });

        // Update existing bank_transfer to trait
        DB::table('payments')
            ->where('payment_mode', 'bank_transfer')
            ->update(['payment_mode' => 'trait']);

        // Remove payments with 'other' mode
        DB::table('payments')
            ->where('payment_mode', 'other')
            ->delete();

        // Update the enum constraint
        DB::statement("ALTER TABLE payments MODIFY COLUMN payment_mode ENUM('cash', 'trait', 'cheque') NOT NULL");
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('payments', function (Blueprint $table) {
            $table->dropColumn(['date_entree', 'date_activation', 'is_validated', 'status']);
        });

        // Revert payment_mode enum
        DB::statement("ALTER TABLE payments MODIFY COLUMN payment_mode ENUM('cash', 'bank_transfer', 'cheque', 'other') NOT NULL");
    }
};
