<?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
    {
        Schema::create('oauth_accounts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('merchant_id')->nullable()->constrained('merchants')->nullOnDelete();
            $table->string('platform', 30);
            $table->string('provider', 30);
            $table->string('openid', 100)->nullable();
            $table->string('unionid', 100)->nullable();
            $table->string('app_id', 100)->nullable();
            $table->string('nickname')->nullable();
            $table->string('avatar')->nullable();
            $table->json('raw_payload')->nullable();
            $table->timestamps();
            $table->index(['platform', 'provider']);
            $table->unique(['provider', 'openid']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('oauth_accounts');
    }
};
