<?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', 'microsoft_user_id')) {
                $table->string('microsoft_user_id')->nullable()->unique();
            }
            if (! Schema::hasColumn('users', 'identity_source')) {
                $table->string('identity_source', 32)->default('local')->index();
            }
        });
    }

    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            if (Schema::hasColumn('users', 'microsoft_user_id') || Schema::hasColumn('users', 'identity_source')) {
                $cols = [];
                if (Schema::hasColumn('users', 'microsoft_user_id')) {
                    $cols[] = 'microsoft_user_id';
                }
                if (Schema::hasColumn('users', 'identity_source')) {
                    $cols[] = 'identity_source';
                }
                $table->dropColumn($cols);
            }
        });
    }
};
