<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * PRC-1143 — konsolide şema: `promotion_usages` tablosunun TAM tanımı (tek dosya).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('promotion_usages', function (Blueprint $t) {
            $t->id();
            $t->unsignedBigInteger('promotion_id');
            $t->unsignedBigInteger('client_id')->nullable();
            $t->unsignedBigInteger('order_id')->nullable();
            $t->unsignedBigInteger('client_product_id')->nullable();
            $t->decimal('discount_amount', 12, 2)->default(0.00);
            $t->decimal('original_amount', 12, 2)->default(0.00);
            $t->decimal('final_amount', 12, 2)->default(0.00);
            $t->unsignedBigInteger('currency_id')->nullable();
            $t->timestamp('used_at')->nullable();
            $t->json('metadata')->nullable();
            $t->timestamp('created_at')->nullable();
            $t->timestamp('updated_at')->nullable();
            $t->index('client_id', 'promotion_usages_client_id_foreign');
            $t->index('client_product_id', 'promotion_usages_client_product_id_foreign');
            $t->index('currency_id', 'promotion_usages_currency_id_foreign');
            $t->index('order_id', 'promotion_usages_order_id_foreign');
            $t->index(['promotion_id', 'client_id'], 'promotion_usages_promotion_id_client_id_index');
            $t->index(['promotion_id', 'client_product_id'], 'promotion_usages_promotion_id_client_product_id_index');
            $t->foreign('client_id', 'promotion_usages_client_id_foreign')->references('id')->on('clients')->onDelete('set null');
            $t->foreign('client_product_id', 'promotion_usages_client_product_id_foreign')->references('id')->on('client_products')->onDelete('set null');
            $t->foreign('currency_id', 'promotion_usages_currency_id_foreign')->references('id')->on('currencies')->onDelete('set null');
            $t->foreign('order_id', 'promotion_usages_order_id_foreign')->references('id')->on('orders')->onDelete('set null');
            $t->foreign('promotion_id', 'promotion_usages_promotion_id_foreign')->references('id')->on('promotions')->onDelete('cascade');
        });
    }

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