<?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('api_usage_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained()->onDelete('set null');
            $table->string('service'); // openai, dataforseo, jina
            $table->string('endpoint')->nullable();
            $table->integer('tokens_used')->nullable();
            $table->decimal('cost', 10, 6)->default(0);
            $table->string('response_time')->nullable();
            $table->timestamps();

            $table->index(['user_id', 'created_at']);
            $table->index(['service', 'created_at']);
        });
    }

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