<?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->timestamp('expire_date')->nullable();
            $table->boolean('disabled')->default(false);
            $table->double('discount')->default(0);

            $table->string('code')->unique();
            $table->enum('type', ['percentage', 'amount']);
            $table->decimal('value', 10, 2);
            $table->decimal('max_discount', 10, 2)->nullable();
            $table->decimal('min_order_value', 10, 2);
            $table->integer('usage_limit')->nullable();
            $table->integer('used')->default(0);

            $table->timestamps();
            $table->unsignedBigInteger('application_id')->nullable();
            $table->unsignedBigInteger('dealer_id')->nullable();

            $table->foreign('dealer_id')->references('id')->on('users')->onDelete('cascade');

            $table->foreign('application_id')->references('id')->on('applications')->onDelete('cascade');
        });
    }

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