<?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('ai_token_usages', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->comment('테넌트 ID');
            $table->string('model', 100)->comment('AI 모델명 (gemini-2.0-flash 등)');
            $table->string('menu_name', 100)->comment('호출 메뉴/기능명 (AI리포트, 명함OCR 등)');
            $table->unsignedInteger('prompt_tokens')->default(0)->comment('입력 토큰 수');
            $table->unsignedInteger('completion_tokens')->default(0)->comment('출력 토큰 수');
            $table->unsignedInteger('total_tokens')->default(0)->comment('전체 토큰 수');
            $table->decimal('cost_usd', 10, 6)->default(0)->comment('예상 비용 (USD)');
            $table->decimal('cost_krw', 12, 2)->default(0)->comment('예상 비용 (KRW)');
            $table->string('request_id', 100)->nullable()->comment('요청 추적 ID');
            $table->foreignId('created_by')->nullable()->constrained('users')->comment('생성자 ID');
            $table->timestamps();

            $table->index(['tenant_id', 'created_at'], 'idx_ai_token_tenant_date');
            $table->index(['tenant_id', 'menu_name'], 'idx_ai_token_tenant_menu');
        });
    }

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