<?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::create('admin_api_templates', function (Blueprint $table) {
            $table->id()->comment('ID');
            $table->foreignId('user_id')->comment('사용자 ID');
            $table->string('endpoint', 500)->comment('API 엔드포인트');
            $table->string('method', 10)->comment('HTTP 메서드');
            $table->string('name', 100)->comment('템플릿 이름');
            $table->text('description')->nullable()->comment('설명');
            $table->json('headers')->nullable()->comment('요청 헤더');
            $table->json('path_params')->nullable()->comment('경로 파라미터');
            $table->json('query_params')->nullable()->comment('쿼리 파라미터');
            $table->json('body')->nullable()->comment('요청 본문');
            $table->boolean('is_shared')->default(false)->comment('공유 여부');
            $table->timestamps();

            $table->index(['user_id', 'endpoint', 'method']);
            $table->index('is_shared');

            $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('admin_api_templates');
    }
};
