<?php

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

/**
 * PRC-1143 — konsolide şema: `client_discounts` tablosunun TAM tanımı (tek dosya).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('client_discounts', function (Blueprint $t) {
            $t->id();
            $t->string('uuid', 255);
            $t->unsignedBigInteger('client_id');
            $t->string('name', 255);
            $t->text('description')->nullable();
            $t->enum('type', ['percentage','fixed_amount'])->default('percentage');
            $t->decimal('discount_value', 10, 2);
            $t->unsignedBigInteger('currency_id')->nullable();
            $t->decimal('min_order_amount', 10, 2)->nullable();
            $t->decimal('max_discount_amount', 10, 2)->nullable();
            $t->enum('applies_to', ['all_products','specific_products','product_groups']);
            $t->json('product_ids')->nullable();
            $t->json('product_group_ids')->nullable();
            $t->boolean('is_active')->default(1);
            $t->date('starts_at')->nullable();
            $t->date('expires_at')->nullable();
            $t->unsignedBigInteger('created_by_admin_id');
            $t->json('metadata')->nullable();
            $t->timestamp('created_at')->nullable();
            $t->timestamp('updated_at')->nullable();
            $t->index(['client_id', 'is_active'], 'client_discounts_client_id_is_active_index');
            $t->index('created_by_admin_id', 'client_discounts_created_by_admin_id_foreign');
            $t->index('currency_id', 'client_discounts_currency_id_foreign');
            $t->index(['starts_at', 'expires_at'], 'client_discounts_starts_at_expires_at_index');
            $t->unique('uuid', 'client_discounts_uuid_unique');
            $t->foreign('client_id', 'client_discounts_client_id_foreign')->references('id')->on('clients')->onDelete('cascade');
            $t->foreign('created_by_admin_id', 'client_discounts_created_by_admin_id_foreign')->references('id')->on('admins')->onDelete('cascade');
            $t->foreign('currency_id', 'client_discounts_currency_id_foreign')->references('id')->on('currencies')->onDelete('set null');
        });
    }

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