<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('cart_discounts', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->nullable()->index();
            $table->ulid('uuid')->unique();
            $table->string('name', 150);
            $table->text('description')->nullable();
            // Manual kill switch — a rule that is switched off is never
            // evaluated, whatever its schedule says (Deal convention).
            $table->boolean('is_active')->default(true);
            // Validity window: NULL starts_at = no lower bound, NULL ends_at =
            // no upper bound (same convention as product_bundles / deals).
            $table->timestamp('starts_at')->nullable();
            $table->timestamp('ends_at')->nullable();
            $table->decimal('min_cart_value', 12, 2)->default(0);
            $table->foreignId('discount_type_id')->constrained('discount_types')->restrictOnDelete();
            $table->decimal('discount_value', 12, 2);
            // Optional ceiling: a percentage rule never takes more than this
            // off the cart, whatever the threshold math says.
            $table->decimal('max_discount_amount', 12, 2)->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('deleted_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();
        });
    }

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