<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     * Create social accounts table for OAuth authentication.
     */
    public function up(): void
    {
        Schema::create('social_accounts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->string('provider', 50); // google, facebook, github
            $table->string('provider_id', 191);
            $table->string('provider_token', 1000)->nullable();
            $table->string('provider_refresh_token', 1000)->nullable();
            $table->timestamp('token_expires_at')->nullable();
            $table->string('provider_email', 191)->nullable();
            $table->string('provider_name', 191)->nullable();
            $table->string('provider_avatar')->nullable();
            $table->timestamps();
            
            // Unique constraint for provider + provider_id
            $table->unique(['provider', 'provider_id']);
            $table->index(['user_id', 'provider']);
        });
    }

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