<?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('coupons', function (Blueprint $table) {
            $table->id();
            $table->string('code')->unique()->comment('Kupon kodu');
            $table->string('name')->comment('Kupon adı');
            $table->text('description')->nullable()->comment('Kupon açıklaması');
            $table->enum('type', ['percentage', 'fixed'])->comment('İndirim tipi: yüzde veya sabit');
            $table->decimal('discount_value', 10, 2)->comment('İndirim değeri (yüzde veya sabit miktar)');
            $table->foreignId('currency_id')->constrained('currencies')->onDelete('cascade')->comment('Para birimi');
            $table->decimal('min_order_amount', 10, 2)->default(0)->comment('Minimum sepet tutarı');
            $table->decimal('max_discount_amount', 10, 2)->nullable()->comment('Maksimum indirim tutarı');
            $table->integer('usage_limit')->nullable()->comment('Toplam kullanım limiti');
            $table->integer('usage_count')->default(0)->comment('Mevcut kullanım sayısı');
            $table->boolean('is_active')->default(true)->comment('Aktif mi?');
            $table->timestamp('starts_at')->nullable()->comment('Başlangıç tarihi');
            $table->timestamp('expires_at')->nullable()->comment('Bitiş tarihi');
            $table->foreignId('created_by_admin_id')->nullable()->constrained('admins')->onDelete('set null')->comment('Oluşturan admin');
            $table->json('metadata')->nullable()->comment('Ek bilgiler');
            $table->string('target_type')->default('all')->comment('Hedefleme tipi: all, specific_products, specific_groups, specific_tlds');
            $table->json('target_products')->nullable()->comment('Hedef ürün ID\'leri (specific_products için)');
            $table->json('target_groups')->nullable()->comment('Hedef ürün grubu ID\'leri (specific_groups için)');
            $table->json('target_tlds')->nullable()->comment('Hedef TLD\'ler (specific_tlds için)');
            $table->timestamps();

            // Indexes
            $table->index(['code', 'is_active']);
            $table->index(['starts_at', 'expires_at']);
            $table->index(['type', 'currency_id']);
        });
    }

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