<?php

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

/**
 * PRC-1143 — konsolide şema: `coupon_criterias` tablosunun TAM tanımı (tek dosya).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('coupon_criterias', function (Blueprint $t) {
            $t->id();
            $t->unsignedBigInteger('coupon_id');
            $t->enum('criteria_type', ['product','product_group','tld','country','currency','user_type','order_type','order_amount','registration_date']);
            $t->json('criteria_value');
            $t->enum('criteria_operator', ['equals','in','not_in','greater_than','less_than','greater_than_equal','less_than_equal','between','not_between'])->default('equals');
            $t->boolean('is_required')->default(1);
            $t->integer('order')->default(0);
            $t->json('metadata')->nullable();
            $t->timestamp('created_at')->nullable();
            $t->timestamp('updated_at')->nullable();
            $t->index(['coupon_id', 'criteria_type'], 'coupon_criterias_coupon_id_criteria_type_index');
            $t->index(['criteria_type', 'criteria_operator'], 'coupon_criterias_criteria_type_criteria_operator_index');
            $t->index(['is_required', 'order'], 'coupon_criterias_is_required_order_index');
            $t->foreign('coupon_id', 'coupon_criterias_coupon_id_foreign')->references('id')->on('coupons')->onDelete('cascade');
        });
    }

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