<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('users', function (Blueprint $table) {
            // Ajouter les colonnes manquantes si elles n'existent pas
            if (!Schema::hasColumn('users', 'profile_photo')) {
                // Ajouter après la colonne 'email' si elle existe, sinon à la fin
                if (Schema::hasColumn('users', 'email')) {
                    if (!Schema::hasColumn('users', 'profile_photo')) { $table->string('profile_photo')->nullable(); }
                } else {
                    if (!Schema::hasColumn('users', 'profile_photo')) { $table->string('profile_photo')->nullable(); }
                }
            }
            if (!Schema::hasColumn('users', 'bio')) {
                if (!Schema::hasColumn('users', 'bio')) { $table->text('bio')->nullable(); }
            }
            if (!Schema::hasColumn('users', 'privacy_policy')) {
                if (!Schema::hasColumn('users', 'privacy_policy')) { $table->text('privacy_policy')->nullable(); }
            }
            if (!Schema::hasColumn('users', 'admin_created')) {
                if (!Schema::hasColumn('users', 'admin_created')) { $table->boolean('admin_created')->default(false); }
            }
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $columns = [];
            if (Schema::hasColumn('users', 'profile_photo')) {
                $columns[] = 'profile_photo';
            }
            if (Schema::hasColumn('users', 'bio')) {
                $columns[] = 'bio';
            }
            if (Schema::hasColumn('users', 'privacy_policy')) {
                $columns[] = 'privacy_policy';
            }
            if (Schema::hasColumn('users', 'admin_created')) {
                $columns[] = 'admin_created';
            }
            if (!empty($columns)) {
                $table->dropColumn($columns);
            }
        });
    }
};


