<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('users', function (Blueprint $table) {
            if (! Schema::hasColumn('users', 'role_id')) {
                $table->foreignId('role_id')->nullable()->after('id')->constrained('roles');
            }

            if (! Schema::hasColumn('users', 'is_super_admin')) {
                $table->boolean('is_super_admin')->default(0)->after('role_id');
            }

            if (! Schema::hasColumn('users', 'mobile')) {
                $table->string('mobile', 20)->nullable()->after('email');
            }

            if (! Schema::hasColumn('users', 'layer_code')) {
                $table->string('layer_code', 50)->nullable()->after('mobile');
            }

            if (! Schema::hasColumn('users', 'otp_type')) {
                $table->string('otp_type', 20)->nullable()->after('layer_code');
            }

            if (! Schema::hasColumn('users', 'otp_value')) {
                $table->string('otp_value', 10)->nullable()->after('otp_type');
            }

            if (! Schema::hasColumn('users', 'landingpage')) {
                $table->string('landingpage')->nullable()->after('otp_value');
            }

            if (! Schema::hasColumn('users', 'is_active')) {
                $table->boolean('is_active')->default(1)->after('landingpage');
            }

            if (! Schema::hasColumn('users', 'department_id')) {
                $table->foreignId('department_id')->nullable()->after('is_active')->constrained('departments');
            }

            if (! Schema::hasColumn('users', 'created_by')) {
                $table->unsignedBigInteger('created_by')->nullable()->after('department_id');
            }

            if (! Schema::hasColumn('users', 'updated_by')) {
                $table->unsignedBigInteger('updated_by')->nullable()->after('created_by');
            }

            if (! Schema::hasColumn('users', 'deleted_by')) {
                $table->unsignedBigInteger('deleted_by')->nullable()->after('updated_by');
            }

            if (! Schema::hasColumn('users', 'lastlogin')) {
                $table->timestamp('lastlogin')->nullable()->after('deleted_by');
            }

            if (! Schema::hasColumn('users', 'deleted_at')) {
                $table->softDeletes()->after('lastlogin');
            }

            if (Schema::hasColumn('users', 'is_admin')) {
                $table->dropColumn('is_admin');
            }
        });
    }

    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            if (Schema::hasColumn('users', 'role_id')) {
                $table->dropForeign(['role_id']);
                $table->dropColumn('role_id');
            }

            if (Schema::hasColumn('users', 'department_id')) {
                $table->dropForeign(['department_id']);
                $table->dropColumn('department_id');
            }

            $columns = [
                'is_super_admin',
                'mobile',
                'layer_code',
                'otp_type',
                'otp_value',
                'landingpage',
                'is_active',
                'created_by',
                'updated_by',
                'deleted_by',
                'lastlogin',
                'deleted_at',
            ];

            foreach ($columns as $column) {
                if (Schema::hasColumn('users', $column)) {
                    $table->dropColumn($column);
                }
            }

            if (! Schema::hasColumn('users', 'is_admin')) {
                $table->boolean('is_admin')->default(false);
            }
        });
    }
};

