<?php

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

return new class extends Migration
{
    /**
     * Mise à jour des tables newsletter et wallet_transactions
     * 
     * Corrections:
     * 1. Ajouter les colonnes manquantes à la table newsletters
     * 2. Ajouter withdrawal_id à wallet_transactions (clé étrangère)
     */
    public function up(): void
    {
        // 1. Ajouter les colonnes manquantes à newsletters
        if (Schema::hasTable('newsletters')) {
            Schema::table('newsletters', function (Blueprint $table) {
                if (!Schema::hasColumn('newsletters', 'name')) {
                    if (!Schema::hasColumn('newsletters', 'name')) { $table->string('name')->nullable(); }
                }
                if (!Schema::hasColumn('newsletters', 'status')) {
                    if (!Schema::hasColumn('newsletters', 'status')) { $table->enum('status', ['active', 'inactive', 'unsubscribed'])->default('active'); }
                }
                if (!Schema::hasColumn('newsletters', 'source')) {
                    if (!Schema::hasColumn('newsletters', 'source')) { $table->string('source')->nullable(); }
                }
                if (!Schema::hasColumn('newsletters', 'preferences')) {
                    if (!Schema::hasColumn('newsletters', 'preferences')) { $table->json('preferences')->nullable(); }
                }
                if (!Schema::hasColumn('newsletters', 'unsubscribe_token')) {
                    if (!Schema::hasColumn('newsletters', 'unsubscribe_token')) { $table->string('unsubscribe_token')->unique()->nullable(); }
                }
                if (!Schema::hasColumn('newsletters', 'confirmed_at')) {
                    if (!Schema::hasColumn('newsletters', 'confirmed_at')) { $table->timestamp('confirmed_at')->nullable(); }
                }
                if (!Schema::hasColumn('newsletters', 'unsubscribed_at')) {
                    if (!Schema::hasColumn('newsletters', 'unsubscribed_at')) { $table->timestamp('unsubscribed_at')->nullable(); }
                }
            });
        }

        // 2. Ajouter withdrawal_id à wallet_transactions
        if (Schema::hasTable('wallet_transactions')) {
            if (!Schema::hasColumn('wallet_transactions', 'withdrawal_id')) {
                Schema::table('wallet_transactions', function (Blueprint $table) {
                    if (!Schema::hasColumn('wallet_transactions', 'withdrawal_id')) { $table->foreignId('withdrawal_id')->nullable()->constrained('withdrawals')->onDelete('set null'); }
                });
            }
        }
    }

    public function down(): void
    {
        // Retirer withdrawal_id de wallet_transactions
        if (Schema::hasTable('wallet_transactions')) {
            Schema::table('wallet_transactions', function (Blueprint $table) {
                if (Schema::hasColumn('wallet_transactions', 'withdrawal_id')) {
                    $table->dropForeign(['withdrawal_id']);
                    $table->dropColumn('withdrawal_id');
                }
            });
        }

        // Retirer les colonnes ajoutées à newsletters (si nécessaire - attention à la perte de données)
        // Optionnel: décommenter seulement si vous voulez vraiment supprimer ces colonnes
        /*
        if (Schema::hasTable('newsletters')) {
            Schema::table('newsletters', function (Blueprint $table) {
                $columns = ['name', 'status', 'source', 'preferences', 'unsubscribe_token', 'confirmed_at', 'unsubscribed_at'];
                foreach ($columns as $column) {
                    if (Schema::hasColumn('newsletters', $column)) {
                        $table->dropColumn($column);
                    }
                }
            });
        }
        */
    }
};


