<?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('name');
            $table->string('code')->unique();

            $table->string('type', 40);
            $table->decimal('amount', 10, 2)->default(0);
            $table->char('currency', 3)->nullable();

            $table->string('status', 40)->default('draft');
            $table->boolean('is_stackable')->default(false);
            $table->boolean('enabled_for_checkout')->default(true);
            $table->boolean('auto_apply')->default(false);

            $table->unsignedInteger('usage_limit')->nullable();
            $table->unsignedInteger('per_customer_limit')->nullable();
            $table->unsignedInteger('redemptions_count')->default(0);

            $table->text('description')->nullable();
            $table->json('metadata')->nullable();

            $table->timestamp('starts_at')->nullable();
            $table->timestamp('ends_at')->nullable();

            $table->string('paddle_discount_id')->nullable()->unique();
            $table->string('paddle_mode', 40)->default('standard');
            $table->json('paddle_snapshot')->nullable();
            $table->timestamp('paddle_last_synced_at')->nullable();

            $table->foreignIdFor(\App\Models\User::class, 'created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignIdFor(\App\Models\User::class, 'updated_by')->nullable()->constrained('users')->nullOnDelete();

            $table->timestamps();
            $table->softDeletes();

            $table->index(['status', 'starts_at', 'ends_at']);
        });
    }

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