<?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('baskets', function (Blueprint $table) {
            $table->id();
            $table->string('session_id')->index(); // Oturum ID'si
            $table->string('ip_address')->index(); // Kullanıcının IP adresi
            $table->unsignedBigInteger('client_id')->nullable()->index(); // Kullanıcı ile ilişkilendirilecek
            $table->json('basket_data'); // Sepet içeriği (JSON formatında)
            $table->timestamp('last_activity_at')->nullable(); // Son aktivite zamanı
            $table->timestamp('expires_at')->nullable()->index(); // Son kullanma tarihi
            $table->timestamps();

            // Foreign key
            $table->foreign('client_id')->references('id')->on('clients')->onDelete('set null');

            // Indexes
            $table->index(['session_id', 'ip_address']);
            $table->index(['client_id', 'last_activity_at']);
            $table->index(['expires_at', 'last_activity_at']);
        });
    }

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