<?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('coupon_criterias', function (Blueprint $table) {
            $table->id();
            $table->foreignId('coupon_id')->constrained('coupons')->onDelete('cascade')->comment('Kupon ID');
            $table->enum('criteria_type', [
                'product', 'product_group', 'tld', 'country', 'currency',
                'user_type', 'order_type', 'order_amount', 'registration_date',
            ])->comment('Kriter tipi');
            $table->json('criteria_value')->comment('Kriter değerleri');
            $table->enum('criteria_operator', [
                'equals', 'in', 'not_in', 'greater_than', 'less_than',
                'greater_than_equal', 'less_than_equal', 'between', 'not_between',
            ])->default('equals')->comment('Kriter operatörü');
            $table->boolean('is_required')->default(true)->comment('Zorunlu kriter mi?');
            $table->integer('order')->default(0)->comment('Sıralama');
            $table->json('metadata')->nullable()->comment('Ek bilgiler');
            $table->timestamps();

            // Indexes
            $table->index(['coupon_id', 'criteria_type']);
            $table->index(['criteria_type', 'criteria_operator']);
            $table->index(['is_required', 'order']);
        });
    }

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