<?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
    {
        // Check if users table exists and add missing columns
        if (Schema::hasTable('users')) {
            Schema::table('users', function (Blueprint $table) {
                // Add missing columns if they don't exist
                if (!Schema::hasColumn('users', 'name')) {
                    $table->string('name')->nullable()->after('id');
                }
                if (!Schema::hasColumn('users', 'role')) {
                    $table->string('role')->default('Customer')->after('email');
                }
                if (!Schema::hasColumn('users', 'tenant')) {
                    $table->string('tenant')->nullable()->after('role');
                }
                if (!Schema::hasColumn('users', 'company')) {
                    $table->string('company')->nullable()->after('tenant');
                }
                if (!Schema::hasColumn('users', 'department')) {
                    $table->string('department')->nullable()->after('company');
                }
                if (!Schema::hasColumn('users', 'grade')) {
                    $table->string('grade')->nullable()->after('department');
                }
                if (!Schema::hasColumn('users', 'address')) {
                    $table->text('address')->nullable()->after('phone');
                }
                if (!Schema::hasColumn('users', 'last_login_at')) {
                    $table->timestamp('last_login_at')->nullable()->after('status');
                }
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Optionally drop the columns added
        if (Schema::hasTable('users')) {
            Schema::table('users', function (Blueprint $table) {
                if (Schema::hasColumn('users', 'name')) {
                    $table->dropColumn('name');
                }
                if (Schema::hasColumn('users', 'role')) {
                    $table->dropColumn('role');
                }
                if (Schema::hasColumn('users', 'tenant')) {
                    $table->dropColumn('tenant');
                }
                if (Schema::hasColumn('users', 'company')) {
                    $table->dropColumn('company');
                }
                if (Schema::hasColumn('users', 'department')) {
                    $table->dropColumn('department');
                }
                if (Schema::hasColumn('users', 'grade')) {
                    $table->dropColumn('grade');
                }
                if (Schema::hasColumn('users', 'address')) {
                    $table->dropColumn('address');
                }
                if (Schema::hasColumn('users', 'last_login_at')) {
                    $table->dropColumn('last_login_at');
                }
            });
        }
    }
};
