<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('users', function (Blueprint $table) {
            // Add the new column after username
            $table->string('store_slug')->nullable()->unique()->after('username');
        });

        // Automatically migrate existing sellers so their current URLs don't break
        DB::statement('UPDATE users SET store_slug = username WHERE role IN ("seller", "admin", "manager")');
    }

    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('store_slug');
        });
    }
};