<?php

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

return new class extends Migration
{
    public function up()
    {
        // ✅ VOUCHERS TABLE ONLY
        Schema::create('vouchers', function (Blueprint $table) {
            $table->id();
            $table->string('code', 50)->unique();
            $table->enum('type', ['percentage', 'fixed'])->default('percentage');
            $table->decimal('value', 10, 2);
            $table->integer('min_purchase')->default(0);
            $table->integer('max_discount')->nullable();
            $table->integer('max_usage')->default(0);
            $table->integer('used_count')->default(0);
            $table->dateTime('valid_from')->nullable();
            $table->dateTime('valid_until')->nullable();
            $table->boolean('is_active')->default(true);
            $table->text('description')->nullable();
            $table->timestamps();
            
            $table->index('code');
            $table->index('is_active');
        });
        
        Schema::create('voucher_usage', function (Blueprint $table) {
            $table->id();
            $table->foreignId('voucher_id')->constrained()->onDelete('cascade');
            $table->string('transaction_id');
            $table->string('customer_email');
            $table->integer('discount_amount');
            $table->timestamps();
            
            $table->index('transaction_id');
            $table->index('customer_email');
        });
    }

    public function down()
    {
        Schema::dropIfExists('voucher_usage');
        Schema::dropIfExists('vouchers');
    }
};