<?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
    {
        // Ajouter les champs manquants à la table users
        if (!Schema::hasColumn('users', 'email_verified_at')) {
            Schema::table('users', function (Blueprint $table) {
                if (!Schema::hasColumn('users', 'email_verified_at')) { $table->timestamp('email_verified_at')->nullable(); }
            });
        }

        // Améliorer le profil utilisateur
        if (!Schema::hasColumn('users', 'profile_photo')) {
            Schema::table('users', function (Blueprint $table) {
                if (!Schema::hasColumn('users', 'profile_photo')) { $table->string('profile_photo')->nullable(); }
            });
        }

        // Politique de confidentialité
        if (!Schema::hasColumn('users', 'privacy_policy_accepted')) {
            Schema::table('users', function (Blueprint $table) {
                if (!Schema::hasColumn('users', 'privacy_policy_accepted')) { $table->boolean('privacy_policy_accepted')->default(false); }
                if (!Schema::hasColumn('users', 'privacy_policy_accepted_at')) { $table->timestamp('privacy_policy_accepted_at')->nullable(); }
            });
        }

        // Conditions générales d'utilisation
        if (!Schema::hasColumn('users', 'terms_accepted')) {
            Schema::table('users', function (Blueprint $table) {
                if (!Schema::hasColumn('users', 'terms_accepted')) { $table->boolean('terms_accepted')->default(false); }
                if (!Schema::hasColumn('users', 'terms_accepted_at')) { $table->timestamp('terms_accepted_at')->nullable(); }
            });
        }

        // Marketing preferences
        if (!Schema::hasColumn('users', 'marketing_email_accepted')) {
            Schema::table('users', function (Blueprint $table) {
                if (!Schema::hasColumn('users', 'marketing_email_accepted')) { $table->boolean('marketing_email_accepted')->default(false); }
            });
        }

        // Statut d'activation du dashboard (skip email verification pour admin-added users)
        if (!Schema::hasColumn('users', 'dashboard_access_enabled')) {
            Schema::table('users', function (Blueprint $table) {
                if (!Schema::hasColumn('users', 'dashboard_access_enabled')) { $table->boolean('dashboard_access_enabled')->default(false); }
            });
        }

        // Bio/Description
        if (!Schema::hasColumn('users', 'bio')) {
            Schema::table('users', function (Blueprint $table) {
                if (!Schema::hasColumn('users', 'bio')) { $table->text('bio')->nullable(); }
            });
        }

        // Admin specific fields
        if (!Schema::hasColumn('users', 'admin_photo')) {
            Schema::table('users', function (Blueprint $table) {
                if (!Schema::hasColumn('users', 'admin_photo')) { $table->string('admin_photo')->nullable(); }
            });
        }

        if (!Schema::hasColumn('users', 'admin_bio')) {
            Schema::table('users', function (Blueprint $table) {
                if (!Schema::hasColumn('users', 'admin_bio')) { $table->text('admin_bio')->nullable(); }
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            if (Schema::hasColumn('users', 'email_verified_at')) {
                $table->dropColumn('email_verified_at');
            }
            if (Schema::hasColumn('users', 'profile_photo')) {
                $table->dropColumn('profile_photo');
            }
            if (Schema::hasColumn('users', 'admin_photo')) {
                $table->dropColumn('admin_photo');
            }
            if (Schema::hasColumn('users', 'bio')) {
                $table->dropColumn('bio');
            }
            if (Schema::hasColumn('users', 'admin_bio')) {
                $table->dropColumn('admin_bio');
            }
            if (Schema::hasColumn('users', 'privacy_policy_accepted')) {
                $table->dropColumn(['privacy_policy_accepted', 'privacy_policy_accepted_at']);
            }
            if (Schema::hasColumn('users', 'terms_accepted')) {
                $table->dropColumn(['terms_accepted', 'terms_accepted_at']);
            }
            if (Schema::hasColumn('users', 'marketing_email_accepted')) {
                $table->dropColumn('marketing_email_accepted');
            }
            if (Schema::hasColumn('users', 'dashboard_access_enabled')) {
                $table->dropColumn('dashboard_access_enabled');
            }
        });
    }
};


