<?php

declare(strict_types=1);

namespace App\Framework\OAuth\Migrations;

use App\Framework\Database\ConnectionInterface;
use App\Framework\Database\Migration\Migration;
use App\Framework\Database\Migration\MigrationVersion;
use App\Framework\Database\Schema\Blueprint;
use App\Framework\Database\Schema\Schema;

/**
 * Create OAuth Tokens Table
 *
 * Stores OAuth access/refresh tokens for third-party provider authentication
 */
final class CreateOAuthTokensTable implements Migration
{
    public function up(ConnectionInterface $connection): void
    {
        $schema = new Schema($connection);

        $schema->createIfNotExists('oauth_tokens', function (Blueprint $table) {
            $table->id();
            $table->string('user_id', 255);
            $table->string('provider', 50);
            $table->text('access_token');
            $table->text('refresh_token')->nullable();
            $table->string('token_type', 20)->default('Bearer');
            $table->text('scope')->nullable();
            $table->bigInteger('expires_at');
            $table->timestamps();

            // Unique constraint
            $table->unique(['user_id', 'provider']);

            // Indexes
            $table->index(['user_id']);
            $table->index(['provider']);
            $table->index(['expires_at']);
        });

        $schema->execute();
    }

    public function down(ConnectionInterface $connection): void
    {
        $schema = new Schema($connection);
        $schema->dropIfExists('oauth_tokens');
        $schema->execute();
    }

    public function getVersion(): MigrationVersion
    {
        return MigrationVersion::fromTimestamp("2025_01_10_140000");
    }

    public function getDescription(): string
    {
        return "Create OAuth tokens table for third-party authentication";
    }
}
